node.js - 错误 : getaddrinfo ENOTFOUND in nodejs for get call

标签 node.js mongodb backbone.js express restify

我正在 Node 上运行一个 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);
});

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

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

我是否至少朝着写作方向前进?我想要的那种测试的正确方法是什么?为什么我得到的结果比 express 快?谁能指导我在 node/express/backbone 和 mongodb 中应用的最佳起点教程?

最佳答案

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 ,但那里有很多。

关于node.js - 错误 : getaddrinfo ENOTFOUND in nodejs for get call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23259697/

相关文章:

mongodb - mongodb 是否适合用于管理和分发带预订的旅游库存的 SaaS 平台?

mongodb - Pentaho 中没有 MongoDB 选项

backbone.js - 如何更新集合中的模型?

javascript - 在 Backbone js View 中分配 ' this' 上下文

backbone.js - Dart 和第三方库

javascript - 如何将用户数据存储到node js express-session中

node.js - 在 Mongoose 中清理用户输入

node.js - WebStorm 报告 "Unresolved function or method"- 如何摆脱这个问题?

MongoDB 3.0.1 复制设置

node.js - docker compose up 后 Postgres 立即关闭?