How a request flows through a NestJS app, from socket to response. Knowing the order tells you where to put each piece of logic.

The pipeline

flowchart TD
    A[Incoming request] --> M[Middlewares]
    M --> EZ
    subgraph EZ [Exception Zone]
        direction TB
        G[Guards] --> BI[Before Interceptor]
        BI --> P[Pipes]
        P --> C[Controllers]
        C --> AI[After Interceptor]
    end
    BI -. same interceptor wraps both .- AI
    AI --> R[Response]
    G -.throws.-> EF[Exception Filters]
    BI -.throws.-> EF
    P -.throws.-> EF
    C -.throws.-> EF
    EF --> R

The order

  1. Incoming request hits the HTTP adapter.
  2. Middleware: global, then module bound.
  3. Guards: global, controller, route.
  4. Interceptors (before): global, controller, route.
  5. Pipes: global, controller, route, then route parameters in reverse order.
  6. Controller handler runs.
  7. Interceptors (after): route, controller, global. FILO order — first interceptor in is the last one out.
  8. If anything threw, Exception filters catch it, resolving from route up to global.
  9. Response is sent.

Why the order matters

Pick the right tool by asking when it should run:

NeedTool
Mutate the raw request, attach correlation IDsMiddleware
Authorization decision before any workGuards
Wrap the handler with logging, caching, retriesInterceptors
Validate or transform inputPipes
Convert a thrown error into an HTTP responseException filters

Source

Adapted from the official NestJS Request Lifecycle FAQ.