user1783398 user1783398 - 1 year ago 39
Node.js Question

learning node express, why console does not output 'localhost'?

I am learning to use express and node in general and I realise that I could be missing many fundamental knowledge. Below is the code for starting a server to serve static files, and as i understand the console should output the server address and port number on startup.

var express = require('express');
var app = express();


app.get('/', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );

var server = app.listen(3000, function () {

var host = server.address().address
var port = server.address().port

console.log("Server listening at http://%s:%s", host, port)


I am expecting to see 'Server listening at http://localhost:3000"' but instead i get: 'Server listening at http://:::3000'

Can anyone explain what this means please

Answer Source

See the documentation on net.Server::address().

It probably doesn't say localhost because you didn't specify what address you were listening for.

By default, there is no specific address because it will accept incoming requests on the port regardless of the address it was sent to.

Examples of various addresses it could accept are localhost,, or if you port-forward your server through your network, it could be your global IP address. If you explicitly specify an address, it will reject incoming requests from all these other addresses.