Routes & Decorators

Preview — 3 of 10 questions

You want a class to handle incoming HTTP requests for the path /users. Which decorator turns the class into a controller and registers that path prefix?

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

@Controller('users')
export class UsersController {
  @Get() // resolves to GET /users
  findAll(): string {
    return 'all users';
  }
}
A@Injectable('users')
B@Route('users')
C@Module('users')
D@Controller('users')

Which decorator maps a handler method to an HTTP POST request?

javascript
import { Controller, Post, Body } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Post() // POST /users
  create(@Body() body: unknown): string {
    return 'user created';
  }
}
A@Create()
B@HttpPost()
C@Method('POST')
D@Post()

Given a route GET /users/:id, how do you read the id segment inside the handler?

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

@Controller('users')
export class UsersController {
  @Get(':id') // GET /users/42
  findOne(@Param('id') id: string): string {
    return `user ${id}`;
  }
}
A@Param('id') id: string
B@Body('id') id: string
C@Query('id') id: string
D@Header('id') id: string

Sign up free to play

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