39

If I remember correctly it used to display "localhost" a few days ago. I am not sure what had changed that made server.address().address return double colons (::) instead. I read here that it returns an IPv6 address (::) if it is available but it's disabled on my PC. https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback

Jake
  • 1,380
  • 3
  • 14
  • 27

2 Answers2

81

As the docs say,

Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise. A port value of zero will assign a random port.

So, the following code would print running at http://:::3456:

var express      = require('express');
var app          = express();
var server = app.listen(3456, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('running at http://' + host + ':' + port)
});

But if you add an explicit hostname:

var server = app.listen(3456, "127.0.0.1", function () {

It would print what you want to see: running at http://127.0.0.1:3456

Also, you might want to use some IP lib as pointed in this answer

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
Alexander Mikhalchenko
  • 4,525
  • 3
  • 32
  • 56
  • 6
    IPv6 literals in URLs should be surrounding by square brackets, e.g. `http://[::]:3456` – Steve-o Nov 22 '15 at 16:58
  • Thank you I understand it better now. What I still don't understand is why it is choosing the IPv6 instead of the IPv4 even though I have IPv6 disabled on my machine? – Jake Nov 23 '15 at 00:32
  • @Jake I'm not sure about disabling IPv6m but this also happens in old node versions (in particlular, 0.12) – Alexander Mikhalchenko Nov 23 '15 at 09:57
  • @Jake It's quite possible you haven't actually disabled IPv6 on your machine. For instance, it's almost impossible to do so on Windows since Windows Vista/2008, and succeeding to do so breaks the system so badly Microsoft won't support it and will tell you only to re-enable IPv6 or reinstall Windows. – Michael Hampton Sep 26 '18 at 17:54
  • Square brackets work well with `127.0.0.1` too. E.g.: `http://[${address}]:${port}` – Dmitry Feb 08 '21 at 20:48
-5

The reason why its choosing IPV6 address is possibly because some other process is using IPV4 port no 3456. This happens sometimes due to automatic updates where new processes are installed.

Nikhil
  • 1
  • IP, neither IPv4 nor IPv6, has port numbers. Port numbers are transport addresses for _some_ transport protocols (UDP and TCP). Other transport protocols use other or no addressing. IP, of course, has IP addresses. – Ron Maupin May 27 '19 at 20:33