Nest.js 프로젝트를 처음 시작할 때 필요한 설치부터 Controller, Service, Module 생성 방법까지 정리한 문서입니다.
Nest.js를 시작하려면 Nest CLI를 전역 설치해야 합니다.
npm i -g @nestjs/cli
nest new my-nest-app
src/
├── app.controller.ts // 기본 Controller
├── app.service.ts // 기본 Service
├── app.module.ts // 루트 Module
├── main.ts // 앱 시작점
nest generate controller board
# 또는 줄여서
nest g co board
nest generate service board
# 또는 줄여서
nest g s board
nest generate module board
# 또는 줄여서
nest g mo board
src/board/
├── board.controller.ts
├── board.service.ts
└── board.module.ts
기능 | 설치 명령어 | 설명 |
---|---|---|
유효성 검사 | npm i class-validator class-transformer | DTO에 유효성 검사 데코레이터 사용 가능 |
Swagger 문서화 | npm i @nestjs/swagger swagger-ui-express | API 문서를 자동 생성 |
MongoDB 연동 | npm i mongoose @nestjs/mongoose | Mongoose ODM 연동 |
PostgreSQL 연동 | npm i @nestjs/typeorm typeorm pg | TypeORM + PostgreSQL 연동 |
$ npm install --save @nestjs/swagger
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('board')
.build();
const documentFactory = () => SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, documentFactory);
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
@Controller('board')
@ApiTags('board')
export class BoardController {