作業環境:
express 4.16.0
swagger-ui-express 3.0.10
swagger-jsdoc 1.10.3
指令:
==========================================
express
npm install
npm start
http://127.0.0.1:3000/
==========================================
npm install swagger-ui-express
npm install swagger-jsdoc
http://127.0.0.1:3000/api-docs/
1.執行結果
2.設定JSON格式
3.編寫區域
GET & POST 部分 (users.js)
=================================
var express = require('express');
var router = express.Router();
/**
* @swagger
* /users:
* get:
* tags:
* - users
* description: Returns all users
* produces:
* - application/json
* responses:
* 200:
* description: An array of users
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* id:
* type: integer
* name:
* type: string
*/
/* GET users listing. */
router.get('/', function(req, res, next) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ "id": 1,"name":"user1" }));
//res.send('respond with a resource');
});
/**
* @swagger
* /users:
* post:
* tags:
* - users
* description: Creates a new user
* produces:
* - application/json
* parameters:
* - name: body
* in: body
* required: true
* schema:
* type: object
* required:
* - username
* - password
* properties:
* username:
* type: string
* password:
* type: password
* example: {
* "username": "UserName",
* "password": "UserPassword"
* }
* responses:
* 200:
* description: An array of users
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* username:
* type: string
* name:
* password: string
*/
/* GET users listing. */
router.post('/', function(req, res, next) {
res.setHeader('Content-Type', 'application/json');
//req.body.username req.body.password
res.end(JSON.stringify({ "username": req.body.username,"password":req.body.password }));
//res.send('respond with a resource');
});
module.exports = router;
==================================
參考網址:
https://www.breakyizhan.com/swagger/2991.html
https://swagger.io/specification/
沒有留言:
張貼留言