How do global, controller and route handlers interact?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn How do global, controller and route handlers interact?
Definition:
Nest can attach request handlers at global, controller and route scopes. Scope determines which requests a handler applies to, while handler type determines execution order. Interceptors wrap inner work and unwind on the response path; exception filters select a matching handler rather than form a normal middleware chain.
Key points:
- Guards and inbound interceptors generally run global → controller → route.
- Outbound interceptors run route → controller → global.
- Filters start at the closest matching scope; the same error does not pass through every filter.
Reference: Nest request lifecycle.
- Keep ordering-dependent behavior explicit and verify it when multiple decorators or global registrations are involved.
Interview point:
- An interceptor cannot catch an earlier guard failure because it has not started wrapping the handler yet.
Adapted from 03_NESTJS.md · “How do global, controller and route handlers interact?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.