Facebook Pixel

Common Express Middleware Mistakes (and How to Fix Them)

Middleware has predictable failure modes. Here are the common mistakes and fixes.

Common Express Middleware Mistakes

Middleware is powerful but easy to get wrong. Here are the common mistakes and how to fix them.

Mistake 1: Forgetting next()

A middleware does work but never calls next() or sends a response. The request hangs. Fix: every middleware must call next() or send a response.

Mistake 2: Calling next() After Sending a Response

A middleware sends res.json() and then calls next(). Headers are already sent; downstream code may try to respond again. Fix: do not call next() after sending a response. Use return res.json(...) to be safe.

Mistake 3: Middleware in the Wrong Order

You add routes before express.json. req.body is undefined. Fix: add global middlewares (json, cors, helmet) before routes. Add error handler last.

Mistake 4: Sync Heavy Work in Middleware

You parse a large file synchronously in a middleware. The event loop blocks for all requests. Fix: keep middlewares light. Do heavy work in handlers with async APIs, or offload to worker threads.

Mistake 5: Repeating Middleware Per Route

You add auth to every route manually. Easy to forget one. Fix: use router.use(auth) for groups of routes that share middleware.

Mistake 6: Middleware That Throws Sync

A middleware throws synchronously. Express 4 catches sync throws, but if it is in a try/catch you wrote, you might swallow it. Fix: let Express catch sync throws, or use next(err) explicitly.

Mistake 7: Async Middleware Without Wrapper

An async middleware rejects a promise. Express 4 does not catch it. Fix: wrap with Promise.resolve(fn(req, res, next)).catch(next).

Mistake 8: Modifying req.body In Place Without Validating

A middleware mutates req.body. Downstream code assumes the original shape. Fix: validate first, then mutate. Or replace req.body with a validated object.

Mistake 9: Error Handler in the Middle

You put the error handler before some routes. Errors from those routes are not caught. Fix: error handler is always last.

Mistake 10: No Error Handler at All

You forget the error handler. Errors crash the server or leak stack traces. Fix: always register a four-parameter error handler at the end of app.js.

The Takeaway

Common Express middleware mistakes: forgetting next(), calling next() after responding, wrong order, sync heavy work, repeating middleware per route, sync throws in try/catch, async middleware without wrapper, mutating req.body without validating, error handler in the middle, and no error handler at all.

Forgetting next(), calling next() after responding, wrong order, sync heavy work, repeating middleware per route, sync throws in try/catch, async middleware without wrapper, mutating req.body without validating, error handler in the middle, and no error handler at all.

A middleware forgot to call next() or send a response. Every middleware must do one or the other. Without it, the request never ends and the client eventually times out.

No. Once you send a response, the cycle is over. Calling next() can lead to downstream code trying to respond again, causing 'headers already sent' errors. Use return res.json(...) to be safe.

Express 4 does not catch promise rejections from async middlewares. Wrap with Promise.resolve(fn(req, res, next)).catch(next) so rejections go to the error handler. Express 5 catches them automatically.

Always last, after all routes and other middlewares. Express runs middlewares in order, so an error handler in the middle would miss errors from routes declared after it.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.