request
-> middleware (global, then module)
-> guards (global, controller, route)
-> interceptors before (global, controller, route)
-> pipes (global, controller, route, route params)
-> handler -> service
-> interceptors after (route, controller, global)
-> exception filters (route, controller, global) on error
-> response
Nest processes a request in this order:
- Middleware: globally bound middleware first, then module-bound middleware.
- Guards: global, then controller, then route. Any guard can stop the request.
- Interceptors (before part): global, then controller, then route.
- Pipes: global, then controller, then route, and finally parameter-level pipes. Pipes transform and validate the handler's arguments.
- Handler: the route handler runs and calls services.
- Interceptors (after part): the result flows back in reverse order, route to controller to global.
- Response: Nest sends the response.
Errors: if anything throws along the way, exception filters handle it. Nest checks route filters, then controller filters, then global filters. Only the first matching filter runs. Exceptions thrown in guards, pipes or interceptors also reach the filters.
- Middleware runs first, then guards, then interceptors (before part), then pipes.
- Then the handler runs and calls services.
- Interceptors' after-logic runs in reverse order.
- Exception filters are checked route, controller, then global, and only the first match runs.
Read more