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 |