Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I listen on 'localhost' but can I listen on '127.0.0.1'?

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?

like image 862
pimvdb Avatar asked Jul 24 '11 11:07

pimvdb


1 Answers

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
like image 53
Andrey Sidorov Avatar answered Oct 04 '22 21:10

Andrey Sidorov