node.js 错误:getaddrinfo ENOTFOUND
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44009848/
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
Error: getaddrinfo ENOTFOUND
提问by Anderson
I have a simple Node.js bot that makes an HTTP request each second to a rest API.
我有一个简单的 Node.js bot,它每秒向 rest API 发出一个 HTTP 请求。
If the returned data is right then I construct an URL where I HTTP POST.
如果返回的数据是正确的,那么我会在其中构建一个 URL 来进行 HTTP POST。
Everything works alright but after ~4-5hrs of running I got this error
一切正常,但运行约 4-5 小时后出现此错误
0|server | error: Error: getaddrinfo ENOTFOUND www.rest-api.com www.rest-api.com:443
0|server | at errnoException (dns.js:28:10)
0|server | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:73:26)
Can someone explain to me why this has happened?
有人可以向我解释为什么会发生这种情况吗?
After I restart my server everything got working.
重新启动服务器后,一切正常。
I'm using axiosto make the http requests.
我正在使用axios发出 http 请求。
回答by William
I met the same issue and solved it!
我遇到了同样的问题并解决了它!
try:
尝试:
sudo vi /etc/hosts
sudo vi /etc/hosts
and add:
并添加:
127.0.0.1 localhost
127.0.0.1 本地主机
to hosts
给主人
回答by arboreal84
The symptom is that the remote address cannot be resolved.
症状是无法解析远程地址。
The cause could be many things. First, try to see if it's node specific by trying to resolve the address directly:
原因可能有很多。首先,通过尝试直接解析地址来尝试查看它是否特定于节点:
$ nslookup www.rest-api.com
Or:
或者:
$ dig www.rest-api.com
If that doesn't work, you've got a connectivity problem. It could be anything. Try looking at how long your DHCP lease lasts if you are using DHCP.
如果这不起作用,则您遇到了连接问题。它可以是任何东西。如果您使用 DHCP,请尝试查看您的 DHCP 租约持续多长时间。
However if that does work fine but your node application still fails, you might be running into this: https://github.com/nodejs/node/issues/5436, which is a bug in an underlying library. You can implement the workaround mentioned in that thread, which is specifying the IP version family through the following parameter { family: 4 }as a part of your request options.
但是,如果这样做工作正常但您的节点应用程序仍然失败,您可能会遇到以下问题:https: //github.com/nodejs/node/issues/5436,这是底层库中的一个错误。您可以实施该线程中提到的解决方法,即通过以下参数指定 IP 版本系列{ family: 4 }作为请求选项的一部分。
回答by CodeCaptain
I had this issue becuase there was a typo in my URL - the path did not exist. Gotta be careful with long subdomain URLs. I discovered the issue while using the Postman Console - available by going to View > Show Postman Console.
我遇到了这个问题,因为我的 URL 中有一个错字 - 路径不存在。必须小心长子域 URL。我在使用 Postman Console 时发现了这个问题 - 可以通过转到查看 > 显示 Postman Console 来获得。

