Cơ BảnNestJS iconNestJS

Controller trong NestJS làm gì? Cách định nghĩa routes với decorators?

Controller chịu trách nhiệm nhận HTTP requests và trả về responses. Controller map routes đến handler methods thông qua decorators.

@Controller('users') đặt base route /users. Các HTTP method decorators: @Get(), @Post(), @Patch(), @Put(), @Delete(). Có thể thêm path vào decorator như @Get(':id') để tạo route động.

Parameter decorators để extract data từ request: @Param('id') lấy route param, @Query() lấy query string, @Body() lấy request body, @Headers() lấy headers, @Req() / @Res() để access raw request/response (dùng @Res() sẽ mất một số tính năng NestJS như interceptors).

Xem toàn bộ NestJS cùng filter theo level & chủ đề con.

Mở danh sách NestJS