How to run Node/Express on a specific port?
Last Updated :
05 Aug, 2024
Port in Node is the numerical endpoint that is assigned to the process to allow communication over the network. Each application is identified uniquely by using this port number. There are port numbers that range from 0 to 65535, some of the ports are already assigned to the common services. In this article, we are going to learn how we can run a node on a specific port rather than using the default port. We will run a node on a specific port by using the two approaches.
Prerequisites
We will discuss the following two approaches to run Node/Express on a specific port
Steps to use middleware chaining in Express JS
Step 1: In the first step, we will create the new folder by using the below command in the VScode terminal.
mkdir folder-name
cd folder-name
Step 2: After creating the folder, initialize the NPM using the below command. Using this the package.json file will be created.
npm init -y
Step 3: Now, we will install the express dependency for our project using the below command.
npm i express
Step 4: Now create the below Project Structure of our project which includes the file as app.js.
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.18.2"
}
Approach 1: By Specifying Port in Code
We will specify the port explicitly in the code within the Node.js application. The server is then configured to listen in this predetermined port by setting and communicating the desired port for the application.
Syntax:
const port = 3000; // Specify the desired port
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Example: In the below example, we have specified the predetermined port in the code which is set to 4000. So the node application is running on Port 4000.
JavaScript
// app.js
const http = require('http');
// specifying the desired port
const port = 4000;
const server = http.createServer((req, res) => {
// adding port information to the response
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Hello, GeeksforGeeks! Server is running on port ${port}`);
});
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Output:
Approach 2: Using Environment Variable
We will be using the Environment Variable to dynamically determine the port rather than modifying the actual source code. Using this method, we can properly specify the port in execution time rather than predetermining.
Syntax:
const port = process.env.PORT || 3000;
Example: In the below example, we are running the application on Port number 6000 which is set by using the Environment Variable in terminal.
JavaScript
// app.js
const http = require('http');
// using environment variable PORT or default to 3000
const port = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
// adding port information to the response
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Hello, Geek! Server is running on port ${port}`);
});
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Output:
Similar Reads
How to Install Express in a Node Project? ExpressJS is a popular, lightweight web framework for NodeJS that simplifies the process of building web applications and APIs. It provides a robust set of features for creating server-side applications, including routing, middleware support, and easy integration with databases and other services.Be
2 min read
How to use the app object in Express ? In Node and Express, the app object has an important role as it is used to define routes and middleware, and handling HTTP requests and responses. In this article, we'll explore the various aspects of the `app` object and how it can be effectively used in Express applications. PrerequisitesNode.js a
3 min read
How to Deploy Node.js Express Application on Render ? Deploying a Node.js Express application on Render is straightforward and involves a few key steps to set up your project, configure deployment settings, and manage your application on the Render platform. Render provides an easy-to-use platform for deploying applications, offering features like auto
4 min read
How to skip a middleware in Express.js ? If we want to skip a middleware we can pass parameters to the middleware function and decide based on that parameter which middleware to call and which middleware to not call. Prerequisite: express.js: To handle routing. Setting up environment and execution: Step 1: Initialize node project npm init
1 min read
How to Run Node.js with Express on Mobile Devices ? Node.js is a cross-platform open-source Javascript runtime environment and library for running web applications outside the browser. It is based on Chrome's V8 engine. Node.js is used to create server-side and command-line applications. Express.js is one of the most popular node frameworks. It is a
2 min read