개발이야기/Node&Nest

Windows11에서 Nest.js 개발환경 셋팅

Roslyn 2024. 1. 17. 11:37
반응형

먼저 nest.js를 설치한다.

npm i -g @nestjs/cli

 

이제 원하는 폴더로 이동해서 프로젝트를 생성해 보자.

nest new sampleproject

 

여기서 [sampleproject] 는 내가 원하는 프로젝트명인데, 가급적 소문자를 권장한다.

그럼 다음과 같이 설치에 사용할 저장소를 선택해 달라는 질문이 나온다.

 

 

여기서는 그냥 "npm"을 선택해서 진행했다.

셋팅이 완료되고 나면 dir 명령을 통해 폴더를 확인해 보자.

내가 지정한 프로젝트명과 동일한 폴더명이 생겨 있을 것이다.

해당 폴더로 이동하고, vs code로 에디터를 열어보자.

루트 폴더 아래로 기본 파일들이 생성된 것을 볼 수 있다.

 

 

기본 생성된 결과물로 실행을 시켜보자.

이제 swagger을 설치해 보자.

 

npm install --save @nestjs/swagger swagger-ui-express

 

설치가 완료되면 main.ts 파일을 다음과 같이 수정해 보자.

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const config = new DocumentBuilder()
    .setTitle('Your API Title')
    .setDescription('Your API Description')
    .setVersion('1.0')
    .addTag('nestjs-swagger-example') // optional
    .build();

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document);

  await app.listen(3000);
}
bootstrap();

 

수정했으면 이제 콘솔에서 실행시켜 보자.

npm run start

 

기본적으로 3000번 포트에 동작하도록 되어 있다.

주소창에 http://localhost:3000/api 로 접속해 보자.

 

 

반가운 스웨거가 우리를 반겨주고 있다.

반응형