7

I'm having difficulties understanding why the following line of code works in node.js:

server.listen(12345, "127.0.0.1"); // works

but this one does not:

server.listen(12345, "localhost"); // fails

Coding localhost literally results in the following error thrown:

events.js:45
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: ECONNREFUSED, Could not contact DNS servers
    at IOWatcher.callback (dns.js:74:15)

I do not understand why it should 'contact DNS servers' as it's localhost as defined in my HOSTS file (I'm using node.js under Windows).

Why doesn't hardcoding localhost work?

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 4
    Have you checked your hosts file to ensure that the localhost mapping has not been overwritten? What happens if you ping localhost from the command line? – shanethehat Jul 24 '11 at 11:08
  • @shanethehat: That works just fine; my web server at `http://localhost/` does not have any problems either. – pimvdb Jul 24 '11 at 11:10
  • Have you tried using "http:// localhost" (space added as the comment is altered otherwise)? I ask because you have said you tried "localhost" and then tried it with http in the browser. If you just type "localhost" in the browser, the browser does you a favor and automatically prepends the http:// for you. – rf43 Jul 24 '11 at 12:41
  • @DDoSAttack: Thanks but no luck. `http://localhost` does not work; `http://127.0.0.1` does not work either, only exactly `127.0.0.1`. – pimvdb Jul 24 '11 at 12:44

3 Answers3

4

Are uou using cygwin build? Did you try 'Set up Domain Name Resolution (DNS)' :

Cygwin internally uses Windows for DNS queries. node.js uses the c-ares library that relies on /etc/resolv.conf. Cygwin ships with an empty /etc/resolv.conf. In order to enabled networking from your scripts, add these IPs to the file (Google Public DNS):

$ vim /etc/resolv.conf

nameserver 8.8.8.8
nameserver 8.8.4.4
Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • I did not, but where should I save the file `/etc/resolv.conf` on Windows? – pimvdb Jul 24 '11 at 15:46
  • '/' is usually usually cygwin installation dir, i e if you have cygwin installed at `c:\cygwin` then `resolv.conf` is `c:\cygwin\etc\resolv.conf` – Andrey Sidorov Jul 26 '11 at 03:20
3

It turned out that using the cygwin build was the problem.

I looked at http://nodejs.org/ and I discovered that only recently a native Windows binary file was made available, which works like a charm.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
1

I've never personally used node.js, however some quick searching turned up some interesting results...

From what I can tell, everything I've seen says that you need to find the IP address that you are looking to listen to. Here is an example: Get local IP address in node.js this behavior makes sense too. Domain names are arbitrary, it's the IP that matters so sometimes that is all something wants to deal with.

If I'm missing something about how node.js works I am sorry.

Community
  • 1
  • 1
rf43
  • 4,385
  • 3
  • 23
  • 28