# 一.基础

  1. 解释:每个接口有大量参数,前端需要知道有安歇接口和每个接口 有几个参数,类似说明书。是 nodejs 中的一个模块。通过这个模块可以快速生成接口开发文档。

  2. 实现:通过注释来生成接口文档的。

  3. 官网:https://apidocjs.com/

# 二.使用

步骤

  • 步骤 1:下载模块,后期通过命令基于注释生成文档(仅生成一次)

    npm install apidoc -g

  • 步骤 2:在项目根目录创建 apidoc.json 文件(仅一次)

详细见👉:https://apidocjs.com/#configuration

{
  "name": "example",
  "version": "0.1.0",
  "description": "apiDoc basic example",
  "title": "Custom apiDoc browser title",
  "url": "http://localhost:3000" //真实的接口地址
}

// 例子
{
  "name": "测试测试文档",
  "version": "1.1.0",
  "description": "apiDoc basic example",
  "title": "Custom apiDoc browser title",
  "url": "http://localhost:3000" //真实的接口地址
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 步骤 3:写接口注释(改多少次,就生成多少次)

具体参数说明详细见👉:https://apidocjs.com/#params

/**
 * @api {get} /user/:id Request User information
 * @apiName GetUser
 * @apiGroup User
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
// 例子
/**
 *  固定格式 请求方式 请求地址
 * @api {get} /test/ 测试列表
 * 接口名字
 * @apiName GetUser
 * 属于哪个组
 * @apiGroup User
 *api参数,可有多个apiParam
 * @apiParam {Number} id Users unique ID.
 *成功的返回
 * @apiSuccess {String} meta 状态码&提示信息.
 * @apiSuccess {String} data 返回的数
 */
// 示例
res.send({
  meta: {
    state: 200,
    msg: "成功",
  },
  data: data,
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  • 步骤 4:生成接口文档(改多少次,就生成多少次)

apidoc -i ./写接口注释的目录 -o ./接口文档存放目录

运行接口文档存放目录的 index.html 文件