Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 697
Anthony Avatar asked May 24 '12 16:05

Anthony


1 Answers

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.

like image 138
Mustafa Avatar answered Sep 30 '22 00:09

Mustafa