Swagger는 API 문서 자동화 도구이다.
프로젝트를 진행하다보면 수많은 API를 작성하게 되는데, 이것을 "문서화" 하여 팀원들에게 공유하는것은 상당히 귀찮은 작업이었습니다. nest.js의 Swagger를 사용하면 자동으로 작성한API를 보여주며, 코드가 수정되면 수정된 API를 자동으로 반영해줍니다. 또한 Swagger는 API를 테스트할 수 있는 화면을 제공합니다.
이제 Swagger를 알았으니, 설치하러 가봅시다.
https://docs.nestjs.com/openapi/introduction
$ 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('cats')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
(주의) app.listen이 swagger보다 위에 있으면 제대로 작동하지 않을 수 있습니다.
await app.listen(3000); //이놈 맨 밑으로
Nest.js라는 프레임워크를 사용하면 할 수록, 개발에만 집중할 수 있도록 감동적인 기능을 제공하는 것을 볼 수 있는것 같습니다. express를 사용할때 Nest.js를 사용해보고 감동 받았다는 후기들을 보았는데 실제로 공부할수록 공감이 갑니다.