Open In App

Express app.listen() Function

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Express.js, the app.listen() function is used to start a server and make it listen for incoming requests on a specified port and host. This method is essential for running an Express application as it enables the server to handle HTTP requests from clients (like browsers).

Syntax: 

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

Parameters:

  • 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.

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: Below is the code of app.listen() Function implementation.

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);
})

Steps to run the program: 

Run the index.js file using the below command: 

node index.js

Output:  

Server listening on Port 3000

So this is how you can use the Express app.listen() function which Binds and listens for connections on the specified host and port.

Conclusion

The app.listen() function in Express.js is a crucial method for initializing a server and allowing it to listen for incoming requests on a specified port (and optionally on a specified host). It serves as the entry point for running an Express application, making it possible to handle HTTP requests from clients like browsers or other services.




Next Article

Similar Reads