Express app.listen() Function

Last Updated : 3 Jun, 2026

In Express, app.listen() is the function used to start a web server and make the application ready to accept incoming requests. It binds the Express app to a specific port, and optionally a host, so the server can listen for client connections. In most Express projects, app.listen():

  • Can accept an optional host address
  • Commonly placed at the end of the main server file
  • Required for handling incoming client requests over the network

Syntax

app.listen([port[, host[, backlog]]][, callback])

where,

  • port: This is the number that specifies where your app will listen for incoming requests. It's like an "address" that tells your app where to wait for messages.
  • host (Optional): This is the IP address of the machine where the app should listen. You only need to specify the host if you’ve already provided a port number. In other words, you need to mention the port first, and then you can specify the host.
  • backlog (Optional): This defines the maximum number of requests that can be waiting to be processed at once. It’s useful if your app might get a lot of requests at the same time. You can only set the backlog after you’ve specified both the port and the host.
  • callback (Optional): This is a function that runs once the app has started listening. You can use the callback without setting the port, host, or backlog if you just want to confirm that the app has started.

Return Value: It usually returns an HTTP server object.

How app.listen() Works

The app.listen() method starts your server and makes it wait for incoming requests on a specific port. Once the server is running, it responds to requests from users.

  • Starts the server and listens for requests on a given port.
  • Waits for users to send requests to your app.
  • Sends back a response (like a webpage or data) when a request is received.

Steps to Install the Express module

Step 1: You can install this package by using this command.

npm install express

Step 2: After installing the express module, you can check your express version in the command prompt using the command.

npm version express

Project Structure:

NodeProj
Project Structure

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^5.1.0"
}

Example 1:

JavaScript
const express = require('express');
const app = express();
const PORT = 3000;

app.listen(PORT, function(err){
    if (err) console.log("Error in server setup")
    console.log("Server listening on Port", PORT);
})

Run the index.js file using the below command: 

node index.js

Output:  

Server listening on Port 3000

Example 2:

JavaScript
const express = require('express');
const app = express();

// Listen on port 3000 and host 127.0.0.1
const server = app.listen(3000, '127.0.0.1', () => {
    console.log('Server is running on http://127.0.0.1:3000');
});

console.log(server.address());

Output:

Server is running on http://127.0.0.1:3000
{ address: '127.0.0.1', family: 'IPv4', port: 3000 }

In this code, we start the Express server on port 3000 and host 127.0.0.1, stores the returned HTTP server object in server, and executes a callback function once the server begins listening.

Comment