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 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 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 “verify” parameter is added to the request URL that means whenever you access the localhost url, the middleware will validate the request based on the verify parameter. If the validation is successfull 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 https://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 https://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 https://localhost:3001/?verify=human then you can see the output as shown below.

Welcome to home page

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below. Download VS Code from Here


Leave a Comment

Mahjong Ways 2

CERI188

CERI188

sv388

CERI188

KUPU178 TOTO SLOT

CERI188

CERI188 TOTO Singapore

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

sabung ayam online

THOR311

parlay

RATU311

KUPU178

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 DEMO SLOT

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

KUPU178

RATU311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

parlay

HOKI311

THOR311

Thor311 Aplikasi

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311