Open In App
Related Articles

How to create custom middleware in express ?

Improve Article
Save Article
Like Article

Express.js is the most powerful framework of the node.js. Express.js is a routing and Middleware framework for handling the different routing of the webpage, and it works between the request and response cycle. Express.js use different kinds of middleware functions in order to complete the different requests made by the client for e.g. client can make get, put, post, and delete requests these requests can easily handle by these middleware functions

Working of the middleware functions:

Custom Middlewares:

We can create multiple Custom middleware using express.js according to the routing of the request and also forward the request to the next middleware.



Parameters: Custom Middleware takes the following two parameters:

  • path: The path or path pattern or in regular expression by which particular Middleware will be called.
  • callback: The second parameter is the callback function that takes three parameters request, response, and next() function as an argument. 

Installing module: Install the express module using the following command.

npm install express

Project structure: Our project structure will look like this.


// Requiring module 
const express = require("express"); 
// Creating express app object 
const app = express();"/check",(req,res,next)=>{
  res.send("This is the post request")
  res.send("This is the get request")
// Server setup 
app.listen(3000, () => { 
  console.log("Server is Running"); 

Run index.js file using below command:

node index.js


Server is Running

Now open the postman tool and send the following requests:

  • Handling Post request: 

  • Handling get request:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials