What I Learned/Framework
[Nest.js] 프로젝트 세팅
Interrobang
2023. 8. 2. 20:07
Nest.js 설치 (계속 프로젝트마다 사용할 것이니 global로)
$ npm i -g @nestjs/cli
원하는 디렉토리에서 터미널을 열고 프로젝트 생성
$ nest new project-name
package manager 선택 -> 이왕이면 npm으로(Nest.js와의 호환성이 높음)
? Which package manager would you ❤️ to use? (Use arrow keys)
❯ npm
yarn
pnpm
모듈 생성
$ nest g mo module_name
컨트롤러 생성
$ nest g co controller_name
서비스 생성
$ nest g s service_name
lodash 설치: 유틸성 패키지
+ tsconfig.json에 속성 추가
$ npm i lodash
"esModuleInterop": true
class-validator 설치: 입력값이 DTO에 적합한지 유효성 검사 (다른 패키지 설치에 문제 있을 경우 삭제 후 재설치)
class-transformer 설치: parameter 자동 형변환
$ npm i class-validator class-transformer
// main.ts
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({ transform: true }));
await app.listen(3000);
}
bootstrap();
@nestjs/mapped-types 설치: DTO 변환 서포트
$ npm i @nestjs/mapped-types
typeorm 설치
db 종류에 따라서 @nestjs/typeorm 설치
$ npm i typeorm
$ npm i @nestjs/typeorm mssql
@nestjs/config 설치: .env를 이용해 db 연결 정보 담기
$ npm i @nestjs/config
@nestjs/jwt 설치 (인증 관련해서 필요시)
$ npm i @nestjs/jwt
cache-manager 설치 (데이터 변경은 적은데 반복적인 조회가 많은 경우, 아니더라도 이왕이면 적용해주는 것이 좋음)
$ npm i cache-manager
$ npm i -D @types/cache-manager
@nestjs/thorottler 설치: 특정 유저가 짧은 시간내에 API를 여러번 호출하는 것을 방지
$ npm i @nestjs/throttler
웹 서버 실행
$ npm run start