1. Swagger 모듈을 Nest.js 프로젝트에 추가 npm install --save @nestjs/swagger swagger-ui-express
  2. main.ts에 Swagger 모듈 추가
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

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

  const config = new DocumentBuilder() // Swagger 문서 설정 구성
    .setTitle('Your API')
    .setDescription('API description')
    .setVersion('1.0')
    .addTag('your-tag')
    .build();
  const document = SwaggerModule.createDocument(app, config); // 문서 생성
  SwaggerModule.setup('api', app, document); // Swagger UI 설정

  await app.listen(3000);
}
bootstrap();
  1. Nest.js 컨트롤러와 DTO 클래스에서 Swagger 주석을 사용하여 API 엔드포인트 및 모델을 문서화
import { ApiProperty } from '@nestjs/swagger';

// CreateCatDto 클래스 정의
export class CreateCatDto {
  @ApiProperty() // name 필드를 문서화
  name: string; // 이름(string) 필드

  @ApiProperty() // age 필드를 문서화
  age: number; // 나이(number) 필드
}
import { Controller, Post, Body } from '@nestjs/common';
import { ApiTags, ApiBody, ApiCreatedResponse } from '@nestjs/swagger';
import { CreateCatDto } from './create-cat.dto';

@Controller('cats') // '/cats' 엔드포인트를 다루는 컨트롤러
@ApiTags('cats') // Swagger 문서에 'cats' 태그 추가
export class CatsController {
  @Post() // POST 요청을 처리하는 메서드
	  @ApiCreatedResponse({ description: 'The record has been successfully created.' }) // 응답 설명
	  @ApiBody({ type: CreateCatDto }) // 요청 본문의 형식을 문서화하며, CreateCatDto 클래스를 예상
  create(@Body() createCatDto: CreateCatDto) {
    // 여기에서 createCatDto를 사용하여 새로운 고양이를 생성합니다.
    return 'This action creates a new cat'; // 응답 메시지
  }
}
////////////////////////////////////////////////////////////////////
@Controller('logger')
@ApiTags('logger')
export class LoggerController {
  constructor(private loggerService: LoggerService) {}

  @Get('test')
  @ApiOkResponse({ description: 'Logged to Redis And Send to Slack' })
  async logTest() {
    this.loggerService.log(`Hello World!`);
    return 'Logged to Redis And Send to Slack';
  }
}
  1. 브라우저에서 http://localhost:3000/api 또는 지정한 다른 포트로 접속하여 Swagger UI 확인