NestJS NestJS 3 — Questions and Answers
Question 1: What is the role of a NestJS Interceptor?
- To validate incoming request data
- To bind extra logic before/after a method execution and transform results (Correct answer)
- To route requests to the correct controller
- To manage database connections
Correct answer: To bind extra logic before/after a method execution and transform results
Interceptors implement the NestInterceptor interface and can add extra logic around method calls, transform results, and extend basic function behavior.
Question 2: Which RxJS operator is commonly used in NestJS interceptors to transform the response?
- switchMap
- mergeMap
- map (Correct answer)
- filter
Correct answer: map
The map operator from RxJS is commonly used in interceptors to transform the response stream returned by the route handler.
Question 3: What does a NestJS Pipe primarily do?
- Connects services to controllers
- Transforms and validates input data before it reaches the route handler (Correct answer)
- Filters out unauthorized requests
- Manages the application lifecycle
Correct answer: Transforms and validates input data before it reaches the route handler
Pipes implement the PipeTransform interface and are used to transform and/or validate data before it reaches the route handler.
Question 4: Which built-in NestJS pipe validates and strips non-whitelisted properties from a DTO?
- ParseIntPipe
- ValidationPipe (Correct answer)
- DefaultValuePipe
- ParseEnumPipe
Correct answer: ValidationPipe
ValidationPipe with whitelist:true strips properties not defined in the DTO class, and forbidNonWhitelisted:true throws an error for unexpected properties.
Question 5: Which decorator binds a pipe to a specific route parameter?
- @Param() with pipe config
- @UsePipes()
- @Body() with a pipe argument
- Both @UsePipes() and passing the pipe directly to @Param()/@Body() (Correct answer)
Correct answer: Both @UsePipes() and passing the pipe directly to @Param()/@Body()
NestJS allows binding pipes at the method level with @UsePipes() or at the parameter level by passing the pipe as an argument to @Param(), @Body(), etc.
Question 6: What class must be extended or implemented to create a custom Exception Filter in NestJS?
- BaseExceptionFilter or ExceptionFilter (Correct answer)
- HttpException
- ErrorHandler
- NestFilter
Correct answer: BaseExceptionFilter or ExceptionFilter
Custom exception filters implement the ExceptionFilter interface or extend BaseExceptionFilter, and use the @Catch() decorator to specify which exceptions to handle.
Question 7: In NestJS, how do you create a global Exception Filter?
- Use @Catch() on the AppModule
- Call app.useGlobalFilters(new MyFilter()) in main.ts
- Add the filter to the providers array with APP_FILTER token
- Both B and C are valid approaches (Correct answer)
Correct answer: Both B and C are valid approaches
Global filters can be registered via app.useGlobalFilters() in main.ts, or by providing them with the APP_FILTER token inside a module for dependency injection support.
What is the role of a NestJS Interceptor?