Server Development / NestJS
Request Lifecycle
The path of a request through middleware, guards, interceptors, pipes, and exception filters.
The NestJS request lifecycle
Between the socket and your route handler, a NestJS request passes through middleware, guards, interceptors, and pipes, and exception filters wait for anything that goes wrong. This overview shows the order, what each stage is for, and how global, controller, and route bindings stack.
Middleware in NestJS
Middleware is the first code a NestJS request meets, and the only stage that runs before Nest knows which handler will answer. This chapter covers class and function middleware, binding it to routes in a module, global middleware, and the jobs that belong in a later stage instead.
Guards in NestJS
A guard answers one question before a NestJS handler runs: may this request continue? This chapter covers the CanActivate contract, reading the request and the handler from the ExecutionContext, steering a guard with metadata, and binding guards to routes or to the whole application.
Interceptors in NestJS
An interceptor wraps a NestJS route handler, so it can act both before the handler runs and on whatever the handler returns. This chapter covers the NestInterceptor contract, the RxJS operators that do most of the work, and the built-in interceptors for serialization and caching.
Pipes and validation in NestJS
Pipes are the last stage before a NestJS handler runs, and they see each argument on its own. This chapter covers the built-in parsing pipes, request validation with ValidationPipe and class-validator, writing a custom pipe, and where pipes should be bound.
Exception filters in NestJS
Exception filters are where a NestJS application decides what an error looks like to the client. This chapter covers the built-in HTTP exceptions, attaching a cause, custom exception classes, writing a filter, and a catch-all filter that gives every error one shape.