Pipes, guards and interceptors — Microservices · NestJS

The request pipeline you know from HTTP, on a message: a ValidationPipe that refuses a bad payload with an RpcException, a guard that reads its key from the payload, and an interceptor that audits every handler, in the order Nest runs them.

What you will learn

Read the theory for Pipes, guards and interceptors

All Microservices lessons

All NestJS courses

loading types…

What you'll learn

  • Bind pipes, guards and interceptors to message handlers exactly as to routes, and know they run in the same order: guards, interceptors, pipes, handler
  • Validate a payload with ValidationPipe and an exceptionFactory that throws an RpcException, since its default BadRequestException means nothing to a transport
  • Know that pipes only run on decorated parameters: a DTO needs @Payload() to be validated
  • Read a message's data and handler in a guard or interceptor through ExecutionContext.switchToRpc() and getHandler()