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

When 127.0.0.1 below is omitted app.address() is not null, but when a host is set, it is null.var expre

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.

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.

Share Improve this question edited May 24, 2012 at 16:27 Anthony asked May 24, 2012 at 16:02 AnthonyAnthony 2,4113 gold badges21 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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 puter, since every puter does not have to be "localhost", they can have different domains allow you to only bind sockets to that domain.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745144247a4613575.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信