×

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

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.

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.

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

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.

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.

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.

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

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.

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.

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.

Scalability and Validation Forms in Angular 8

Form Validation Validation is an essential part of managing any set of forms. If we are checking for required fields or querying an external API for a username. Angular 8 provides a set of...

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

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.

Angular 8 Module

Angular 8 Module It is a collection of services, directives, controllers, filters, and configuration information. angular.module is used to configure the $injector. The module is a container of the different parts of an application....

6 minutes read.

Event Binding in Angular 8

Event binding in Angular 8 Event binding is a data-binding from an element to a component. User actions such as keystrokes, mouse movement, clicks, and touches can be bound to the component property...

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

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.