0
0
NestJSframework~10 mins

Why controllers handle incoming requests in NestJS - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a controller that handles HTTP GET requests.

NestJS
import { Controller, [1] } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  @[1]()
  findAll() {
    return 'This action returns all cats';
  }
}
Drag options to blanks, or click blank then click option'
APost
BPut
CGet
DDelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Post() instead of @Get() for GET requests
Forgetting to import the decorator
2fill in blank
medium

Complete the code to inject a service into the controller constructor.

NestJS
import { Controller } from '@nestjs/common';
import { CatsService } from './cats.service';

@Controller('cats')
export class CatsController {
  constructor(private readonly [1]: CatsService) {}
}
Drag options to blanks, or click blank then click option'
AcatsService
BCatsService
CserviceCats
Dcats
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name with uppercase as parameter name
Using unrelated variable names
3fill in blank
hard

Fix the error in the controller method to correctly handle a POST request with a body.

NestJS
import { Controller, Post, [1] } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  @Post()
  create(@[1]() createCatDto: any) {
    return 'This action adds a new cat';
  }
}
Drag options to blanks, or click blank then click option'
ABody
BParam
CQuery
DHeaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Param() to get body data
Forgetting to import the decorator
4fill in blank
hard

Fill both blanks to create a controller method that handles GET requests with a route parameter.

NestJS
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}`;
  }
}
Drag options to blanks, or click blank then click option'
AParam
BBody
CQuery
DHeaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Body() or @Query() for route parameters
Not importing Param decorator
5fill in blank
hard

Fill all three blanks to create a controller method that handles PATCH requests with a route parameter and body.

NestJS
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}`;
  }
}
Drag options to blanks, or click blank then click option'
AParam
BBody
CQuery
DHeaders
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up @Param() and @Body() decorators
Not importing both decorators