Pipes, Guards & Interceptors

Preview — 3 of 10 questions

In NestJS, what is the primary purpose of a pipe?

javascript
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number): string {
    return `user ${id}`; // id is already a number
  }
}
ATo authorize requests by returning true or false
BTo catch and format exceptions
CTo transform or validate input data before it reaches the handler
DTo log requests and responses

Which built-in pipe converts a string route parameter into a number, throwing if it isn't numeric?

javascript
import { Controller, Get, Query, ParseIntPipe } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  list(@Query('page', ParseIntPipe) page: number): string {
    return `page ${page}`; // "3" -> 3
  }
}
ADefaultValuePipe
BParseBoolPipe
CValidationPipe
DParseIntPipe

A query param limit is optional, and you want it to default to 20 and still be parsed as an integer. Which pipe ordering works?

javascript
import { Controller, Get, Query, DefaultValuePipe, ParseIntPipe } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  list(@Query('limit', new DefaultValuePipe(20), ParseIntPipe) limit: number): string {
    return `limit ${limit}`;
  }
}
A@Query('limit', ParseIntPipe, new DefaultValuePipe(20))
B@Query('limit', ParseIntPipe) only
C@Query('limit', new DefaultValuePipe(20)) only
D@Query('limit', new DefaultValuePipe(20), ParseIntPipe)

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.