Angular 8 Tutorial

Angular 8 Introduction History and versions of Angular 8 Architecture of Angular 8 How to install Angular 8 and set-up it. Creating our first Angular 8 app Angular 8 app loading

Difference between Angular And react

Angular vs react

Angular 8 Advantages Disadvantage

Advantage and Disadvantage of Angular 8

Angular 8 file structure

Angular 8 file structure

Angular 8 components

Components of Angular 8

Angular 8 CLI Commands

All CLI commands of Angular

Angular 8 with Bootstrap

How to install bootstrap for Angular 8 Libraries of Angular 8

Angular 8 Routing

Routing in Angular 8

Angular 8 directives

Angular 8 Directives Angular 8 ngIf directive Angular 8 ngFor directive Angular 8 ngSwitch directive Angular 8 ngClass directive Angular 8 ngStyle directive

Angular 8 pipes

Angular 8 Pipes

Angular 8 databinding

Angular 8 Data binding Angular 8 Event binding Angular 8 Property binding Two-way data binding in Angular 8

String Interpolation In Angular 8

Angular 8 String interpolation

Angular 8 forms

Angular 8 Forms Data flow of forms in Angular 8 Creating forms in Angular 8 Testing and validation of forms in Angular 8

Error fixing in Angular 8

Error fixing in Angular 8

Dependency injection and services in Angular 8

Dependency injection services in Angular 8

Angular 8 Animations

Angular 8 Animations

Dynamic components in Angular 8

Dynamic components in Angular 8

Angular 8 Module

Angular 8 Module Deploying an angular 8 app

Introduction of unit testing in angular 8

Unit testing in angular 8

Observables in angular 8

Observables in angular 8

Angular 8 universal

Angular 8 universal

Angular 8 Changes and new features

New features and changes in Angular 8

Conclusion

Angular 8 Conclusion

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