git clone https://github.com/swagger-api/swagger-ui.git
npm init (生成package.json文件) name: (node_app) node_app version: (1.0.0) description: entry point: (index.js) test command: git repository: keywords: author: license: (ISC)
npm install express --save
var express = require('express'); var app = express(); app.use('/static', express.static('public')); app.get('/api/person', function (req, res) { //返回的數據類型和url路徑要一至 res.send([{ "firstName": "s", "lastName": "s", "userName": "d" }]); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); });
swagger: "2.0" info: version: 1.0.0 title: Simple API description: A simple API to learn how to write OpenAPI Specification schemes: - https - http host: 192.168.8.109:3000 basePath: /api paths: {}
swagger: "2.0"
info: version: 1.0.0 title: Simple API description: A simple API to learn how to write OpenAPI Specification
schemes: - https host: simple.api basePath: /openapi101
swagger: "2.0" info: version: 1.0.0 title: Simple API description: A simple API to learn how to write OpenAPI Specification schemes: - https - http host: 192.168.8.109:3000 basePath: /api paths: /person: get: summary: Gets some persons description: Returns a list containing all responses: 200: description: A list of Person schema: type: array items: required: - username properties: firstName: type: string lastName: type: string userName: type: string
paths: /persons:
get: summary: Gets some persons description: Returns a list containing all persons.
responses: 200: description: A list of Person
schema: type: array items: required: - username properties: firstName: type: string lastName: type: string username: type: string
#START############################################################################ parameters: - name: pageSize in: query description: 要傳入每一頁面大小 type: number - name: pageNumber in: query description: 要傳入的分頁數量 type: number # END ############################################################################ 完整的類型 swagger: "2.0" info: version: 1.0.0 title: Simple API description: A simple API to learn how to write OpenAPI Specification schemes: - https - http host: 192.168.8.109 basePath: /index paths: /person: get: summary: Gets some persons description: Returns a list containing all #START############################################################################ parameters: - name: pageSize in: query description: 要傳入每一頁面大小 type: number - name: pageNumber in: query description: 要傳入的分頁數量 type: number # END ############################################################################ responses: 200: description: A list of Person schema: type: array items: required: - username properties: firstName: type: string lastName: type: string userName: type: string 經過 get /persons?pageSize=20&pageNumber=2 來訪問第2頁的用戶信息(不超過20條)
paths: /persons: get: summary: Gets some persons description: Returns a list containing all persons. The list supports paging. #START############################################################################ parameters: # END ##############################
swagger: "2.0" info: version: 1.0.0 title: Simple API description: A simple API to learn how to write OpenAPI Specification schemes: - https host: simple.api basePath: /openapi101 paths: /persons: username: type: string #START############################################################################ /persons/{username}: get: summary: Gets a person description: Returns a single person for its username # END ############################################################################