npm install swagger-jsdoc swagger-ui-express --save-dev
// swagger/swaggerDoc.js
const swaggerJSDoc = require('swagger-jsdoc');
const option = {
const swaggerDefinition = {
info: {
version: "1.0.0",
title: "title",
description: "설명"
},
// url -1
servers: [
{
url: "http://localhost:8000",
}
],
// 또는 ursl -2
host: "localhost:8000",
basePath: "/",
// jwt토큰
securityDefinitions: {
jwt: {
type: 'apiKey',
in: 'header',
name: 'Authorization',
}
},
security: [
{ jwt: [] }
]
},
// router에 swagger 연동
apis: ["/../router/*.js"]
}
const = swaggerSpec = swaggerJSDoc(options);
module.exports = swaggerSpec;
// app.js
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const app = express();
...
app.use('/test', swaggerUi.serve, swaggerUi.setup(require('./swagger/swaggerDoc'), {exploerer: true}));
// router/*.js
/**
* @swagger
* tags:
* name: test
* description: swagger 테스트
*/
/**
* @swagger
* /test/LOGIN:
* post:
* description: 로그인
* tags: [test]
* produces:
* - "application/json"
* parameters:
* - name: "body"
* in: "body"
* required: true
* schema:
* type: object
* properties:
* id:
* type: string
* example: test
* password:
* type: string
* example: 12345678
* responses:
* "200":
* description: "successful operation"
*
*/
router.post("/LOGIN", login)