5

I've got a program on my machine listening on port 8080 to respond to HTTP requests. But when I try to go to http://localhost:8080/ in a browser...

Chrome says:

The localhost page isn’t working

localhost didn’t send any data. ERR_EMPTY_RESPONSE

Firefox redirects the request to www.localhost.com:8080 which of course isn't correct. My research suggests that it is doing this because it can't find localhost.

Safari says it can't open the page because "the server unexpectedly dropped the connection."

However, if I go to http://127.0.0.1:8080 the page loads correctly.

Therefore, it would seem that for some reason OS X isn't properly resolving localhost to 127.0.0.1. If I try to ping localhost from the command line, it works, but my understanding is that name resolution works differently in different places.

I have read various issues where localhost doesn't resolve e.g. from ping, but I am not having that problem. All the cases where localhost were not working in the browser appear to not have solutions related to the actual problem I am having. I have also seen places where people have mentioned that localhost does work in the browser, so I am wondering what I am doing wrong here.

From comments
If I try the IPv6 address for localhost http://[::1]:8080/ then I get the same errors as using localhost.

9
  • Firefox redirects a properly formed "http://" url?
    – Allan
    Jan 18, 2017 at 19:28
  • What is your server and what do its logs show
    – mmmmmm
    Jan 18, 2017 at 19:32
  • @Allan yes it does
    – Michael
    Jan 18, 2017 at 19:37
  • @Mark my server is a node.js app using express. it produces no output when trying to connect to "localhost" but logs the connecting IP address and URL when connecting via 127.0.0.1
    – Michael
    Jan 18, 2017 at 19:38
  • Perhaps OSX is finding localhost as the IPv6 address ::1 - what does entering http://::1:8080 give
    – mmmmmm
    Jan 20, 2017 at 0:07

3 Answers 3

8

First check name resolution with the system resolver:

dscacheutil -q host -a name localhost

If you get an output like:

name: localhost
ipv6_address: ::1

name: localhost
ip_address: 127.0.0.1

the system resolver works correctly.

Stop your node.js app, create a simple node.js server by using this file with an arbitrary name (e.g. multiserver.js):

var http = require('http');
function handler(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
};
http.createServer(handler).listen(3000, '127.0.0.1');
http.createServer(handler).listen(3001, 'localhost');
http.createServer(handler).listen(3002, '192.168.0.5');
http.createServer(handler).listen(3003, 'host.example.com');
http.createServer(handler).listen(3004, 'LocalHostName.local');

(replace the IP of the :3002 server by an IP of your host (ifconfig), the host.example.com of the :3003 server by the output of echo $HOSTNAME and LocalHostName by the output of scutil --get LocalHostName)

and start it with

node ../multiserver.js

Now test your "multiserver" with Google Chrome by entering http://name:port (don't forget to prepend http://).

The first two ports should work with both "names" (127.0.0.1/localhost). So entering either http://127.0.0.1:3000, http://127.0.0.1:3001, http://localhost:3000 or http://localhost:3001 should all yield "Hello World".

Also check the nine permutations of the next three ports and names (e.g. http://LocalHostName.local:3002 or http://192.168.0.5:3004) which should also all yield "Hello World" if the hostname/DNS are set up correctly.

If all this works, your express/node.js app is faulty.

2
  • Definitely seems to be something with my app... although I'm now calling createServer exactly as you do in my app (not using express, basically stripped out everything http to match your example) and it still won't respond on localhost while your example will.
    – Michael
    Jan 20, 2017 at 22:03
  • When I try dscacheutil -q host -a name localhost I get nothing. If I try dscacheutil -q host -a name apple.com I do get a result however.
    – Dave Sag
    Sep 3, 2018 at 2:24
-2

Maybe try to use the port 80. You said 127.0.0.1:8080 works so I am wondering if 127.0.0.1 works too. I think if you type in localhost it will resolve to localhost:80 where nothing is listening.

-2

I am not a Javascript programmer so if anyone has better links please edit.

The issue is that your server is listening on the IPv4 address only and it should be listening on the IPv6 address as well.

How you chnage your server is off topic for Ask Different as it requires code changes. This StackOverflow answer might help.

Apple has changed macOS from El Capitan onwards to look for IPv6 addresses in preference to the older ones as reported here

You can test the server directly at http://[::1]:8080

In the worse case you could remove localhosts alias to ::1 but better if you can't do anything then just use http://127.0.0.1:8080 in your testing

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .