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.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, 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, the request is sent to the server and backend, and then the server responds to the corresponding request.

Routing is the response of a client's request by the last node of 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.

Generally, there are two most popular methods app.get() and app.post() functions. We always use one or more callback functions with these methods.

In these methods, when there is a request made by the client to the correct endpoint of the app, then these callback functions run.

When there are multiple callback functions, then the next() function is used to transfer the control to the next callback function.

We will provide some examples related to the express.get() function:

For installation, we use:

npm install express

Example:

var express = require('express');
var app = express();
var PORT = 3000;


app.get('/', (req, res) => {
res.send("GET Request Called")
})


app.listen(PORT, function(error){
    if (error) console.log("error found ",error);
    console.log("Server is working", PORT);
});

For the above code, firstly, we imported the express app in our file and created our variable app.

Now, we make the call to the app.get() function, and there is a callback function. We choose the port number 3000 to display the callback function. So if we go to the localhost at 3000 port, we will see the output as

“Get Request Called”.

 Output:

Express.js app.get() Request Function

In the path, we can use regular expressions to generalize routes.

We can use the following characters:

?

*

+

()

[]

-

.

For example:

app.get('/pq?rst', (request, response) => {
    response.send('? char is used as regular expression’)
  })

For the above code, the route path will be either ‘/prst’ or ‘pqrst’ because the ‘?’ character will either remove the character just before it or not. So we have two options.

Example:

app.get('/pq+rst', (request, response) => {
    response.send('+ char is used as regular expression')
  })

In the above example, the route path can be ‘prst’ or ‘pqrst’ or ‘pqqrst’ or ‘pqqqrst’ and so on.

Basically, the + character is replaced by 0 or more characters to the character just before it.

Example:

app.get('/pq*rst', (request, response) => {
    response.send('* char is used as regular expression')
  })

In the above example, the route path can be ‘pqrst’ or ‘pqarst’ or ‘pq123rst’ or ‘pqadsffrst’ etc.

Basically ‘*’ character can be replaced by zero or more strings of any length.

So the route path can also be ‘pqJAVATPOINTrst’.

Example:

app.get('/pq(rs)?t', (request, response) => {
    response.send('? and () char is used as regular expression')
  })

In the above example, there are two possibilities of route path, and that is either ‘pqt’ or ‘pqrst’ because ‘()’ is used collectively for many characters.

So either ‘rs’ will be used or not because of the ‘?’ character.

Example:

app.get(/qrs/, (request, response) => {
    response.send('double / is used as regular expression')
  })

In the above example, route paths can be ‘pqrst’ or ‘pqrs’ or ‘qrs’ or ‘JAVAqrsTPOINT’ etc. basically, the string between these two ‘/’ should be there in the path, and then that path will be matched.

app.get(/.*rst$/, (request, response) => {
    response.send('. And * char is used as regular expression')
  })

In the above example, the route path can be ‘pqrst’ pr ‘prst’ or ‘288rst’.Basically, the string which ends with ‘rst’ can be the route path.

Example:

var express = require('express');
var app = express();
var PORT = 3002;


const callBack1 = function (req, res, next) {
    console.log('first callback')
    next()
  }
 
  const callBack2 = function (req, res, next) {
    console.log('second callback')
    next()
  }
 
  const callBack3 = function (req, res) {
    res.send('third callback')
  }
 
  app.get('/', [callBack1, callBack2, callBack3]);


  app.listen(PORT, function(error){
    if (error) console.log("error found ",error);
    console.log("Server is working", PORT);
});

Output:

Express.js app.get() Request Function

In the above example, multiple callback functions are used, and at the end, callback 3 is called, and there is next() used to go to the next callback function.