<aside> 👉 Controller는 들어오는 요청(request)를 받고 처리된 결과를 응답(response)으로 돌려주는 인터페이스 역할을 한다.
</aside>
Controller의 목적은 응용 프로그램에 대한 특정 요청을 받는 것이다.
Controller는 end-point 라우팅(routing) 매커니즘을 통해 각 Controller가 받을 수 있는 요청을 분류한다.
기본 Controller를 만들기 위해 클래스와 Decorators를 사용한다. → 요청을 해당 컨트롤러에 연결한다.
nest CLI를 이용하여 쉽게 생성할 수 있다.
nest g controller [name]
Controller를 사용 목적에 따라 구분하면 구조적이고 모듈화된 소프트웨어를 작성할 수 있다!
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}
서버가 수행해야하는 많은 귀찮은 작업을 데코레이터로 기술하여, 어플리케이션이 가지는 핵심 로직에 집중할 수 있도록 도와준다.
→ @Controller
데코레이터를 클래스에 선언하면, 해당 클래스는 Controller의 역할을 하게 된다.