How to Use Express JS Middleware for All Routes

How to Use Express JS Middleware for All Routes

Hi friends, In this tutorial I will discuss how to use express js middleware for all routes with an example. So, let us discuss a little bit about middleware.

Also read, How to install Express js in vs code with an example

What is middleware

  • Middleware is a function in express js that consists of three parameters: request, response, and next.

Syntex:-

const name_of_middleware = (req, resp, next)=>{ //validate the request if needed next(); }

  • The objective of creating middleware is to accept or handle the request and return the response back to the client.

  • It also validates the request and returns the response based on the successful validation and callback to the next() method.

  • The middleware will not complete the request and response cycle until the next() method is called.

Example of express js middleware

Step 1:- First, initialize the packages and modules of the express js framework with the help of require() method as shown below.

const express = require('express');

Step 2:- Now, get all the features and functionalities of the express module and assign it to a variable as shown below.

const data = express();

Step 3:- Now, define the middleware as shown below.

const handleRequest = (req, resp, next)=>{
    console.log("tell me something");
    if(!req.query.verify)
    {
        resp.send("Please add the verify parameter");
    }
    else if(req.query.verify!=="human")
    {
        resp.send("Verify should be human");
    }
    else{
        next();
    }
}

In the above piece of code, you can see that the "verify" parameter is added to the request URL which means whenever you access the localhost URL, the middleware will validate the request based on the verify parameter. If the validation is successful then it will call the next() method.

Step 4:- Now, use the above middleware using the data variable of express JS as shown below.

data.use(handleRequest);

Step 5:- Now, declare a route for http request using GET method using the "data" variable which returns the response from the node JS server as shown below.

data.get('/',(req,resp)=>{
   resp.send('Welcome to home page');
});

Step 6:- Declare the port as shown below.

data.listen(3001);

Complete Code:-

const express = require('express');
const data = express();
const handleRequest = (req, resp, next)=>{
    console.log("tell me something");
    if(!req.query.verify)
    {
        resp.send("Please add the verify parameter");
    }
    else if(req.query.verify!=="human")
    {
        resp.send("Verify should be human");
    }
    else{
        next();
    }
}
data.use(handleRequest);
data.get('/',(req,resp)=>{
   resp.send('Welcome to home page');
});

If you aceess the URL in your browser as localhost:3001 then you will see the output as shown below.

Please add the verify parameter

If you access the URL in your browser as localhost:3001/?verify=asdfasf then you can see the output as shown below.

Verify should be human

If you access your URL in your browser as http://localhost:3001/?verify=human then you can see the output as shown below.

Welcome to home page

Conclusion:- I ho

Did you find this article valuable?

Support Biplab Sinha by becoming a sponsor. Any amount is appreciated!