npm i apidoc -g #全局安装
接着配置api-doc
方式一:根目录配置apidoc.json
{
"name": "example",
"version": "0.1.0",
"description": "apiDoc basic example",
"title": "Custom apiDoc browser title",
"url" : "https://api.github.com/v1"
}
方式二:项目package.json配置api-doc
{
"name": "test",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"ejs": "~2.5.7",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"morgan": "~1.9.0"
},
"devDependencies": {
"express-session": "^1.15.6",
"mysql": "^2.15.0"
},
"apidoc": { //配置api-doc
"title": "接口文档", //Api-Doc的网页Title
"url": "http://localhost:3000" //Api测试需要这个地址,地址必须正确
}
}
编写api代码
var express = require('express');
var router = express.Router();
/**
* @api {get} /users 用户列表
* @apiDescription 用户列表
* @apiName users
* @apiGroup 前端接口
* @apiParam {number} page页码
* @apiSuccess {number} code 返回200为成功
* @apiSuccess {string} message 响应状态
* @apiSuccess {object} data 查询的数据
* @apiSuccessExample {json} 成功回调:
* {
* code : 200,
* message : "成功",
* data:{
* list: [
* {id:1,username:"小明"},
* {id:2,username:"小红"},
* ]
* }
* }
* @apiSampleRequest http://localhost:3000/users
* @apiVersion 1.0.0
*/
router.get('/', function(req, res, next) {
res.json({
code : 200,
message : "成功",
data:{
list: [
{id:1,username:"小明"},
{id:2,username:"小红"},
]
}
});
});
module.exports = router;
项目目录下运行
apidoc -i routes/ -o public/apidoc/
// -i 是api目录 -o 是生成文档的目的地
运行成功后,浏览器打开 http://localhost:3000/apidoc
网友评论