NodeJS: What is default server's port?

Viewed 409

We often see example hello world code for Node that creates an Http Server, starts listening on a port:

const http = require('http');
const server = http.createServer((req, res) => {
    console.log("Hello World!!!");
});
server.listen();   //This will start our node server

So, in above what is the default server's port in which it is started?

2 Answers

If you check the expressjs official documentation of app.listen([port[, host[, backlog]]][, callback]) method here

You see that there comes: If port is omitted or is 0, the operating system will assign an arbitrary unused port, which is useful for cases like automated tasks (tests, etc.).

From the docs:

If port is omitted or is 0, the operating system will assign an arbitrary unused port, which can be retrieved by using server.address().port after the 'listening' event has been emitted.

Related