1、apidoc的官網
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文檔:
