Routes & Decorators — Series 3

Preview — 3 of 10 questions

What distinguishes the two?

javascript
@Put(':id')   replace(@Param('id') id: string, @Body() dto: ReplaceCatDto) {}
@Patch(':id') update(@Param('id') id: string, @Body() dto: UpdateCatDto) {}
A@Put creates a resource and @Patch updates one
B@Put accepts a body while @Patch does not
CPUT replaces the resource with the representation sent, so the body is expected to be complete; PATCH applies a partial modification, so the body carries only the fields to change
DThey are interchangeable; Nest treats both identically

Why does dto arrive empty?

javascript
@Get('search')
search(@Body() dto: SearchDto) {}
ABecause @Body() only works alongside @Param()
BBecause GET requests conventionally carry no body — browsers, fetch and most HTTP clients will not send one, and intermediaries may drop it — so there is nothing for the body parser to produce; search criteria belong in the query string
CBecause Nest strips bodies from GET requests for security reasons
DBecause SearchDto needs @IsOptional() on every field

What does GET /cats/featured return?

javascript
@Controller('cats')
export class CatsController {
  @Get('featured')
  featured(@Param('id') id: string) { return typeof id; }
}
A'undefined' — the route declares no :id segment, so there is nothing to extract and the parameter is simply undefined
B'string', with id set to 'featured'
CA 400, because a declared parameter is missing from the request
DA 500, since parameter extraction fails

Sign up free to play

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