1. 程式人生 > >nodejs restful 的api文件生成

nodejs restful 的api文件生成

1、apidoc的官網

http://apidocjs.com

 

2、安裝nodejs環境,安裝apidoc 

npm install apidoc -g

 

3、在專案根目錄建立:apidoc.json;如:

{   "name": "video-server",   "version": "1.0.0",   "description": "video-server相關API文件",   "title": "video-server API",   "url": "http://localhost:3001/api",   "sampleUrl": "http://localhost:3001/api",   "forceLanguage": "zh-cn",   "template": {     "withCompare": true,     "withGenerator": true   } }   4、public目錄下新建資料夾apidoc,用於存放APIDOC生成的檔案,app.js新增一句話:(我使用koa2) app.use(require('koa-static')(__dirname + '/public'))   5、在對應的介面上添加註釋,可參考下邊的配置: /** * 獲取歷史視訊列表 * @api {post} /getfiles 獲取歷史視訊列表 * @apiDescription 根據通道、日期資訊獲取歷史視訊列表 * @apiName getfiles * @apiParam {String} channelid 通道號 * @apiParam {String} date 日期 * @apiSuccessExample {json} Success-Response: * { * "status": "success", * "files": * [ * { * "filepath":"/rec/channel1/2018-12-19_12-12-12.mp4", * "filename":"2018-12-19 12:12:12.mp4", * "filesize":"440MB" * } * ] * } * @apiErrorExample {json} Error-Response: * { * "status": "failed", * } * @apiVersion 1.0.0 */   6、生成API文件 apidoc -i routes/ -o public/apidoc   7、index.js 增加如下內容: router.get('/apidoc', function (req, res, next) {   res.type('text/html')   res.sendfile('public/apidoc/index.html'); });   8、啟動專案,訪問http://localhost:3000/apidoc/index.html,就可以看到生成的api文件: