×

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

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

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 *ngFor Directive

Angular 8ngFor Directive The *ngFor directive is used to repeat to repeat a portion of HTML template once per each item from an iterable list (collection). The ngFor is an Angular...

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

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

Creating form in Angular 8

The angular reactive form is used to handle the user's input. We can use Angular form in the application to authorize the user to log in, to update profile, to enter information,...

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