02-express安装apidoc生成接口文档
1.安装 npm i apidoc
2.在项目根目录下创建 apidoc.json
{
"name": "news是项目接口文档",
"version": "0.1.0",
"description": "新闻接口文档",
"title": "新闻接口文档",
"url": "http://localhost:3000/api"
}
3.在routes/users.js修改如下
var express = require('express')
var router = express.Router()
/**
* @api {post} /users/login 用户登录
* @apiDescription 用户登录
* @apiName login
* @apiGroup auth
* @apiParam {string} username 用户名
* @apiParam {string} password 密码
* @apiSuccess {json} result
* @apiSuccessExample {json} Success-Response:
* {
* "success" : "true",
* "result" : {
* "username" : "用户名",
* "password" : "密码"
* }
* }
* @apiVersion 0.0.1
*/
router.post('/login', function (req, res, next) {
let username = req.body.username
let password = req.body.password //判断是否存在当前字段
if (username && password) {
//判断是否为空
res.send(req.body)
}
})
module.exports = router
在终端控制台输入
apidoc -i routes/ -o public/apidoc/
再启动项目
npm start
在浏览器中输入localhost:3000/apidoc/index.html