错误:getaddrinfo ENOTFOUND in nodejs for get call [英] Error: getaddrinfo ENOTFOUND in nodejs for get call

查看:48
本文介绍了错误:getaddrinfo ENOTFOUND in nodejs for get call的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running a web server on node the code for which is given below

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);

然后我想在下面的代码中做一个get请求

And then i want to do a get request in the following code

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:
');
        process.stdout.write(d);
        console.info('

Call completed');
    });

});

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

我刚开始使用 node,我什至不知道这是否是正确的方法.我想测试express和restify的性能.我对我写的服务器代码做了一个apache基准测试,发现restify更好的矛盾结果.所以我想通过调用远程服务来测试更多,然后再测试读写mongodb.上面的代码是我的起点.我收到错误

I am just starting with node and i dont even know if this is the right way. I want to test the performance of express and restify.I have done a apache benchmark test for the server code i wrote and found contradicting results that restify is better.So i want to test out some more by making calls to remote services and then later read write to mongodb.The above code is my starting point.I am getting the error

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

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

Am i atleast heading in the write direction? What is the right way to the kind of tests that i want to? Why did i get the result restify faster than express? Can anyone guide me to the best starting point tutorials for application in node/express/backbone and mongodb?

推荐答案

getaddrinfo ENOTFOUND 表示客户端无法连接到给定地址.请尝试在没有 http 的情况下指定主机:

getaddrinfo ENOTFOUND means client was not able to connect to given address. Please try specifying host without 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/<开始,你不会出错/a> 然后阅读一些好书以获得更深入的知识 - 我推荐 专业 Node.js ,但有很多.

Regarding learning resources, you won't go wrong if you start with http://www.nodebeginner.org/ and then go through some good book to get more in-depth knowledge - I recommend Professional Node.js , but there's many out there.

这篇关于错误:getaddrinfo ENOTFOUND in nodejs for get call的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆