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

空扰寡人 提交于 2019-12-03 04:27:41
Alexander Mikhalchenko

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

Best regards, Alexander

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!