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 response

Which Tool for Which Job

StageInterfaceKnows the handler?Typical use
MiddlewareNestMiddlewareNoLogging, CORS, helmet, request id
GuardCanActivateYes (ExecutionContext)Authentication, roles, feature flags
InterceptorNestInterceptorYesTiming, response shape, caching, timeouts
PipePipeTransformOnly the argumentValidation, type conversion
Exception filterExceptionFilterYes (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.