nodejs httprequest with data - getting error getaddrinfo ENOENT

こ雲淡風輕ζ 提交于 2019-11-27 12:02:55

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".

Keep it just the domain.

Here's an example: http://allampersandall.blogspot.com/2012/03/nodejs-http-request-example.html

rockerston

The problem can also happen if you have a trailing slash:

Good: "www.google.com"

Bad: "www.google.com/"

Avoid all of these hostname/protocol/port/slash problems by using the request module instead of http

https://github.com/mikeal/request

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.

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,
};

I was getting this error when calling server.listen(PORT, HOST); where HOST could not be resolved back to the local machine.

Once I changed this back to a hostname/domain name/ip that the local machine resolved to, this error went away.

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

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.conf file.

I added Google's nameserver at the beginning of my resolv.conf file (8.8.8.8) and the code started working just fine once again, no more error.

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.

At first I checked if I was having any DNS issues by fetching the URL I needed using wget on 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.

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.

  • Give your Lambda function a VPC.
  • Select at least 2 Subnets.
  • And select a Security Group.

I spent a day until I found this fix, hope it helps someone else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!