Skip to content

Commit

Permalink
[BE][Docs] #82 : swagger 설치 및 예시 적용
Browse files Browse the repository at this point in the history
- swagger 설치
- 예시 코드 작성
- 예시 코드 동작 확인
  • Loading branch information
happyhyep committed Nov 6, 2024
1 parent f71c26a commit 193999b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
19 changes: 12 additions & 7 deletions backend/src/index.js
Original file line number Diff line number Diff line change
@@ -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}`);
});
13 changes: 13 additions & 0 deletions backend/src/routes/example.js
Original file line number Diff line number Diff line change
@@ -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');
});
24 changes: 24 additions & 0 deletions backend/swaggerConfig.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 193999b

Please sign in to comment.