×

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

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.

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.

Data flow forms in Angular 8

When we are building forms in Angular, it is essential to understand how the framework handles data flowing from the user or programmatic changes. Reactive and template-driven forms follow two different strategies when...

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

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

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

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

Angular 8 Animation

What is Animation? The animation is a process of drawing, designing, making layouts, and preparation of photographic sequences which are integrated into the multimedia and gaming products. A reflection of movement created for displaying...

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

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.

Dependency Injection in Angular 8

Dependency injection (DI), is an essential application design pattern. Angular 8 has its own DI framework, which used in the design of Angular application to increase efficiency and portability. Dependencies are the services that...

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

History and Versions of Angular 8

Introduction to Angular JS Misko created Angular JS and the first version of Angular also name as "Angular 1". He built a framework to handle the downfalls of HTML. The first...

3 minutes read.

Angular 8 File Structure

File Structure in Angular 8: A file structure contains the files for one or more projects. A project is the set of files that comprise an application on a shareable...

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

Installation of Angular 8

Installation Guides to Angular 8 step by Step Angular combines declarative templates, dependency injection and, end to end tooling to solve development challenges. Angular empowers developers to build an application that lives on the...

4 minutes read.

Angular 8 ngClass Directive

Angular 8 ngClass Directive ngClass Directive is a type of attribute directive. Angular provided built-in directive, and it helps in adding or removing CSS classes on an HTML element. The ngClass directive allows...

2 minutes 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.

String Interpolation in Angular 8

String Interpolation in Angular 8 String interpolation is a one-way data-binding technique which is used to output the data from a typescript code to HTML template. It uses the template expression. It uses the...

2 minutes read.