Reputation: 903
Following the standard ExpressJs hello word example, i get a host of ' : : '.
Why does this happen?
hello word example:
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
I found a tip that was add 'localhost' after port parameter. It solved when i was looking just my pc, but it wont work over the network. What should i do?
Upvotes: 14
Views: 8041
Reputation: 1693
This will give you the results you are looking for. You should not need to include 'localhost'
var server = app.listen(3000, function () {
var port = server.address().port;
require('dns').lookup(require('os').hostname(), function (err, add, fam) {
debug('Example app listening at http://%s:%s', add, port);
})
});
Upvotes: 2
Reputation: 48346
For the code
var server = app.listen(3000, function () {
without an address parameter in the listen
function, Node will bind it to any address, related to the address 0.0.0.0
of IPV4, and corresponding to ::
in IPV6
. And this IPv6
unspecified address, 0:0:0:0:0:0:0:0
, is reduced to ::
,
After running netstat -a
TCP [::]:3000 CP-Q10001:0 LISTENING
We know the node server listen on address ::
with port 3000
.
Refer to http.listen
which express.js
used in here
app.listen = function listen() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
If the hostname is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise.
Upvotes: 11
Reputation: 943
I tried the example and had the same output for hostname '::', I did the following change as a workaround:
var server = app.listen(3000, 'localhost', function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
output:
Example app listening at http://127.0.0.1:3000
Upvotes: 10