nodejs无法在Windows上解析'localhost' [英] nodejs cannot resolve 'localhost' on windows
问题描述
以下代码在windows下node.js产生异常:
The following code produces an exception in node.js under windows:
var Socket = require("net").Socket;
socket = new Socket();
socket.connect(80, "localhost");
这是消息:
events.js:2083: Uncaught Error: getHostByName ENOTFOUND
当我删除 localhost
时,它工作正常.这可能是什么原因造成的?我尝试关闭防火墙,但没有效果.
When I remove localhost
, it works fine. What could be causing this?
I tried turning the firewall off, but to no effect.
推荐答案
node.js 使用 c-ares 解析器,完全忽略了系统解析器.所以关于 C:WINDOWSsystem32driversetchosts 的建议可能是无关紧要的.
node.js uses c-ares resolver and ignores system resolver completely. So the advices regarding C:WINDOWSsystem32driversetchosts are probably irrelevant.
c-ares 库读取某些系统配置文件 - 例如在 Windows 和 Cygwin 上,它读取/etc/resolv.conf.所以你应该检查它是否读取/etc/hosts 或 C:WINDOWSsystem32driversetchosts.如果它没有并且没有对 localhost 的内置支持 - 那么您将不得不使用 127.0.0.1
c-ares library reads certain system config files - e.g. on Windows and Cygwin it reads /etc/resolv.conf. So you should check if it reads /etc/hosts or C:WINDOWSsystem32driversetchosts. If it doesn't and doesn't have builtin support for localhost - then you will have to use 127.0.0.1
这篇关于nodejs无法在Windows上解析'localhost'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!