gpt4 book ai didi

javascript - 当在 app.listen(PORT, HOST) 中设置 HOST 时,为什么 app.address() 为空?

转载 作者:数据小太阳 更新时间:2023-10-29 04:44:43 30 4
gpt4 key购买 nike

下面127.0.0.1省略时app.address()不为null,但设置了host时为null。

var express = require('express'),
app = express.createServer();

app.use(express.logger());

app.get('/', function(req, res){
res.send('Hello World');
});

app.listen(3000, '127.0.0.1');
console.log(app.address());
console.log('Express server started on port %s', app.address().port);

错误输出:TypeError: Cannot read property 'port' of null

Node v0.6.5

我安装了 express 版本 2.5.9 - 它返回我已经安装了 2.5.8 - 不确定那是什么。

最佳答案

因为,app.address() 继承自 Node 的 HTTP 模块。如果您查看 documentation那里写道:

Returns the bound address and port of the server as reported by the operating system.

因此我假设,当您使用 IP 请求端口时,操作系统不会将其报告给您,因此此方法返回 null。但是,如果不需要,您可能需要计算机的 IP 或地址,因为每台计算机不必是“本地主机”,它们可以有不同的域,允许您只将套接字绑定(bind)到该域。

关于javascript - 当在 app.listen(PORT, HOST) 中设置 HOST 时,为什么 app.address() 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10741157/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com