NestJSInterview preparation
Intermediate·Concept·2 min read

Guard vs pipe vs interceptor vs filter?

Practice this question

Definition:

Guards, pipes, interceptors and filters are separate request extension points with different jobs. A guard decides whether work may proceed; a pipe validates or transforms arguments; an interceptor wraps execution; a filter handles uncaught errors. Choosing the correct point avoids mixing authentication, validation and formatting into business methods.

Key points:

  • Guard: “Can this request continue?” Pipe: “Is this input valid, and should it change type?”

  • Interceptor: wraps handler work for timing, response mapping or similar logic.

  • Filter: turns an uncaught exception into an error response.

  • Business rules that must apply outside HTTP belong in the service too, even if a guard or pipe handles request-level checks.

Comparison:

ToolMain questionExample
GuardMay this request proceed?Require an admin role
PipeIs this argument valid?Parse and validate an integer ID
InterceptorWhat wraps handler execution?Measure handler duration
FilterHow should an uncaught error be returned?Map a domain error to HTTP

Interview point:

  • Give one concrete use for each and say when it runs in the lifecycle.
Adapted from 03_NESTJS.md · “Guard vs pipe vs interceptor vs filter?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.