Forgetting to call next() or send a response. The request then hangs. Every middleware must call next() or send a response. Also, putting business logic in middleware makes it hard to test and reuse.
How to Write Custom Middleware in Express
What is a common mistake when writing Express middleware?
The answer
More FAQs in How to Write Custom Middleware in Express
Use the (req, res, next) signature. Do something with req or res, then call next() to continue or send a response to end the cycle. Keep middlewares small and one-job.
Write middleware for cross-cutting concerns (auth, validation, logging, request IDs, permissions). Put business logic in handlers or services. Middleware shapes the request; logic acts on it.
Use a factory function. The factory takes options and returns a middleware. Example: validate(schema) returns a middleware that validates against that schema. requireRole(role) returns a middleware that checks that role.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.