Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk5 min

NestJS Request Flow: Where Setup, Validation, and Errors Belong

NestJS requests move from middleware through guards, interceptors, pipes, and the controller; errors branch to the most local applicable exception filter.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A NestJS request normally passes through middleware, guards, inbound interceptors, pipes, the controller handler, outbound interceptors, and then the response. Uncaught errors branch to an applicable exception filter instead of completing the normal path. The order tells you where to put request setup, authorization, validation, response wrapping, and error handling.

NestJS request lifecycle cheat sheet

Incoming request
  → Middleware
  → Guards: global → controller → route
  → Interceptors, inbound: global → controller → route
  → Pipes: global → controller → route → parameter
  → Controller handler (and service work, if called)
  → Interceptors, response: route → controller → global
  → Response

This is the general path described by the NestJS request lifecycle FAQ, not a guarantee that every application uses every stage. Middleware, guards, and interceptors can affect whether work continues; a controller may or may not call a service.

As an Amazon Associate I earn from qualifying purchases.

What each stage does—and where to use it

Component Runs when Best fit
Middleware Before route handling Request-level setup that does not need the selected handler
Guard After middleware, before interceptors and pipes Route-aware access decisions
Interceptor Wraps handler execution and its result stream Logging, response transformation, caching, or stream-level error handling
Pipe Immediately before handler invocation Validating or transforming handler arguments
Exception filter When an uncaught exception occurs Handling an error and shaping its response

Middleware: request setup before Nest reaches the route

Middleware receives the request, response, and next() function. It can establish request context or attach identity information before route handling, but it does not have the route-aware role of a guard. Nest supports function- and class-based middleware; module-bound middleware is configured with a module’s configure() method and MiddlewareConsumer. See the NestJS middleware guide.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Middleware must either end the response or pass control onward with next(). If it does neither, the request hangs. Middleware runs sequentially in binding order: globally bound middleware runs first, followed by module-bound middleware matched to the path. Nest’s FAQ describes ordering across modules as global modules first, then the root module, then other modules by distance from the root in the import graph.

Middleware errors have a special boundary: because middleware runs before Nest selects a route handler, only global exception filters can handle its exceptions. Middleware signatures and behavior can also differ between the Express and Fastify adapters.

Guards: decide whether the route can proceed

Guards run after all middleware and before any interceptor or pipe. A guard implements CanActivate and can return a boolean, Promise, or Observable. Returning true permits processing; returning false denies it. Because a guard receives ExecutionContext, it can inspect the handler and execution context when making its decision. See the NestJS guards guide.

Guards run in binding order at each scope: global, controller, then route. This makes them the natural place for route-aware authorization, such as checking roles or permissions after authentication has identified the user.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Interceptors: wrap the handler and its result

An interceptor receives an ExecutionContext and a CallHandler. Calling next.handle() yields an RxJS Observable. Code around that stream can log, transform a result, handle an error, or short-circuit the handler—for example, by returning a cached Observable. The NestJS interceptors guide explains the stream-based model.

Interceptors nest: inbound execution enters global, controller, then route interceptors; the result unwinds route, controller, then global. This is why “before” and “after” logs appear in opposite orders. An interceptor can also observe errors from pipes, controllers, or services with catchError. A tap(nextValue) callback does not run when the handler throws; use an error callback or finalize() if observation or cleanup must cover failures too.

Pipes: validate or transform arguments before the method runs

Pipes act on handler arguments immediately before the controller method is invoked. They can validate input, throwing if it is invalid, or transform it—for example, converting a path parameter from a string to an integer. A pipe exception prevents the handler from running and enters Nest’s exceptions layer. See the NestJS pipes guide.

Pipe scopes run global, controller, route, then parameter-level. For multiple handler parameters, Nest’s lifecycle FAQ describes parameter processing from last to first: for parameters declared as body, params, and query, a controller-level pipe processes query, params, then body; route-level pipes follow the same parameter sequence.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Nest’s documented built-in pipes include ValidationPipe, StandardSchemaValidationPipe, ParseIntPipe, ParseFloatPipe, ParseBoolPipe, ParseArrayPipe, ParseUUIDPipe, ParseEnumPipe, DefaultValuePipe, ParseFilePipe, and ParseDatePipe. Applying these at the boundary keeps parsing and validation out of handler logic.

Controller and service: application work after inputs pass

Nest invokes the controller method after guards permit the request and pipes produce acceptable arguments. The method may call a service or provider for application work, but Nest does not automatically insert a service step into every request.

Exception filters: handle uncaught exceptions

Exception filters are not a routine final stage for successful requests. When an uncaught exception occurs, the normal lifecycle is short-circuited and Nest checks the most local applicable filter first: route, then controller, then global. If a route filter handles the exception, it is not passed on to controller or global filters. See the NestJS lifecycle FAQ and exception filters guide.

Middleware is the exception to that route-aware scope sequence: its errors occur before a route is selected, so only global filters apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Walk through a request with every scope bound

  1. Middleware runs first. Globally bound middleware runs before matched module-bound middleware. Each middleware must end the response or call next() to continue.
  2. Guards make the access decision. Nest runs global, controller, then route guards. A denial stops the request before interceptors, pipes, or the handler.
  3. Interceptors enter around the handler. Their inbound leg follows global, controller, route order.
  4. Pipes process inputs. Nest applies global, controller, and route pipes, then parameter-level pipes. A thrown exception prevents handler invocation.
  5. The controller method runs. It may call a service as part of its work.
  6. Interceptors unwind around the result. Response-side processing runs route, controller, then global.
  7. An uncaught exception changes the path. Nest skips the remaining ordinary flow and looks for an applicable filter, starting at route scope. Middleware exceptions can reach only global filters.

Debug lifecycle problems by finding the first stage that stops

  • The controller does not run: inspect guard decisions first, then look for a pipe validation or transformation exception. Both can prevent handler invocation.
  • “Before” and “after” interceptor logs appear reversed: that is the expected nesting behavior; inbound order is global to route, while the response unwinds route to global.
  • A controller-level filter misses a middleware error: middleware runs before route selection, so only global filters apply.
  • A global filter does not run after a route filter handles an error: exception filters do not chain after one has handled the exception.
  • A bad :id is rejected before findOne(): a parameter pipe such as ParseIntPipe runs before the handler and can reject or transform the value there.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.