Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to extract the request body in a NestJS controller method.
NestJS
import { Controller, Post, [1] } from '@nestjs/common'; @Controller('users') export class UsersController { @Post() createUser(@[1]() body: any) { return body; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Param() instead of @Body() to get the request body.
Forgetting to import the correct decorator.
Using @Query() which extracts query parameters, not the body.
✗ Incorrect
The @Body() decorator extracts the request body in a NestJS controller method.
2fill in blank
mediumComplete the code to define a DTO class and use it to type the request body in a NestJS controller.
NestJS
import { Controller, Post, Body } from '@nestjs/common'; class CreateUserDto { name: string; age: number; } @Controller('users') export class UsersController { @Post() createUser(@Body() body: [1]) { return body; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' defeats the purpose of typing the request body.
Using a class name that is not defined.
Using 'Object' which is less specific than the DTO.
✗ Incorrect
Using a DTO class like CreateUserDto helps type the request body for better validation and clarity.
3fill in blank
hardFix the error in the code to correctly extract the request body in a NestJS controller method.
NestJS
import { Controller, Post, Body } from '@nestjs/common'; @Controller('products') export class ProductsController { @Post() addProduct(@[1] body: any) { return body; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Body without parentheses.
Using other decorators like @Param or @Query incorrectly.
Forgetting to import the decorator.
✗ Incorrect
The @Body() decorator is required to extract the request body; missing parentheses cause errors.
4fill in blank
hardFill both blanks to create a POST route that extracts and types the request body using a DTO in NestJS.
NestJS
import { Controller, Post, [1] } from '@nestjs/common'; class UpdateUserDto { email: string; password: string; } @Controller('users') export class UsersController { @Post('update') updateUser(@[2]() body: UpdateUserDto) { return body; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong decorator.
Using different decorators for import and parameter.
Forgetting parentheses in the parameter decorator.
✗ Incorrect
The @Body decorator is imported and used to extract the typed request body in the controller method.
5fill in blank
hardFill all three blanks to create a NestJS controller method that extracts a typed request body and returns a confirmation message.
NestJS
import { Controller, Post, [1] } from '@nestjs/common'; class CreateOrderDto { productId: number; quantity: number; } @Controller('orders') export class OrdersController { @Post() createOrder(@[2]() order: [3]) { return `Order for product ${order.productId} with quantity ${order.quantity} received.`; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong DTO class name.
Using @Param instead of @Body.
Not importing the @Body decorator.
✗ Incorrect
Import and use @Body decorator and type the parameter with the correct DTO class CreateOrderDto.