错误:getaddrinfo在nodejs中进行ENOTFOUND以获取调用

nnm*_*nnm 44 mongodb node.js express backbone.js restify

我在节点上运行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);
Run Code Online (Sandbox Code Playgroud)

然后我想在以下代码中执行get请求

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);
});
Run Code Online (Sandbox Code Playgroud)

我只是从节点开始,我甚至不知道这是否正确.我想测试express和restify的性能.我已经为我写的服务器代码做了一个apache基准测试,发现矛盾的结果更好了.所以我想通过调用远程服务然后再测试一些读写mongodb.上面的代码是我的起点.我收到了错误

{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }
Run Code Online (Sandbox Code Playgroud)

我至少在写作方向前进?我想要的那种测试的正确方法是什么?为什么我得到的结果比表达更快?任何人都可以指导我在node/express/backbone和mongodb中应用程序的最佳起点教程吗?

Kar*_*rol 83

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
};
Run Code Online (Sandbox Code Playgroud)

关于学习资源,如果你从http://www.nodebeginner.org/开始,然后通过一些好书来获得更深入的知识,你就不会出错- 我推荐了Professional Node.js,但是有很多那里.

  • 如果你有`localhost`作为你的主机但它仍然不起作用,检查你的`\ etc\hosts`文件是否有这一行:`127.0.0.1 localhost`. (5认同)

Ben*_*Kim 14

对我来说,这是因为在/ etc/hosts文件中没有添加主机名


neo*_*rol 12

检查这样的主机文件

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
Run Code Online (Sandbox Code Playgroud)

  • 这为我解决了问题!经过几个小时的搜索,我发现我的 mac 上的 `/etc/hosts` 文件是空的!在Mac上更改文件后,运行“sudo Killall -HUP mDNSResponder”以使更改生效! (3认同)

jay*_*jay 7

我无法解释为什么,但不必更改URL参数localhost,以127.0.0.1mongodb.MongoClient.connect()方法在Windows 10解决了问题.