Skip to content
Preptima
mediumConceptDesignMidSenior

Walk me through the middleware chain of an Express service. What has to come before what, and why?

4 min readUpdated 2026-08-15

Answer

Short answer

Middleware runs in registration order, so ordering is behaviour rather than style: the request id precedes logging, the body parser precedes anything reading the body, authentication precedes authorisation, and the error handler goes last because Express only searches forward from the failure.

Was this answer helpful?

Likely follow-ups

  • You register the error handler before your routes. What happens?
  • An async route handler throws. Does your error middleware see it?
  • Why should the body parser have a size limit, and what happens without one?
  • Where does a response-time header have to be set, given that headers flush with the first write?

Related questions

Further reading