Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE][Docs] #82 : swagger 설치 및 예시 적용 #120

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading
Loading