Express.js Request Flow: Connect Routers, Middleware, and Error Handling
Express processes a request through an ordered stack of routes and middleware. Each middleware must either end the response or pass control onward. This guide follows the current Express 5 documentation and shows how to organize the pipeline so asynchronous errors reach a handler.
Express processes a request through an ordered stack of routes and middleware. Each middleware must either end the response or pass control onward. This guide follows the current Express 5 documentation and shows how to organize the pipeline so asynchronous errors reach a handler.
Arrange the request pipeline
Express runs registered middleware and route handlers in order. Middleware can read or change the request and response, end the response, or call next() to continue. If it does neither, the request can hang.
A typical API registers body parsing and shared request context first, feature routers next, then a not-found handler and an error handler. Register express.json() before routers that use the parsed body. Use express.Router() to group feature-specific middleware and routes, then mount it under a path such as /api/v1/users.
A request logger that calls next() lets the matching router run. If a router ends the response, later middleware does not run. If it calls next() without responding, Express continues in registration order. Middleware position therefore defines behavior, not just code organization.
Make routes and responses explicit
Register handlers by HTTP method and path. Route parameters and query values are external input; validate their presence and format before business logic. Return an appropriate 4xx response for invalid input and pass unexpected server errors into the shared error path.
Place a 404 handler after all routers so unmatched requests receive an explicit response, such as JSON with status 404. If it is registered too early, valid routes registered later will not be reached.
Forward async errors in Express 5
Express handles exceptions thrown by synchronous route handlers. In Express 5, a route or middleware that returns a Promise automatically passes a rejection or thrown error to next(error). Do not assume earlier major versions behave the same; check the version installed by the project.
If a handler starts async work but does not return its Promise, Express cannot automatically observe its failure. Return the async flow or catch the failure and pass it to next(err). Express then skips ordinary routing and uses error-handling middleware.
Error middleware has four arguments, (err, req, res, next), and belongs after routes. If response headers have already been sent, delegate with next(err) instead of writing another body. Keep stack traces and secrets out of public responses; log diagnostic detail server-side and return a stable message and request ID.
Configure and verify
For an existing Express 5 API:
Run npm ls express to confirm the installed version and Promise error behavior.
Register body parsing and request-wide middleware before routers.
Mount feature routers with express.Router(); ensure every middleware ends the response or calls next().
Put the 404 handler after routers and the four-argument error handler last.
Request a valid route, an unknown route, invalid input, and a route whose async handler rejects. Check status codes and response bodies. These are verification steps, not tests run for this article.
If a request hangs, inspect whether the last middleware ended the response or called next(). If async errors only appear in logs or destabilize the process, check that the Promise is returned, the error reaches next(err), and the error handler follows the routers. Repeat the four checks after changes and restore the prior middleware order if behavior regresses.
References
[Express: Using middleware](https://expressjs.com/en/guide/using-middleware/)
[Express: Routing](https://expressjs.com/en/guide/routing/)
[Express: Error handling](https://expressjs.com/en/guide/error-handling/)
[Express: Writing middleware](https://expressjs.com/en/guide/writing-middleware/)