使用apidoc根据JS文件生成接口文档

1.安装nodejs。下载网址:http://www.nodejs.org

2.安装apidoc。运行cmd,切换到nodejs的安装目录,在命令行输入:

1
npm install apidoc -g

,完成apidoc的安装。

3.在nodejs的安装目录新建一个myapp的文件夹(用于放接口源码文件,文件为js格式,源码如下)。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @api {POST} /address/index 用户地址添加/修改
* @apiGroup Address
* @apiVersion 0.0.1
* @apiDescription 用户地址添加/修改
* @apiParam {String} _csrf
* @apiParam {int} phone 手机号
* @apiParam {String} type 添加时add/修改时edit
* @apiParam {String} username 联系人
* @apiParam {String} province 省
* @apiParam {String} city 市
* @apiParam {String} district 区
* @apiParam {String} address 详细地址
* @apiParam {int} code 邮编
* @apiParam {int} default 0 非默认 1 默认
* @apiParam {int} id 收货地址id,type为edit时,必须传
* @apiParamExample {json} 请求样例:
* ?account=sodlinken&password=11223344&mobile=13739554137&vip=0&recommend=
* @apiSuccess (200) {String} msg 信息
* @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
* @apiSuccessExample {json} 返回样例:
* {"code":"0","msg":"注册成功"}
*/
 
/**
* @api {POST} /login 用户登录
* @apiGroup Users
* @apiVersion 0.0.1
* @apiDescription 用于用户登录
* @apiParam {String} userName 用户名
* @apiParam {String} password 密码
* @apiParamExample {json} 请求样例:
* ?userName=张三&password=11223344
* @apiSuccess (200) {String} msg 信息
* @apiSuccess (200) {String} code 0 代表无错误 1代表有错误
* @apiSuccess (200) {String} user 用户信息
* @apiSuccess (200) {String} userId 用户id
* @apiSuccessExample {json} 返回样例:
* {"code":"0","msg":"登录成功","userId":"fe6386d550bd434b8cd994b58c3f8075"}
*/
 
/**
* @api {GET} /users/:id 获取用户信息
* @apiGroup Users
* @apiVersion 0.0.1
* @apiDescription 获取用户信息
* @apiSuccess (200) {String} msg 信息
* @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
* @apiSuccess (200) {String} name 真实姓名
* @apiSuccess (200) {String} mobile 手机号
* @apiSuccess (200) {String} birthday 生日
* @apiSuccess (200) {String} email 邮箱
* @apiSuccess (200) {String} summary 简介
* @apiSuccess (200) {String} recommendCode 我的推荐码
* @apiSuccess (200) {String} idCardNo 身份证号
* @apiSuccess (200) {String} memberState 会员状态 0普通用户 1VIP 2账户冻结
* @apiSuccess (200) {String} address 家庭住址
* @apiSuccess (200) {String} money 账户现金
* @apiSuccessExample {json} 返回样例:
* {
* "code": 0,
* "msg": "",
* "name": "真实姓名",
* "mobile": 15808544477,
* "birthday": "1990-03-05",
* "email": "slocn@gamil.com",
* "summary": "简介",
* "recommendCode": "我的推荐码",
* "idCardNo": "身份证号",
* "memberState": 1,
* "address": "家庭住址",
* "money": "30.65"
* }
*/
/**
* @api {POST} /users/:id 修改(完善)用户信息
* @apiGroup Users
* @apiVersion 0.0.1
* @apiDescription 修改(完善)用户信息
* @apiParam (200) {String} [name] 真实姓名
* @apiParam (200) {String} [mobile] 手机号
* @apiParam (200) {String} [birthday] 生日
* @apiParam (200) {String} [email] 邮箱
* @apiParam (200) {String} [summary] 简介
* @apiParam (200) {String} [idCardNo] 身份证号
* @apiParam (200) {String} [address] 家庭住址
* @apiSuccess (200) {String} msg 信息
* @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
* @apiSuccessExample {json} 返回样例:
* {"code":"0","msg":"修改成功"}
*/

4.生存文档

在nodejs安装目录下,cmd命令行运行 :

1
apidoc -i myapp/ -o apidoc/

 

myapp是当前工作目录下的源码目录

apidoc是用于存放生成出的文档文件的目录

运行成功显示:success: Done

到apidoc文件夹中打开index.html

posted @ 2017-07-18 15:39  zhangzhongxian  阅读(370)  评论(0编辑  收藏  举报