Sometimes, we want to fix Node.js Express.js app only working on port 3000 with JavaScript.
In this article, we’ll look at how to fix Node.js Express.js app only working on port 3000 with JavaScript.
How to fix Node.js Express.js app only working on port 3000 with JavaScript?
To fix Node.js Express.js app only working on port 3000 with JavaScript, we can set the port with the process.env.PORT
property.
For instance, we write
app.set("port", process.env.PORT || 3000);
to set the port of the Express app with app.set
.
We try to get the port number from the PORT
environment variable, which is process.env.PORT
.
Otherwise, we use port 3000 as the fallback.
Then we start our Node Express app by running
PORT=8080 node app.js
to set the PORT
environment variable to 8080 and run app.js
which has the app.set
code.
Conclusion
To fix Node.js Express.js app only working on port 3000 with JavaScript, we can set the port with the process.env.PORT
property.