Skip to main content

Command Palette

Search for a command to run...

What is Middleware in Express and How It Works

Learn what middleware is in Express.js, how it works in the request lifecycle, and why it is essential for backend development.

Updated
5 min readView as Markdown
What is Middleware in Express and How It Works

Introduction

When a request reaches an Express server, it usually does not go directly to the route handler.

Instead, it passes through multiple functions first.

These functions are called middleware.

Middleware is one of the most important concepts in Express.js because it helps:

  • Process requests

  • Validate data

  • Authenticate users

  • Log information

  • Modify responses

In this article, you'll learn:

  • What middleware is

  • Where middleware sits in the request lifecycle

  • Types of middleware

  • Middleware execution order

  • The role of next()

  • Real-world middleware examples

What is Middleware?

Middleware is a function that runs:

Between the incoming request and the final response.

Think of middleware as a checkpoint in the request-response cycle.

It can:

  • Read request data

  • Modify request or response

  • Stop the request

  • Pass control to the next middleware

Simple Analogy: Request Pipeline

Imagine airport security.

Before passengers board a plane, they go through:

  1. Identity verification

  2. Security checks

  3. Ticket validation

Only after passing these checkpoints can they continue.

Middleware works similarly.

Before reaching the final route handler, requests may pass through:

  • Logging middleware

  • Authentication middleware

  • Validation middleware

Where Middleware Sits in the Request Lifecycle

Basic flow:

Client Request
      ↓
Middleware
      ↓
Route Handler
      ↓
Response

The middleware sits between:

  • The incoming request

  • The final response

Basic Middleware Example

const express = require("express");

const app = express();

app.use((req, res, next) => {
  console.log("Middleware executed");

  next();
});

app.get("/", (req, res) => {
  res.send("Home Page");
});

app.listen(3000);

Understanding the Flow

When a request comes:

  1. Middleware runs first

  2. Console message appears

  3. next() moves to the route handler

  4. Response is sent

The Role of next()

The next() function is extremely important.

It tells Express:

“Move to the next middleware or route handler.”

Example:

next();

Without next():

  • The request gets stuck

  • The response may never be sent

What Happens Without next()

Example:

app.use((req, res, next) => {
  console.log("Middleware running");
});

This middleware never calls:

next()

So the request stops there.

The browser may keep loading forever.

Middleware Execution Order

Middleware runs in the exact order it is defined.

Example:

app.use((req, res, next) => {
  console.log("First");
  next();
});

app.use((req, res, next) => {
  console.log("Second");
  next();
});

app.get("/", (req, res) => {
  res.send("Done");
});

Output:

First
Second

Then the response is sent.

Middleware Execution Sequence

Request
   ↓
Middleware 1
   ↓
Middleware 2
   ↓
Middleware 3
   ↓
Route Handler
   ↓
Response

This chain is often called the:

Middleware Pipeline

Types of Middleware in Express

Express supports different types of middleware.

1. Application-Level Middleware

Applied to the entire application.

Example:

app.use((req, res, next) => {
  console.log("Application middleware");

  next();
});

Runs for every request.

2. Router-Level Middleware

Applied only to specific routes or routers.

Example:

const router = express.Router();

router.use((req, res, next) => {
  console.log("Router middleware");

  next();
});

Only runs inside that router.

3. Built-in Middleware

Express provides built-in middleware.

Example:

JSON Middleware

app.use(express.json());

This middleware:

  • Parses incoming JSON

  • Adds parsed data to req.body

Static File Middleware

app.use(express.static("public"));

Used for:

  • Images

  • CSS

  • JavaScript files

Real-World Middleware Examples

Logging Middleware

Used to log request information.

Example:

app.use((req, res, next) => {
  console.log(req.method, req.url);

  next();
});

Useful for:

  • Debugging

  • Monitoring traffic

Authentication Middleware

Checks if the user is authorized.

Example:

app.use((req, res, next) => {
  const authenticated = true;

  if (authenticated) {
    next();
  } else {
    res.send("Access denied");
  }
});

Used in:

  • Protected routes

  • Login systems

  • APIs

Request Validation Middleware

Validates incoming data.

Example:

app.use((req, res, next) => {
  if (!req.query.name) {
    return res.send("Name is required");
  }

  next();
});

Prevents invalid requests from reaching the main logic.

Middleware Can Modify Requests

Middleware can add custom data to requests.

Example:

app.use((req, res, next) => {
  req.user = {
    name: "John"
  };

  next();
});

Later middleware and routes can access:

req.user

Route-Specific Middleware

Middleware can run only for certain routes.

Example:

const logger = (req, res, next) => {
  console.log("Logging");

  next();
};

app.get("/dashboard", logger, (req, res) => {
  res.send("Dashboard");
});

The logger runs only for /dashboard.

Why Middleware is Powerful

Middleware makes applications:

  • Modular

  • Reusable

  • Organized

  • Easier to maintain

Instead of repeating logic in every route, middleware centralizes common tasks.

Common Beginner Mistakes

Forgetting next()

This causes requests to hang.

Sending Multiple Responses

Wrong:

res.send("Hello");

next();

After sending a response, avoid calling next() unnecessarily.

Wrong Middleware Order

Example:

app.get("/", handler);

app.use(express.json());

The JSON middleware should usually come before routes.

Request → Middleware → Route Flow

Client
  ↓
Request
  ↓
Middleware
  ↓
Route Handler
  ↓
Response

Multiple Middleware Chain

Request
  ↓
Logger Middleware
  ↓
Authentication Middleware
  ↓
Validation Middleware
  ↓
Route Handler
  ↓
Response

Why Middleware is Important in Express

Middleware is the backbone of Express applications.

Almost every major feature depends on middleware:

  • Authentication

  • Security

  • Parsing data

  • File uploads

  • Logging

  • Error handling

Understanding middleware is essential for becoming confident with Express.js backend development.

Conclusion

Middleware in Express acts like checkpoints in the request-response lifecycle.

It allows developers to:

  • Process requests

  • Validate data

  • Authenticate users

  • Reuse logic

  • Organize applications cleanly

The key thing to remember is:

Middleware runs in sequence and uses next() to continue the pipeline.

Once you understand middleware, Express.js becomes much easier to understand and scale.