关于javascript:egg参数校验

5次阅读

共计 1127 个字符,预计需要花费 3 分钟才能阅读完成。

简略介绍

egg-router-auth 基于 apidoc 来构建参数校验

性能

  • 在 egg 我的项目中验证路由表中是否蕴含申请的 url,如果申请了路由表中未存在的路由,则会提醒相应信息
  • 在 egg 我的项目中验证在许可的路由中是否存在用户的 jwt 登录
  • 验证路由的参数

装置

npm i egg-router-auth --save

配置相干

文件目录配置

倡议 文件目录需按此配置

project
├── app
│   ├── controller
│   │   └── home.js
│   └── router.js
├── apidoc
│   └── output
|       └── api_data.json
│   └── template
|       └── api_data.json
|
|...

开启插件

// config/plugin.js
exports.auth = {
  enable: true,
  package: 'egg-router-auth',
};

配置

// config/config.default.js
config.auth = {jwtExclude: ['/api/login', '/api/public/verification'], // 验证用户登录须要跳过的路由
  errorCode: -2, // 谬误的 code,
  output: 'apidoc/output', // apidoc 输入目录,必选
  template: 'apidoc/template' // apidoc 模板,可选
}

应用

  // app/controller/home.js
  /**
  * @api {GET} /api/test 一般测试接口
  * @apiParam {string} user 用户名
  */
  async test() {const { ctx} = this;
    const res = '测试';
    ctx.body = res;
  }
  
  /**
  * @api {GET} /api/test 多参数类型测试接口
  * @apiParam {string|null} user 用户名
  */
  async test1() {const { ctx} = this;
    const res = '测试';
    ctx.body = res;
  }
  
  /**
  * @api {GET} /api/test 可选参数测试接口
  * @apiParam {string} [user] 用户名
  */
  async test2() {const { ctx} = this;
    const res = '测试';
    ctx.body = res;
  }
  
  /**
  * @api {GET} /api/test 简单类型测试接口
  * @apiParam {object} user 用户
  * @apiParam {string} user.name 用户名
  */
  async test3() {const { ctx} = this;
    const res = '测试';
    ctx.body = res;
  }

发问交换

请到 egg-router-auth issues 异步交换。

正文完
 0