NestJS Controllers

노마드코더 NestJS로 API 만들기 수강 중

Contollers 생성

nest g co movies
  • 자동으로 movies 폴더 하위에 movies.controler.spec.tsmovies.controller.ts 파일 생성
  • app.module.ts 모듈에 컨트롤러가 자동으로 등록
import { MoviesController } from './movies/movies.controller';

@Module({
  imports: [],
  controllers: [MoviesController],
  providers: [],
})

Controllers 구현

Get

@Get('/:id')
getOne(@Param('id') movieId: string) {
  return `This will return one movie with the id: ${movieId}`;
}
  • 파라미터를 받아서 리턴
@Get('search')
search(@Query('year') searchingYear: string) {
  return `we are searching for a movie with a title made after: ${searchingYear}`;
}

입력

http://localhost:3000/movies/search?year=2000

출력

we are searching for a movie with a title made after: 2000
  • @Query로 searchingYear 값을 받아 출력해줌

Post

@Post()
create(@Body() movieData) {
  console.log(movieData);
  return movieData;
}

Delete

@Delete('/:id')
remove(@Param('id') movieId: string) {
  return `This will delete a movie with the id: ${movieId}`;
}

Patch

@Patch('/:id')
patch(@Param('id') movieId: string, @Body() updateData) {
  return {
    updateMovie: movieId,
    ...updateData,
  };
}
  • Put은 모든 리소스를 업데이트하지만
  • Patch는 일부 리소스를 업데이트

Patch 결과

입력

{
	"name": "Tenet",
	"director": "Nolan"
}

출력

{
	"updateMovie": "12",
	"name": "Tenet",
	"director": "Nolan"
}
  • @Body로 json 입력값을 받고 업데이트를 해주어 결과값 리턴

[참조] 노마드코더 NestJS로 API 만들기

끝!