koa2+apidoc,编写在线文档

1,500 阅读1分钟

APIDOC****的官网 apidocjs.com

安装:

npm install apidoc -g  

一、再根目录创建一个 apidoc.json 文件(初始化用)

{
  "name": "blog-api",
  "version": "1.0.0",
  "description": "blog项目API文档",
  "title": "blog项目API文档",
  "url": "http://localhost:8001",
  "sampleUrl": "http://localhost:8001",
  "forceLanguage": "zh-cn",
  "template": {
    "withCompare": true,
    "withGenerator": true
  }
}

name: 项目名称

version: 项目的版本

description: 项目简介

title: 浏览器的标题文字

url: 在线文档地址

sampleUrl: 窗体来测试API方法(发送请求)

forceLanguage: 浏览器语言

template: 设置特定于apiDoc的默认模板

二、在app.js中创建访问路径

// 配置在线接口文档,放在 public 中,访问 localhost:端口
app.use(require('koa-static')(__dirname + '/public'))

三、在定义的接口路由上方写上注解

例子:

/**
 * @api {get} /api/blog/list 列表(用户查看)
 * @apiVersion 1.0.0
 * @apiName list
 * @apiGroup blog
 * @apiHeader {String} Authorization 用户授权token
 * @apiParam (query) {String} [author] 作者
 * @apiParam (query) {String} [keywrod] 标题搜索 
 * @apiSampleRequest /api/blog/list
 */
router.get("/list", async (ctx, next) => {})

/**
 * @api {post} /api/register 注册
 * @apiVersion 1.0.0
 * @apiName register
 * @apiGroup user
 * @apiParam {String} username 用户名
 * @apiParam {String} password 密码
 * @apiParam {String} realname 昵称
 * @apiSampleRequest /api/register
 */
router.post('/register', async (ctx, next) => {})

四、扫描路由文件,生成api文档

  apidoc -i routes -o public

五、访问

启动项目,访问http:localhost:8001,就可以看到生成的api文档