How to run nodejs or express app with a custom name instead of localhost:3000.

Access your nodejs/express app with custom name without port number

Subscribe to my newsletter and never miss my upcoming articles

Everyone new to nodejs/express is immediately familiar with the conventional format of creating nodejs/express app as shown below.

const express=require("express");
const app=express();
const PORT= process.env.PORT || 3000;
app.listen(PORT,()=>console.log(`server started on ${PORT}`));

However, the app.listen() can take an optional second parameter which is the IP address or the name of the PC the app is running on just immediately after the port, before the call back function. Then specifying port 80 instead of any other port will make the app accessible just with the PC name without the port number.

Below is the full snippet to demonstrate the explanation above.

const express=require("express");
const app=express();
const PORT= 80;
app.listen(PORT,`chinedu`,()=>console.log(`server started on ${PORT}`));

With the above snippet, you can now access the express app on the browser with the url chinedu

This kind of scenario is normally very useful when you want to run your node application within a wired LAN network and make it accessible to any host connect to the LAN.

No Comments Yet