How to Create Middleware Chain in Express: Simple Guide
In Express, you create a middleware chain by passing multiple
middleware functions in sequence to app.use() or route handlers. Each middleware calls next() to pass control to the next function, forming a chain that processes requests step-by-step.Syntax
Middleware functions in Express have the signature (req, res, next). You chain them by listing multiple middleware functions in app.use() or route handlers. Each middleware must call next() to continue the chain.
- req: The request object.
- res: The response object.
- next: Function to call the next middleware.
javascript
app.use(middleware1, middleware2, middleware3); // or for a route app.get('/path', middleware1, middleware2, (req, res) => { res.send('Done'); });
Example
This example shows three middleware functions chained together. Each logs a message and calls next(). The final handler sends a response. This demonstrates how requests flow through the middleware chain.
javascript
import express from 'express'; const app = express(); // First middleware function mw1(req, res, next) { console.log('Middleware 1'); next(); } // Second middleware function mw2(req, res, next) { console.log('Middleware 2'); next(); } // Third middleware function mw3(req, res, next) { console.log('Middleware 3'); next(); } app.use(mw1, mw2, mw3); app.get('/', (req, res) => { res.send('Hello from final handler'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Output
Server running on http://localhost:3000
Middleware 1
Middleware 2
Middleware 3
Common Pitfalls
Common mistakes include forgetting to call next() in middleware, which stops the chain and leaves requests hanging. Another is sending multiple responses by calling res.send() in more than one middleware. Always call next() unless you end the response.
javascript
import express from 'express'; const app = express(); // Wrong: missing next(), request hangs function badMiddleware(req, res, next) { console.log('This middleware forgets next()'); // next() missing here } // Right: calls next() to continue function goodMiddleware(req, res, next) { console.log('This middleware calls next()'); next(); } app.use(badMiddleware); // This will cause requests to hang app.use(goodMiddleware); app.get('/', (req, res) => { res.send('Hello'); });
Quick Reference
- Middleware signature:
(req, res, next) - Call
next()to pass control to next middleware - Chain middleware by listing them in
app.use()or route handlers - End response with
res.send(),res.json(), or similar - Do not call
next()after sending a response
Key Takeaways
Chain middleware by passing multiple functions to app.use() or route handlers.
Always call next() in middleware to continue the chain unless ending the response.
Middleware functions receive req, res, and next parameters.
Avoid sending multiple responses or forgetting next() to prevent hanging requests.
Middleware chain processes requests step-by-step in the order they are declared.