最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论