Express Middleware Cheat Sheet

Request flows, routers, and error handlers

Last Updated: November 21, 2025

Middleware Order

Type Placement
Logger Early
Auth Before secured routes
Router Mount after globals
Error handler Last

Commands

app.use(express.json())
Parse JSON
app.use('/api', apiRouter)
Mount router
app.use((err, req, res, next) => {})
Catch errors

Advice

Keep middleware single responsibility, avoid async leaks, and call `next(err)` for errors.

💡 Pro Tip: Place error handlers last and target middleware to specific routes when possible.
← Back to Web Frameworks | Browse all categories | View all cheat sheets