2018. 9. 30. 19:56 Back-End/Node

swaager

참조 : https://blog.cloudboost.io/adding-swagger-to-existing-node-js-project-92a6624b855b


1. npm 설치

npm i swagger-ui-express -S


2. app에 코드 추가

var swaggerUi = require("swagger-ui-express"),
swaggerDocument = require("./swagger.json");
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));


3. swagger.json 파일 작성

ex) 

{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Title",
"description": "Yet Another Node.js Blogg Application API",
"license": {
"name": "MIT",
"url": "https://opensource.org/licenses/MIT"
}
},
"host": "localhost:3000",
"basePath": "/api/v1",
"tags": [
{
"name": "Users",
"description": "API for users in the system"
}
],
"schemes": ["http"],
"consumes": ["application/json"],
"produces": ["application/json"],
"paths": {
"/users": {
"post": {
"tags": ["Users"],
"description": "Create new user in system",
"parameters": [
{
"name": "user",
"in": "body",
"description": "User that we want to create",
"schema": {
"$ref": "#/definitions/User"
}
}
],
"produces": ["application/json"],
"responses": {
"200": {
"description": "New user is created",
"schema": {
"$ref": "#/definitions/User"
}
}
}
},
"get": {
"tags": ["Users"],
"summary": "Get all users in system",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Users"
}
}
}
}
},
"/users/{userId}": {
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"description": "ID of user that we want to find",
"type": "string"
}
],
"get": {
"tags": ["Users"],
"summary": "Get user with given ID",
"responses": {
"200": {
"description": "User is found",
"schema": {
"$ref": "#/definitions/User"
}
}
}
},
"delete": {
"summary": "Delete user with given ID",
"tags": ["Users"],
"responses": {
"200": {
"description": "User is deleted",
"schema": {
"$ref": "#/definitions/User"
}
}
}
},
"put": {
"summary": "Update user with give ID",
"tags": ["Users"],
"parameters": [
{
"name": "user",
"in": "body",
"description": "User with new values of properties",
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"200": {
"description": "User is updated",
"schema": {
"$ref": "#/definitions/User"
}
}
}
}
}
},
"definitions": {
"User": {
"required": ["email", "_id"],
"properties": {
"_id": {
"type": "string",
"uniqueItems": true
},
"email": {
"type": "string",
"uniqueItems": true
},
"lastName": {
"type": "string"
},
"firstName": {
"type": "string"
}
}
},
"Users": {
"type": "array",
"$ref": "#/definitions/User"
}
}
}


'Back-End > Node' 카테고리의 다른 글

company node ver.1  (0) 2018.10.04
적응 충첩, 초기 반납  (0) 2018.10.04
Node  (0) 2018.09.28
랜덤한 값 생성  (0) 2018.09.27
Node 프로그래밍 시 유의사항 ver.19 제이쿼리 (jQuery) & 표준 라이브러리 (Standard Library)  (0) 2018.09.24
Posted by Yuni-Q

블로그 이미지
https://github.com/Yuni-Q/TIL에 정리하기 전 잊지 않기 위해 간단하게 메모해 두는 곳입니다.
Yuni-Q

태그목록

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.5
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

최근에 올라온 글

최근에 달린 댓글

글 보관함