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
-
1A hack I implemented successfully: var host = server.address().address; host = (host == '::')? 'localhost':host;– ofer.shefferJun 24, 2016 at 10:01
2 Answers
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
-
6IPv6 literals in URLs should be surrounding by square brackets, e.g.
http://[::]:3456
– Steve-oNov 22, 2015 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?– JakeNov 23, 2015 at 0:32
-
@Jake I'm not sure about disabling IPv6m but this also happens in old node versions (in particlular, 0.12) Nov 23, 2015 at 9: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. Sep 26, 2018 at 17:54
-
Square brackets work well with
127.0.0.1
too. E.g.:http://[${address}]:${port}
– DmitryFeb 8, 2021 at 20:48
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.
-
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. May 27, 2019 at 20:33