Node.js server.address().address returns ::

Node.js server.address().address returns ::

如果我没记错的话,几天前它曾经显示"localhost"。我不确定是什么改变了 server.address().address return 双冒号 (::)。 我在这里读到它 return 是一个 IPv6 地址 (::) 如果它可用但它在我的电脑上被禁用。 https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback

正如文档所说,

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.

因此,以下代码将打印 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)
});

但是如果你添加一个明确的主机名:

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

它会打印你想看到的:running at http://127.0.0.1:3456

此外,您可能想使用 some IP lib as pointed in

之所以选择IPV6地址,可能是因为其他进程正在使用IPV4端口号3456。 有时由于安装新进程的自动更新而发生这种情况。