apidoc 是一款api文檔生成工具,簡單易用。apidoc會生成一個html,建議同時配置在服務器上,例如通過Nginx配置靜態資源,供多人協作瀏覽,配置好之后,每次修改了api,只需要一行命令即可完成文檔更新。
一、安裝
$npm install apidoc -g
二、配置(可選)
在項目目錄下添加 apidoc.json 文件
ex:{
"name": "example", //項目名稱
"version": "0.1.0",
"description": "apiDoc basic example",
"title": "Custom apiDoc browser title",
"url" : "https://api.github.com/v1" //一般為域名
}
三、基本格式
在相關文件內加入注釋,ex:
/**
* @api {get} /user/:id Request User information
* @apiName GetUser
* @apiGroup User
*@apiPermission Web
*
* @apiParam {Number} id Users unique ID.
*
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname? Lastname of the User.
* @apiSuccessExample Success-Response:
*? ? HTTP/1.1 200 OK
*? ? {
*? ? ? "firstname": "John",
*? ? ? "lastname": "Doe"
*? ? }
*/
三、基本命令
ex:
$ apidoc -i controller -o doc
controller:輸入目錄,添加了上面的api注釋的目錄
-o :文檔輸出目錄
四、使用
打開doc目錄下的index.html文件,即可看到剛剛生成的api文檔。
如果配置到服務器上,使用方法同理。