Search code examples
node.jsmacosnetwork-programmingnode-inspector

Why won't localhost route to 127.0.0.1 in chrome (OSX)?


When I launch a script using node-debug, it attempts to navigate to the URL localhost/debug?port=5858 but does not find a page served there.

If I change the "localhost" to 127.0.0.1 everything works fine.

I can ping localhost and it resolves appropriately to 127.0.0.1

Any ideas?


Solution

  • localhost has an IPv6 address (::1) as well as an IPv4 address (127.0.0.1). My guess is that your web server is only serving over IPv4, and chrome is preferring the IPv6 address.

    $ dscacheutil -q host -a name localhost
    name: localhost
    ipv6_address: ::1
    ipv6_address: fe80:1::1
    
    name: localhost
    ip_address: 127.0.0.1
    $ netstat -an | grep "[.]80 .*LISTEN"
    tcp46      0      0  *.80                   *.*                    LISTEN 
    

    Note the "tcp46" in the last line -- that means the web server is listening for both TCP/IPv4 and TCP/IPv6 connections, If you run the same command, I suspect you'll see just "tcp4".

    I'm not familiar with Node.js, but this posting seems to imply you can listen on both localhost addresses by using server.listen(80, '::'). Alternately, you could create separate listeners for the IPv4 and IPv6 addresses, as described here.