Express app.listen() Function
Last Updated :
14 Apr, 2025
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:
Project StructureThe 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.
Similar Reads
Express app.use() Function The app.use() function in Express.js adds middleware to the application's request-processing pipeline. It applies the specified middleware to all incoming requests or to specific routes, allowing you to modify request/response objects, perform operations, or handle errors throughout the application.
3 min read
Express app.post() Function The app.post() function in Express.js handles HTTP POST requests to a specific route. It defines a callback function to process incoming data sent via POST, typically used for submitting forms or sending data to a server from clients.Syntax:app.post(path, callback [, callback ...])Arguments:Path: Th
2 min read
Express.js | app.route() Function The app.route() function returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware. Use app.route() to avoid duplicate route names (and thus typo errors). Syntax:app.route( path )Installation of the express module:You can visit the link to Install th
2 min read
Express.js | app.set() Function The app.set() function is used to assign the setting name to value. You may store any value that you want, but certain names can be used to configure the behavior of the server. Syntax:app.set(name, value)Installation of the express module:You can visit the link to Install the express module. You ca
2 min read
Express.js | app.put() Function The app.put() function routes the HTTP PUT requests to the specified path with the specified callback functions. Syntax:app.put(path, callback [, callback ...])Arguments:Path: The path for which the middleware function is invoked and can be any of the:A string represents a path.A path pattern.A regu
2 min read
Express.js | app.all() Function The app.all() function is used to route all types of HTTP requests. Like if we have POST, GET, PUT, DELETE, etc, requests made to any specific route, let's say /user, so instead of defining different APIs like app.post('/user'), app.get('/user'), etc, we can define single API app.all('/user') which
2 min read
Express.js res.append() Function The res.append() function appends the specified value to the HTTP response header field and if the header is not already set then it creates the header with the specified value. Syntax: res.append(field [, value])Parameter: The field parameter describes the name of the field that need to be appended
2 min read
Express.js | app.get() Function The app.get() function returns the value name app setting. The app.set() function is used to assign the setting name to value. This function is used to get the values that are assigned. Syntax: app.get(name) Installation of the express module: You can visit the link to Install the express module. Y
1 min read
Express.js | app.METHOD() Function The app.METHOD() function is used to route an HTTP request, where METHOD is the HTTP method of the request, such as GET, PUT, POST, and so on, in lowercase. Thus, the actual methods are app.get(), app.post(), app.put(), and so on. Syntax: app.METHOD(path, callback [, callback ...]) Parameters: Path
2 min read
Express.js | app.param() Function The app.param() function is used to add the callback triggers to route parameters. It is commonly used to check for the existence of the data requested related to the route parameter. Syntax: app.param([name], callback) Parameters: name: It is the name of the parameter or an array of them.callback:
2 min read