×

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:

Express.js app.listen() Function

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.

Express.js app.listen() Function

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:

Express.js app.listen() Function

Related Topics

ngSwitch Directive in Angular 8

Angular 8 ng-Switch Directive The ng-Switch Directive hides and shows the HTML elements depending on an expression. Child elements with the ng-switch-when directive will be displayed if it gets a match; otherwise, the component and...

2 minutes read.

Express.js app.get() Request Function

Express.js Express 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,...

4 minutes read.

Node.js async.queue() Method

Node.js async.queue() Method What is Async module? In Node.js, we have an async module that has a lot of functionality, but the main use case of this module is to do multiple...

3 minutes read.

$timeout service in AngularJS

The discipline of web development is expanding quickly. A technology that is released today will inevitably become obsolete in a few months. The webpages were static in the past and...

4 minutes read.

Creating form in Angular 8

The angular reactive form is used to handle the user's input. We can use Angular form in the application to authorize the user to log in, to update profile, to enter information,...

2 minutes read.

Angular 8 Observables

Observables provide support for passing messages between publisher and subscription in our application. The observables can deliver the multiple values of any type like literal, messages, depending on the content. It is an...

6 minutes read.

What is AOT and JIT Compiler in Angular

A program written in a high-level language is translated into machine code or a low-level language by a piece of software called a compiler. HTML templates are the primary building...

3 minutes read.

Deployment of an Angular 8 app

Deploying an Angular 8 app To implement our application, we have to compile it and then host the JavaScript, CSS, and HTML on a web server. Build an Angular application which is very portable...

4 minutes read.

Angular 8 Components

Angular is used for building mobile and desktop web applications. The component is the basic building block of Angular. It has a selector, template, style, and other properties, and it...

4 minutes read.

Dynamic components in Angular 8

Dynamic components in Angular 8 The dynamic component is one of the versatile and core concept introduced in Angular, Component template is not fixed. An application needs to load new elements...

4 minutes read.

Angular CLI Commands

All CLI commands of Angular Angular CLI is a command-line interface which is used to initialize, develop, and maintain Angular applications. We can use these Command on command prompt or consequentially by an associated...

8 minutes read.

Angular 8 Unit Testing

What is unit testing? Unit testing is a type of software testing where individual components of the software are tested. It is done during the development of any application. A unit may be...

6 minutes read.

Angular 8 Tutorial

What is Angular 8 Angular is a Framework of JavaScript used to build web and mobile applications. Angular 8 is a client-side TypeScript based structure which is used to create dynamic...

6 minutes read.

How to create a Todo List App in Angular 7

What is a Todo List App? Todo List app is an app where users can add various tasks that they want to perform and have an idea of every task. If...

4 minutes read.

Angular 8 NgStyle Directive

Angular 8 NgStyle Directive The ngStyle attribute is used to change and style the multiple properties of Angular. We can change the value, color, and size, etc. of the component. It is a built-in...

1 minute read.

Angular 8 App Loading

How an Angular 8 app loaded and started When we create an Angular app and run it by using ng serve command, it looks like the below screenshot. It is a simple...

3 minutes read.

Angular 8 Architecture

Architecture of Angular 8 Angular 8 is a platform and framework which is used to create clients applications in HTML and Typescript. Angular 8 is written in Typescript. Typescript is a...

5 minutes read.

Creating first APP in Angular 8

Firstly, we have to open Git Bash and, then we have to write the following command in it. ng new my -app The project has been created now so that now we have to go...

2 minutes read.

Angular 8 Directives

Angular 8 Directives: Directives are instructions in the DOM (Document Object Model). It specifies how to place our business logic in Angular. The directive is markers on a DOM element...

3 minutes read.

Advantages | Disadvantages of Angular 8

Advantages of Angular 8 There are some Advantages of angular 8 which are given below: It offers clean code developmentHigher PerformanceAn angular framework can take care of routing, which...

1 minute read.