Node.js server.address()。address returns :: [英] Node.js server.address().address returns ::

查看:582
本文介绍了Node.js server.address()。address returns ::的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我记得正确,它用来在前几天显示localhost。我不知道有什么改变,使server.address()。地址返回双冒号(:)。
我在这里读到它返回一个IPv6地址(:),如果它可用,但它在我的电脑上被禁用。
https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback

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

推荐答案

正如文档所说,


开始接受连接在指定的端口和主机名上。如果省略主机名,则当IPv6可用时,服务器将接受任何IPv6地址(:)的连接,否则将接收任何IPv4地址(0.0.0.0)。端口值为零将分配一个随机端口。

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.

所以,以下代码将打印在http :// ::: 3456

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 () {

它将打印您想要看到的内容:运行于http://127.0.0.1:3456

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

您还可以使用某些IP lib ,如这个答案

最好的问候,亚历山大

这篇关于Node.js server.address()。address returns ::的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆