Way to Create Custom Middleware in express

Last Updated : 26 Sep, 2026

Express.js is the most powerful framework of the node.js. Express.js provides routing and middleware functionality for handling incoming client requests. Middleware functions execute during the request-response cycle and can process requests, modify responses, or pass control to the next middleware or route handler.

frame_4027

Custom Middleware

Custom middleware functions are created by developers to perform specific tasks during the request-response cycle. A custom middleware can process the request and response objects and use next() to pass control to the next middleware or route handler.

Syntax:

app.use(path, (req, res, next) => {
// Middleware logic
next();
});

Parameters: Custom Middleware takes the following two parameters:

  • path: The path or path pattern for which the middleware will be called. It is optional when using app.use().
  • callback: The middleware function that receives the request (req), response (res), and next() function as arguments.

Installation of the Express module

You can install this package by using this command.

npm install express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project structure:

Screenshot-2026-08-03-153443

Example : Create a custom middleware to log incoming requests:

index.js
// Requiring module
const express = require("express");
// Creating express app object
const app = express();
// Custom middleware
app.use((req, res, next) => {
    console.log(`Request received: ${req.method} ${req.url}`);
    next();
});
// Route handler
app.get("/gfg", (req, res) => {
    res.send("This is the GET request");
});
// Server setup
app.listen(3000, () => {
    console.log("Server is Running");
});

Steps to run the program:

Run index.js file using below command:

node index.js

Output:

Server is Running

Now open the postman tool and send the following requests to http://localhost:3000/gfg

Console Output:

Screenshot-2026-08-24-121607

Postman Output:

Screenshot-2026-08-24-121636

Working of Custom Middleware

  • A custom middleware function is created using app.use().
  • The middleware receives req, res, and next as arguments.
  • The middleware performs the required operation.
  • Calling next() passes control to the next middleware or route handler.
  • The route handler processes the request and sends the response.
  • If the middleware sends a response, it should not call next() for normal request processing.

Use Cases of Custom Middleware

  • Logging incoming requests.
  • Validating request data.
  • Implementing authentication and authorization.
  • Modifying request or response objects.
  • Adding custom headers to responses.
  • Performing common processing before route handlers.
Comment

Explore