0
0
ExpressHow-ToBeginner · 3 min read

How to Use Multiple Middleware in Express: Simple Guide

In Express, you can use multiple middleware by listing them as arguments in app.use() or route handlers. Each middleware runs in order, and you call next() to pass control to the next one.
📐

Syntax

You can add multiple middleware functions by passing them as separate arguments to app.use() or route methods like app.get(). Each middleware receives req, res, and next parameters. Calling next() moves to the next middleware.

  • app.use(middleware1, middleware2, ...) applies middleware globally.
  • app.get(path, middleware1, middleware2, handler) applies middleware only for that route.
javascript
app.use(middleware1, middleware2, middleware3);

app.get('/path', middleware1, middleware2, (req, res) => {
  res.send('Done');
});
💻

Example

This example shows three middleware functions running in order for the / route. Each logs a message and calls next(). The final handler sends a response.

javascript
import express from 'express';
const app = express();

// Middleware 1: logs request method
function mw1(req, res, next) {
  console.log('Middleware 1:', req.method);
  next();
}

// Middleware 2: logs request URL
function mw2(req, res, next) {
  console.log('Middleware 2:', req.url);
  next();
}

// Middleware 3: adds a custom property
function mw3(req, res, next) {
  req.customData = 'Hello from mw3';
  next();
}

app.get('/', mw1, mw2, mw3, (req, res) => {
  res.send(req.customData);
});

app.listen(3000, () => console.log('Server running on http://localhost:3000'));
Output
Middleware 1: GET Middleware 2: / Server running on http://localhost:3000
⚠️

Common Pitfalls

Common mistakes include forgetting to call next() in middleware, which stops the request from continuing. Another is placing middleware in the wrong order, causing unexpected behavior. Also, not handling errors properly can cause the app to hang.

javascript
import express from 'express';
const app = express();

// Wrong: missing next(), request will hang
function badMiddleware(req, res, next) {
  console.log('This middleware does not call next()');
  // next() is missing here
}

// Correct: calls next() to continue
function goodMiddleware(req, res, next) {
  console.log('This middleware calls next()');
  next();
}

app.get('/', badMiddleware, (req, res) => {
  res.send('You will never see this');
});

app.get('/fixed', goodMiddleware, (req, res) => {
  res.send('This works fine');
});
📊

Quick Reference

  • Use multiple middleware by listing them as arguments.
  • Always call next() unless sending a response.
  • Order matters: middleware runs in the order you list them.
  • Global middleware with app.use(), route-specific with app.get(), app.post(), etc.

Key Takeaways

List multiple middleware functions as arguments to run them in order.
Always call next() in middleware to continue the request flow.
Middleware order affects how requests are processed.
Use app.use() for global middleware and route methods for specific routes.
Missing next() causes requests to hang and never reach the handler.