원문을 읽으며 정리합니다.
Table of Contents
프로젝트 생성
먼저 NestJS에서 제공하는 가이드를 따라 프로젝트를 만들자.
NestJS에서 하는 소개와 철학이 궁금하다면 공식 문서에 들어가보자.
$ npm i -g @nestjs/cli
$ nest new nestjs-overview
$ cd nestjs-overview
위 명령어를 실행하면 아래와 같은 파일 구조를 볼 수 있다.
src
├─ app.controller.ts # 애플리케이션의 컨트롤러
├─ app.controller.spec.ts # 컨트롤러의 단위 테스트
├─ app.module.ts # 애플리케이션의 루트 모듈
├─ app.service.ts # 앱의 서비스
└─ main.ts # `NestFactory`를 사용해 애플리케이의 인스턴스를 생성하는 핵심 파일
main.ts
bootstrap
함수 명은 변경 가능하다.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
Nest 애플리케이션 인스턴스를 생성할때 NestFactory
클래스의 스태틱 메서드를 사용한다.
NestJS에선 express와 fastify 두가지 프레임워크를 지원한다. 인스턴스를 생성할때 generic으로 NestExpressApplication
과 NestFastifyApplication
인터페이스 중 선택해서 생성하면 된다.
const app = await NestFactory.create<NestExpressApplication>(AppModule);
- NestJS에서는
@nestjs/platform-express
패키지를 디폴트로 사용한다. app
객체는 명시한 타입의 메서드를 사용할 수 있다.-
원문에 따르면 각 플랫폼의 특정 api가 필요하지 않으면 generic을 안해줘도 된다고 한다.
- "Note, however, you don't need to specify a type unless you actually want to access the underlying platform API."
애플리케이션 실행
아래 명령어를 통해 애플리케이션을 실행할 수 있다.
$ npm run start
src/main.ts
파일에 정의된 포트로 HTTP 서버가 실행된다.- http://localhost:3000로 접속하면
Hello World!
문장을 확인할 수 있다.
Next | NestJS OVERVIEW(2) - Controller |
Intro | NestJS OVERVIEW(0) - Intro |