From 193999b1ae6a30357b50df6aae780760d0fc3784 Mon Sep 17 00:00:00 2001 From: Hyein Jeong Date: Wed, 6 Nov 2024 18:21:11 +0900 Subject: [PATCH] =?UTF-8?q?[BE][Docs]=20#82=20:=20swagger=20=EC=84=A4?= =?UTF-8?q?=EC=B9=98=20=EB=B0=8F=20=EC=98=88=EC=8B=9C=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - swagger 설치 - 예시 코드 작성 - 예시 코드 동작 확인 --- backend/package.json | 4 +++- backend/src/index.js | 19 ++++++++++++------- backend/src/routes/example.js | 13 +++++++++++++ backend/swaggerConfig.js | 24 ++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 backend/src/routes/example.js create mode 100644 backend/swaggerConfig.js diff --git a/backend/package.json b/backend/package.json index d48b18cf..7927b540 100644 --- a/backend/package.json +++ b/backend/package.json @@ -20,6 +20,8 @@ "author": "", "license": "ISC", "dependencies": { - "express": "^4.21.1" + "express": "^4.21.1", + "swagger-jsdoc": "^6.2.8", + "swagger-ui-express": "^5.0.1" } } diff --git a/backend/src/index.js b/backend/src/index.js index 9b77d4fa..e5393134 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -1,12 +1,17 @@ -const express = require('express'); +import express from 'express'; +import swaggerUi from 'swagger-ui-express'; +import specs from '../swaggerConfig.js'; const app = express(); -const port = process.env.PORT || 3000; +const port = 3001; -app.get('/', (req, res) => { - res.json({ - success: true, - }); +app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs)); + +// 예제 라우터 (추가 예정인 라우터의 주석을 Swagger 주석 형식으로 문서화) +app.get('/example', (req, res) => { + res.send('Hello World'); }); -app.listen(port, () => {}); +app.listen(port, () => { + console.log(`Server is running on http://localhost:${port}`); +}); diff --git a/backend/src/routes/example.js b/backend/src/routes/example.js new file mode 100644 index 00000000..e9168225 --- /dev/null +++ b/backend/src/routes/example.js @@ -0,0 +1,13 @@ +/** + * @swagger + * /example: + * get: + * summary: Example endpoint + * description: Returns a simple "Hello World" message + * responses: + * 200: + * description: Successful response + */ +app.get('/example', (req, res) => { + res.send('Hello World'); +}); diff --git a/backend/swaggerConfig.js b/backend/swaggerConfig.js new file mode 100644 index 00000000..b5861345 --- /dev/null +++ b/backend/swaggerConfig.js @@ -0,0 +1,24 @@ +import swaggerJSDoc from 'swagger-jsdoc'; + +const swaggerDefinition = { + openapi: '3.0.0', + info: { + title: 'Your API Name', + version: '1.0.0', + description: 'API documentation for Your Project', + }, + servers: [ + { + url: 'http://localhost:3001', + }, + ], +}; + +const options = { + swaggerDefinition, + apis: ['./routes/*.js'], +}; + +const specs = swaggerJSDoc(options); + +export default specs;