Skip to main content
⚡ OpenDevDocs
Express
backendexpressGuideIntermediate

Express.js Middleware Pipeline

Master Express middleware execution flow, custom authentication guards, request modification, and next() mechanics.

Verified: 2026-09-30
Tested on:node 22.x, express 4.21.x
Platforms:
node
#express#middleware#pipeline#auth#architecture

What is Express Middleware?

Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle.

Request ──> [ Middleware 1 ] ──> [ Middleware 2 ] ──> [ Route Handler ] ──> Response

The Middleware Signature

import { Request, Response, NextFunction } from 'express';

export function myMiddleware(req: Request, res: Response, next: NextFunction) {
  // 1. Perform logic or inspect headers
  console.log(`${req.method} ${req.path}`);

  // 2. Pass control to the next middleware in line
  next();

  // Or terminate the request immediately:
  // return res.status(401).json({ error: 'Unauthorized' });
}

4 Categories of Middleware

1. Application-Level Middleware

Bound to an instance of app.use() and runs for all routes:

app.use(express.json());
app.use((req, res, next) => {
  req.requestTime = Date.now();
  next();
});

2. Router-Level Middleware

Bound to an instance of express.Router():

const adminRouter = express.Router();
adminRouter.use(requireAdminAuth);

3. Route-Specific Middleware

Attached directly to individual route definitions:

router.post('/posts', requireAuth, validatePostPayload, createPostHandler);

4. Error-Handling Middleware

Takes 4 arguments (err, req, res, next) and catches any error passed via next(err):

app.use((err: Error, req: Request, res: Response, _next: NextFunction) => {
  res.status(500).json({ error: err.message });
});

Custom Authentication Middleware Example

// src/middleware/auth.ts
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';

export interface AuthenticatedRequest extends Request {
  user?: { id: string; role: string };
}

export function authenticate(
  req: AuthenticatedRequest,
  res: Response,
  next: NextFunction
) {
  const authHeader = req.headers.authorization;

  if (!authHeader?.startsWith('Bearer ')) {
    return res.status(401).json({ error: 'Missing or malformed authorization header' });
  }

  const token = authHeader.split(' ')[1];

  try {
    const payload = jwt.verify(token, process.env.JWT_SECRET!) as { id: string; role: string };
    req.user = payload;
    next();
  } catch {
    return res.status(401).json({ error: 'Invalid or expired token' });
  }
}

On this page