What ENOTFOUND error in an http request, happening

2019-09-08 08:31发布

问题:

I have a nodejs server, running on an hosted cloud machine. An http request

var req = Http.request(options, function(res) { ... }

with options

host: "www.mysite.com”    
port: "8080"
method: “GET”
path: “/someurl”

reply with this error sometimes (let's say one time out of ten)

{"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo"}

Now, the question is what this error could mean.

The request seems to be correct (right path, right port, url alive and rocking, no http protocol included in path, the path is to a drupal page of a site running on the same machine) and it works almost all the time. Sometimes, with no apparent reason, it fails.

Recently, I noticed, on the machine, issues about ipv6 and I had to disable it on sysctl to make things work. The problem raised with suspicious precision after the issue about ipv6 showed up. Maybe this error is caused by some dns issues on the machine itself (so i have to blame my host)?

回答1:

It's DNS errors, specifically, Node can't resolve a hostname "www.mysite.com" to an IP-address.

There can be various reasons for this:

  • the hostname is just invalid
  • your network connection is faulty
  • your DNS server is faulty
  • the DNS server that handles "mysite.com" is faulty

As a heads-up: while your question is valid, debugging the underlying cause (DNS issues) is off-topic on StackOverflow.