How come app.address() is null when HOST is set in app.listen(PORT, HOST);

When 127.0.0.1 below is omitted app.address() is not null, but when a host is set, it is 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);

Error output: TypeError: Cannot read property 'port' of null

Node v0.6.5

I installed express version 2.5.9 - its returning that I have installed 2.5.8 - not sure what that is about.


Because, app.address() is inherited from Node's HTTP module. If you look to the documentation there writes:

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

Therefore I assume, when you request a port with an IP, the OS does not report it back to you, so this method returns null. However, when you don't, you may need the IP or address of your computer, since every computer does not have to be "localhost", they can have different domains allow you to only bind sockets to that domain.

链接地址: http://www.djcxy.com/p/59834.html

上一篇: 从标注的数据集中提取正则表达式的技术

下一篇: 当HOST在app.listen(PORT,HOST)中设置时,app.address()如何为空;