var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080);
console.log('Server listening:', `http://${server.address().address}:${server.address().port}`);
I think you can use
server.address()#
Added in: v0.1.90 Returns the bound address, the address family name,
and port of the server as reported by the operating system. Useful to
find which port was assigned when getting an OS-assigned address.
Returns an object with port, family, and address properties: { port:
12346, family: 'IPv4', address: '127.0.0.1' }