nodejs cannot resolve 'localhost' on windows

与世无争的帅哥 提交于 2019-11-27 06:55:21

问题


The following code produces an exception in node.js under windows:

var Socket = require("net").Socket;

socket = new Socket();
socket.connect(80, "localhost");

here's the message:

    events.js:2083: Uncaught Error: getHostByName ENOTFOUND

When I remove localhost, it works fine. What could be causing this? I tried turning the firewall off, but to no effect.


回答1:


node.js uses c-ares resolver and ignores system resolver completely. So the advices regarding C:\WINDOWS\system32\drivers\etc\hosts are probably irrelevant.

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:\WINDOWS\system32\drivers\etc\hosts. If it doesn't and doesn't have builtin support for localhost - then you will have to use 127.0.0.1




回答2:


In my case adding the Microsoft Loopback Adaptor fixed this issue.

  1. Click the Start Menu Orb. In the search box Type hdwwiz and then Click the hdwwize.exe program link.
  2. Now the Add Hardware wizard should be open.
  3. Scroll down the list and Select Network Adapters then Click Next.
  4. Give the next window a moment to load, and then Click Microsoft and Select Microsoft Loopback Adapter.

From: http://www.groovypost.com/howto/microsoft/install-a-loopback-adapter-in-windows-7/




回答3:


Option 1: Dont' use windows ;)

Option 2: in C:\WINDOWS\system32\drivers\etc\hosts

add :

 127.0.0.1       localhost


来源:https://stackoverflow.com/questions/6997442/nodejs-cannot-resolve-localhost-on-windows

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