abhay tripathi
abhay tripathi

Reputation: 4022

NestJs Pipe vs filter

I was going through the NestJs Docs. And there's this image.

https://docs.nestjs.com/pipes

filter vs pipe diagram nestJs

Filters are more oriented towards Client-Side and pipes are towards Controllers. To me both seems similar.

What are the differences between Pipe and filter with their respective common use-cases ?

Upvotes: 0

Views: 1162

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70191

Pipes are meant to consuming incoming data from the request, be it url or query parameters, or a request body, and do some validations and/or transformations on them to ensure they are the shape your server expects them to be. Nest has some built in utilities like the ValidationPipe to help with this.

Filters (AKA Exception Filters) on the other hand are meant for catching errors that happened during the execution of a request and handling it, sending the error back to the client in a nice format, taking care of sending back the proper error codes, and any other error handling logic you have (like possibly sending to a monitoring service). Nest has a built in ExceptionFilter that manages this nicely, but you can always create your own to handle the logic differently.

Upvotes: 4

Related Questions