带有数据的 nodejs httprequest - 获取错误 getaddrinfo ENOENT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9580226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nodejs httprequest with data - getting error getaddrinfo ENOENT
提问by Brij Raj Singh - MSFT
Update - Answered by self
更新 - 由自己回答
I see one has to make sure that the DNS is resolved properly from the machine, check out the node documentationto make sure that domain is resolvable.
我看到必须确保从机器正确解析 DNS,查看节点文档以确保该域可解析。
Original Question
原始问题
i am writing a nodes based program,in which the user can ask me to do a httprequest on their behalf {off course they provide me with some data, and method to call with} but every time i do a httprequest it gives me an error
我正在编写一个基于节点的程序,其中用户可以要求我代表他们执行 httprequest {当然他们向我提供了一些数据和调用方法} 但是每次我执行 httprequest 它都会给我一个错误
getaddrinfo ENOENT this is how my code looks
getaddrinfo ENOENT 这是我的代码的样子
function makehttprequest(deviceid, httpaction, httppath,methods, actiondata, callback) {
console.log('we are here with httpaction' + httpaction + ' path ' + httppath + ' method ' + methods + ' action data ' + actiondata);
//do the http post work, get the data, and call the callback function with return data
var options = {
host: httpaction,
port: 80,
path: httppath,
method: methods
};
try {
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
} catch(e) {
console.log('error as : ' + e.message);
}
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
console.log('writing data to request ..');
req.write(actiondata);
console.log('finished writing data to request…');
req.end();
console.log('request ended…');
}
回答by Micah
I've seen this happen when your host (which you pass in as httpaction) has the scheme (so "http://") in front of it. Your host should strictly be the domain like "www.google.com" not "http://www.google.com" or "www.google.com/hello-world" or "http://www.google.com/hello-world".
我已经看到当你的主机(你作为 httpaction 传入)在它前面有方案(所以“http://”)时会发生这种情况。您的主机应严格使用“www.google.com”之类的域,而不是“ http://www.google.com”或“www.google.com/hello-world”或“ http://www.google.com” /你好世界“。
Keep it just the domain.
只保留域。
Here's an example: http://allampersandall.blogspot.com/2012/03/nodejs-http-request-example.html
这是一个例子:http: //allampersandall.blogspot.com/2012/03/nodejs-http-request-example.html
回答by rockerston
The problem can also happen if you have a trailing slash:
如果您有斜杠,也会出现此问题:
Good:"www.google.com"
好的:"www.google.com"
Bad:"www.google.com/"
坏的:"www.google.com/"
回答by jpillora
Avoid all of these hostname/protocol/port/slash problems by using the requestmodule instead of http
通过使用request模块而不是避免所有这些主机名/协议/端口/斜线问题http
回答by Sushil
I hit this again today for a silly mistake. This was because port number was put as part of the hostname.
我今天又犯了一个愚蠢的错误。这是因为端口号被作为主机名的一部分。
// wrong. gets error getaddrinfo ENOENT
var options = {
hostName: 'localhost:1337',
....
}
// correct
var options = {
hostname: 'localhost',
port: 1337,
};
回答by alexfernandez
I was getting [Error: Getaddrinfo ENOENT], but it was right after getting [Error: connect EMFILE]; since I am doing load tests with thousands of clients the EMFILE error (the root cause) was being opaqued. The solution was the same as for EMFILE: increase the number of file descriptors. Just adding it here for completeness in case anyone else has the same problem.
我收到了 [Error: Getaddrinfo ENOENT],但在收到 [Error: connect EMFILE] 之后就正确了;因为我正在对数千个客户端进行负载测试,所以 EMFILE 错误(根本原因)是不透明的。解决方案与 EMFILE 相同:增加文件描述符的数量。只是为了完整起见,在这里添加它,以防其他人遇到同样的问题。
回答by russholio
I was getting this error when calling server.listen(PORT, HOST);where HOST could not be resolved back to the local machine.
我在调用server.listen(PORT, HOST);where HOST 无法解析回本地机器时收到此错误。
Once I changed this back to a hostname/domain name/ip that the local machine resolved to, this error went away.
一旦我把它改回本地机器解析的主机名/域名/ip,这个错误就消失了。
Since I was trying to connect via a hostname for dev purposes I added an entry to my hosts file with the desired hostname and ensured that this matched the hostname passed to server.listen()
由于我出于开发目的尝试通过主机名进行连接,因此我使用所需的主机名在我的主机文件中添加了一个条目,并确保这与传递给的主机名匹配 server.listen()
回答by Gubatron
If all your code seems to be alright and you're still get the same error, which was my case, the solution was checking the nameservers on my /etc/resolv.conffile.
如果您的所有代码似乎都没有问题,而您仍然遇到相同的错误(这就是我的情况),则解决方案是检查我/etc/resolv.conf文件中的名称服务器。
I added Google's nameserver at the beginning of my resolv.conffile (8.8.8.8) and the code started working just fine once again, no more error.
我在resolv.conf文件 (8.8.8.8)的开头添加了 Google 的名称服务器,代码再次开始正常工作,不再出现错误。
It's worth noticing that this error started happening on me on Feb. 4th 2015 after I ran an sudo apt-get upgrade, my node js must have been updated and a bug introduced which seemed to be incompatible with the nameservers I had.
值得注意的是sudo apt-get upgrade,在我运行 2015 年 2 月 4 日之后,这个错误开始发生在我身上,我的节点 js 一定已经更新,并且引入了一个似乎与我拥有的名称服务器不兼容的错误。
At first I checked if I was having any DNS issues by fetching the URL I needed using wgeton the command line, I got the contents of the target url fine so I didn't think it was actually a DNS issue, but it was.
起初,我通过获取我需要wget在命令行上使用的 URL 来检查是否有任何 DNS 问题,我得到了目标 URL 的内容,所以我认为这实际上不是 DNS 问题,但确实如此。
回答by Ninos
I had a similar issue but running as a AWS Lambda function, so in case someone is having this issue with Lambda functions this is how I got it resolved.
我有一个类似的问题,但作为 AWS Lambda 函数运行,所以如果有人遇到 Lambda 函数的这个问题,我就是这样解决的。
- Give your Lambda function a VPC.
- Select at least 2 Subnets.
- And select a Security Group.
- 为您的 Lambda 函数提供一个 VPC。
- 选择至少 2 个子网。
- 并选择一个安全组。
I spent a day until I found this fix, hope it helps someone else.
我花了一天时间才找到此修复程序,希望对其他人有所帮助。

