Skip to main content
D
Node.js
Express Middleware

Express Middleware

Express middleware modifies requests before they reach the route.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

Middleware functions sit in the middle, between the user's incoming request and the final route response. Developers use middleware to log visitors, check if a user is logged in, or format data before the main code runs.

Example

Example
1/* this uses middleware */
2const myLogger = function (req, res, next) {
3  console.log('User visited the site');
4  next(); // Pass control to the next function
5}
6
7app.use(myLogger);

Key Points

  • Middleware functions run in order.
  • The `next()` function moves to the next middleware.
  • `app.use()` applies the middleware to all routes.
  • If you forget `next()`, the server gets stuck loading forever.

Up Next

Continue your journey with the next topic.

Go to REST APIs