Complete the code to define a controller that handles HTTP GET requests.
import { Controller, [1] } from '@nestjs/common'; @Controller('cats') export class CatsController { @[1]() findAll() { return 'This action returns all cats'; } }
The @Get() decorator marks the method to handle HTTP GET requests.
Complete the code to inject a service into the controller constructor.
import { Controller } from '@nestjs/common'; import { CatsService } from './cats.service'; @Controller('cats') export class CatsController { constructor(private readonly [1]: CatsService) {} }
The injected service is usually named in camelCase, matching the class name but starting lowercase.
Fix the error in the controller method to correctly handle a POST request with a body.
import { Controller, Post, [1] } from '@nestjs/common'; @Controller('cats') export class CatsController { @Post() create(@[1]() createCatDto: any) { return 'This action adds a new cat'; } }
The @Body() decorator extracts the body of the POST request.
Fill both blanks to create a controller method that handles GET requests with a route parameter.
import { Controller, Get, [1] } from '@nestjs/common'; @Controller('cats') export class CatsController { @Get(':id') findOne(@[2]('id') id: string) { return `This action returns a cat with id ${id}`; } }
The @Param() decorator is used both in import and method parameter to access route parameters.
Fill all three blanks to create a controller method that handles PATCH requests with a route parameter and body.
import { Controller, Patch, [1], [2] } from '@nestjs/common'; @Controller('cats') export class CatsController { @Patch(':id') update(@[3]('id') id: string, @[2]() updateCatDto: any) { return `This action updates a cat with id ${id}`; } }
The @Param() decorator is used to get the route parameter, and @Body() to get the request body.