Performance: Request body
How the server processes incoming data affects response time and overall user experience.
import { Controller, Post, Body } from '@nestjs/common'; @Controller('users') export class UserController { @Post() createUser(@Body() data: CreateUserDto) { // process validated data } }
import { Controller, Post, Req } from '@nestjs/common'; @Controller('users') export class UserController { @Post() createUser(@Req() request) { const data = JSON.parse(request.body); // process data } }
| Pattern | CPU Usage | Event Loop Blocking | Error Handling | Verdict |
|---|---|---|---|---|
| Manual JSON.parse in controller | High | Blocks event loop | Poor | [X] Bad |
| NestJS @Body with validation pipes | Low | Non-blocking | Good | [OK] Good |
@Post()
create(@Body() data: { name: string }) {
return `Hello, ${data.name}!`;
} @Post()
create(@Body data: { age: number }) {
return `Age is ${data.age}`;
} @Post()
create(???) {
// method body
}Pass the test and this pattern moves unknown