Controller
2022. 9. 4. 16:37ㆍNest.js
Nest의 Controller는 들어오는 요청 을 처리 하고 클라이언트에 응답 을 반환 하는 역할.
기본 컨트롤러를 만들기 위해 클래스와 데코레이터 를 사용
쓰다보니까 점점 눈에 익는다..
import { Controller, Get, Query, Post, Body, Put, Param, Delete } from '@nestjs/common';
import { CreateCatDto, UpdateCatDto, ListAllEntities } from './dto';
@Controller('cats') //localhost:3000/cats
export class CatsController {
@Post()
create(@Body() createCatDto: CreateCatDto) {
return 'This action adds a new cat';
}
@Get()
findAll(@Query() query: ListAllEntities) {
return `This action returns all cats (limit: ${query.limit} items)`;
}
@Get(':id') //localhost:3000/cats/:id
findOne(@Param('id') id: string) {
return `This action returns a #${id} cat`;
}
@Put(':id')
update(@Param('id') id: string, @Body() updateCatDto: UpdateCatDto) {
return `This action updates a #${id} cat`;
}
@Delete(':id')
remove(@Param('id') id: string) {
return `This action removes a #${id} cat`;
}
}
각각의 엔드포인트도 express에 비해 훨씬 더 직관적이다.
express처럼 req.body, req.query 해줄 필요 없이, 기본적으로 Body, Query, Param 을 사용할 수 있다.
Body의 경우는 보통 Dto를 통해 유효성 검사 및 타입을 체크한다.
'Nest.js' 카테고리의 다른 글
Module / 캡슐화 (0) | 2022.09.04 |
---|---|
Providers/ DI (0) | 2022.09.04 |
DTO & 유효성 검사 (ValidationPipe) (0) | 2022.08.27 |
Nest.js 기본 (0) | 2022.08.26 |
TypeORM (0) | 2022.08.23 |