错误:getaddrinfo ENOTFOUND在获取调用的nodejs中

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

我刚刚开始与节点,我甚至不知道这是否是正确的方法。 我想testingexpress和restify的性能。我已经为我写的服务器代码做了一个apache基准testing,发现restify比较好的结果是矛盾的。所以我想通过调用远程服务来testing一些,然后再读写到mongodb上面的代码是我的出发点,我得到的错误

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

我是否在写作方向? 什么是我想要的那种testing的正确途径? 为什么我得到的结果比expression更快? 任何人都可以指导我在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/开始,然后通过一些好书来获得更深入的知识,你不会错误&#x7684; – 我推荐Professional Node.js ,但是有很多那里。

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

我无法解释为什么,但在Windows 10的mongodb.MongoClient.connect()方法上将url参数从localhost改为127.0.0.1解决了这个问题。

检查这样的主机文件

 ## # 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 

我也有同样的问题,我使用正确的代理来解决它。 如果您使用代理networking,请仔细检查您的代理设置。

希望对你有帮助 –

我有亚马逊服务器相同的问题,我改变我的代码

 var connection = mysql.createConnection({ localAddress : '35.160.300.66', user : 'root', password : 'root', database : 'rootdb', }); 

检查mysql节点模块https://github.com/mysqljs/mysql

 var http = require('http'); var options = { host: 'localhost', port: 80, path: '/broadcast' }; var requestLoop = setInterval(function(){ http.get (options, function (resp) { resp.on('data', function (d) { console.log ('data!', d.toString()); }); resp.on('end', function (d) { console.log ('Finished !'); }); }).on('error', function (e) { console.log ('error:', e); }); }, 10000); var dns = require('dns'), cache = {}; dns._lookup = dns.lookup; dns.lookup = function(domain, family, done) { if (!done) { done = family; family = null; } var key = domain+family; if (key in cache) { var ip = cache[key], ipv = ip.indexOf('.') !== -1 ? 4 : 6; return process.nextTick(function() { done(null, ip, ipv); }); } dns._lookup(domain, family, function(err, ip, ipv) { if (err) return done(err); cache[key] = ip; done(null, ip, ipv); }); }; // Works fine (100%) 

我遇到了同样的问题,经过一些试验和错误发现,我的hosts文件定制导致了这个问题。

我的localhost DNS被覆盖,因为一个简单的ping失败:

 $ ping http://localhost # ping: cannot resolve http://localhost: Unknown host 

解决了/private/etc/hosts文件的configuration后,ping localhost成功运行,并且不再收到您所看到的错误:

 # Fatal error: getaddrinfo ENOTFOUND 

要解决该错误:

[错误:getaddrinfo ENOTFOUND]代码:'ENOTFOUND',errno:'ENOTFOUND',系统调用:'getaddrinfo'}“

你只需要打开互联网连接。