experimentalDecorators 와 emitDecoratorMetadata가 Nest가 실제로 동작하게 만들어주는 핵심중의 핵심
모듈 & 컨트롤러
간단한 Nest 애플리케이션을 만들기 위해서는 최소한 모듈과 컨트롤러가 필요함.
일반적으로는 모듈과 컨트롤러를 별도의 파일에서 만듬.
컨트롤러
import { Controller, Module, Get } from "@nestjs/common";
@Controller() // 데코레이터 : Nest에게 컨트롤러 역할을 할 클래스를 생성하려 한다고 알림
class AppController {
@Get() // Get 데코레이터
getRootRoute() {
return 'hi there!'
}
}
모듈
모듈은 컨트롤러를 감싸게 됨.
모든 애플리케이션에는 반드시 모듈이 하나 있어야 함.
@Module({
controllers : [AppController]
})
class AppModule {
}
Nest는 실행될 때마다, AppModule을 확인하고 나열된 모든 컨트롤러를 검색후 인스턴스를 자동으로 생성하고, 데코레이터를 확인하며 라우트 핸들러를 설정한다.
// 대부분 @nestjs/common에서 import함
import { Controller, Module, Get } from "@nestjs/common";
import {NestFactory} from "@nestjs/core";
@Controller()
class AppController {
@Get()
getRootRoute() {
return 'hi there!'
}
}
@Module({
controllers : [AppController]
})
class AppModule {
}
async function bootstrap() {
const app = await NestFactory.create(AppModule)
await app.listen(3000); // Port : 3000
}
bootstrap()