Express.js app.listen() Function
Express or Express.js is a flexible online application framework for Node.js that offers a strong set of functionality for both web and mobile applications. While preserving the familiar and adored Node.js features, Express offers a thin layer of basic web application functionalities. Express is the foundation of many well-known frameworks.
If we analyze the structure of an app, it has a frontend as well as a backend which is based on a server. Whenever a user or client makes some requests to the app, this request is sent to the server at backend, and then the server responds to the corresponding request. Routing is the response of a client's request by the last node of an application.
We use different methods in the Express.js app to respond to the requests made by the client. These methods are based on HTTP protocol, so they are also called HTTP methods.
Binding and listening for connections on the given host and port are done using the app.listen() function. This method is an exact duplicate of http.Server.listen() in Node.
In scenarios like automated processes, the operating system will allocate a random vacant port if the port number is left out or equal to 0.
The app express() returns actually a JavaScript function that is intended to be used as a callback by Node's HTTP servers to process requests. Since the app does not inherit from these, it is simple to deliver HTTP and HTTPS versions of our application using the same program code (it is simply a callback).
Syntax
Generalize syntax of app.listen() function:
app.listen([port_number[, host[, backlog]]][, callback_function])
Parameters Explanation
- We provide port_number when we want our app to listen to the port number.
- The host is the IP address where we want to listen to our app. To mention the host IP, we should have a specified port number, so this is also an optional argument.
- The third is the backlog, where the length of the queue of pending connections is mentioned. Before mentioning the backlog, we should have specified the IP address of the host.
- The fourth argument is a callback which will be run when the app listens on the specified port. We don’t need three previous arguments to mention the callback, and it can be used all alone.
See some examples related to the Express.listen() function:
For installation, we will use
npm install Express
For example:
var express = require('express');
var app = express();
var PORT_NUMBER = 3010;
app.listen(PORT_NUMBER, function(error){
if (error)
console.log("there is definitely error in your function",error);
else
console.log("app.listen() function is using the port number as ", PORT_NUMBER);
})
Output:
In the first line, we imported the express module in our file. We are using the required keyword to import the module in the file but before importing, make sure that the express module is installed on your device properly. Then we initialize our app component by calling Express(). We defined the port number as 3010 because we want to use the app.listen() function on port number 3010 of our local device.
In the app.listen() function we passed two arguments. In the first argument, we passed the port number on which we want to listen and the second function, as described above, is the callback function. In the callback function, we have two possibilities: either our app.listen() function should work fine, or there might be an error.
If the app.listen() function works fine, then we will print the port number on the console, and if there is any error, then we will print the error.
In the above example, we have output as ‘app.listen() function is using the port number as 3010.’
If we again run this file, then we might encounter the error because the port has already been in use, and we again want to use the same port.
Suppose, you didn’t initialize the port number and you give 0 as the port number, then the program will work fine on any random port number which is not used.
For example:
var express = require('express');
var app = express();
// var PORT_NUMBER = 3010;
app.listen(0, function(error){
if (error)
console.log("there is definitely error in your function",error);
else
console.log("app.listen() function is using the port number as random number ");
})
Output: