내일배움 정리/JS 문법 공부
NestJs 컨트롤러 - req 데이터 사용
GREEN나무
2025. 2. 13. 14:50
728x90
컨트롤러
int타입의 id 사용하기
import { Controller, Get, Param, BadRequestException } from '@nestjs/common';
@Get('/:achievementCId')
findOne(@Param('achievementCId') achievementCId: string) {
const id = Number(achievementCId);
if (!id) {
throw new BadRequestException('achievementCId 값이 없거나 형태가 맞지 않습니다');
}
return this.achievementCService.findOne(+id);
}
쿼리 사용하기
import { Controller, Get, Param, Query } from '@nestjs/common';
@Get()
findByCategory(@Query('category') category?: string) {
if (category) {
return this.achievementService.findCategory(category);
}
return this.achievementService.findAll();
}