Careeroza
Lifecycle Overview
The Journey of One Request
Every request passes through the same stages in a fixed order. Knowing this order tells you where to put each piece of logic.
Incoming request POST /api/orders
|
v
1. Middleware logging, request id, raw body, cookies (Express-level)
|
v
2. Guards is the user logged in? allowed? -> 401 / 403
|
v
3. Interceptors (before) start timer, check cache
|
v
4. Pipes validate and transform @Body/@Param/@Query -> 400
|
v
5. Controller handler -> Service -> Database
|
v
6. Interceptors (after) wrap/map the response, log time
|
v
Response sent
Any exception thrown in steps 2-6 --> 7. Exception filters --> error responseWhich Tool for Which Job
| Stage | Interface | Knows the handler? | Typical use |
|---|---|---|---|
| Middleware | NestMiddleware | No | Logging, CORS, helmet, request id |
| Guard | CanActivate | Yes (ExecutionContext) | Authentication, roles, feature flags |
| Interceptor | NestInterceptor | Yes | Timing, response shape, caching, timeouts |
| Pipe | PipeTransform | Only the argument | Validation, type conversion |
| Exception filter | ExceptionFilter | Yes (ArgumentsHost) | Error response format, error logging |
Note: Guards run after all middleware but before any interceptor or pipe. So an unauthenticated request never reaches validation: it gets 401, not 400.