一尘不染

错误:nodejs中的getaddrinfo ENOTFOUND进行了get调用

node.js

我在节点上运行Web服务器,其代码如下

var restify = require('restify');

var server = restify.createServer();

var quotes = [
  { author : 'Audrey Hepburn', text : "Nothing is impossible, the word itself says 'I'm possible'!"},
  { author : 'Walt Disney', text : "You may not realize it when it happens, but a kick in the teeth may be the best thing in the world for you"},
  { author : 'Unknown', text : "Even the greatest was once a beginner. Don't be afraid to take that first step."},
  { author : 'Neale Donald Walsch', text : "You are afraid to die, and you're afraid to live. What a way to exist."}
];

server.get('/', function(req, res) {
  res.json(quotes);
});

server.get('/quote/random', function(req, res) {
  var id = Math.floor(Math.random() * quotes.length);
  var q = quotes[id];
  res.json(q);
});

server.get('/quote/:id', function(req, res) {
  if(quotes.length <= req.params.id || req.params.id < 0) {
    res.statusCode = 404;
    return res.send('Error 404: No quote found');
  }

  var q = quotes[req.params.id];
  res.json(q);
});

server.listen(process.env.PORT || 3011);

然后我想在以下代码中进行获取请求

var https = require('http');

/**
 * HOW TO Make an HTTP Call - GET
 */
// options for GET
var optionsget = {
    host : 'http://localhost',
    port : 3010,
    path : '/quote/random', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};

console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');

// do the GET request
var reqGet = https.request(optionsget, function(res) {
    console.log("statusCode: ", res.statusCode);
    // uncomment it for header details
//  console.log("headers: ", res.headers);


    res.on('data', function(d) {
        console.info('GET result:\n');
        process.stdout.write(d);
        console.info('\n\nCall completed');
    });

});

reqGet.end();
reqGet.on('error', function(e) {
    console.error(e);
});

我只是从节点开始,我什至不知道这是否是正确的方法。我想测试express和restify的性能。我对我编写的服务器代码进行了apache基准测试,发现矛盾的结果是restify更好。所以我想通过调用远程服务来进行更多测试读写到mongodb。上面的代码是我的起点。我遇到了错误

{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

我是否至少朝写方向前进?我要进行哪种测试的正确方法是什么?为什么我得到的结果重新调整的速度快于表达?谁能引导我找到在node / express /
backbone和mongodb中应用的最佳入门教程?


阅读 815

收藏
2020-07-07

共1个答案

一尘不染

getaddrinfo ENOTFOUND表示客户端无法连接到给定地址。请尝试指定不带http的主机:

var optionsget = {
    host : 'localhost',
    port : 3010,
    path : '/quote/random', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};

关于学习资源,如果您先从http://www.nodebeginner.org/开始学习,然后再读一本好书以获取更深入的知识,则不会出错-
我推荐使用Professional
Node.js
,但是这里有很多内容那里。

2020-07-07