×

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.


Related Topics

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.

Angular 8 universal

Angular 8 universal Angular Universal is a technology that allows server-side rendering for Angular apps. An angular app is a single-page app, and it runs in a client-side browser. Rendering pages in the DOM is...

6 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 with Bootstrap

How to install Bootstrap for Angular? Run the following Command in Command prompt. npm install–save bootstrap@3= > The @3 is essential! After that, when we use a project created with Angular CLI 6+, we will...

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.

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 ngIf Directive

ngIf Directive is the part of structural directives.The NgIf is the most straightforward structural Directive and comfortable to understand. The ngIf Directives is used to add and remove HTML elements according to...

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

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

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.

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 8 Error Fixing

We can get errors in Angular because of many causes. Let's see an example to see some specific types of errors. We have to create an app with the name "testing-app." In this app,...

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.

Property binding in Angular 8

Property binding is the primary way to binding data in Angular.  To bind data to a property of any element, we use square braces[ ]. It is also a one-way data-binding...

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

Difference between Angular and React

Difference between Angular and React Parameters AngularReactType Angular is a complete framework. React is a JavaScript library, and much older compared...

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.

Two-way Data Binding in Angular 8

Two-way Data Binding in Angular 8 Two-way data binding is a synchronization between the model and the view. We can use the ngModel directive in two-way data binding. Custom two-way data binding is useful...

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