egg使用SwaggerUi做接口调试

输入安装 egg-swagger-doc

npm i egg-swagger-doc --save // 自动生成接口描述配置

配置 egg-swagger-doc

在/config/config.default.js里面配置

config.swaggerdoc = {
    dirScanner: './app/controller', // 配置自动扫描的控制器路径。
    // 接口文档的标题,描述或其它。
    apiInfo: {
      title: 'NAPI', // 接口文档的标题。
      description: 'swagger-ui for NAPI document.', // 接口文档描述。
      version: '1.0.0', // 接口文档版本。
    },
    schemes: [ 'http', 'https' ], // 配置支持的协议。
    consumes: [ 'application/json' ], // 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html。
    produces: [ 'application/json' ], // 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。
    securityDefinitions: { // 配置接口安全授权方式。
      // apikey: {
      //   type: 'apiKey',
      //   name: 'clientkey',
      //   in: 'header',
      // },
      // oauth2: {
      //   type: 'oauth2',
      //   tokenUrl: 'http://petstore.swagger.io/oauth/dialog',
      //   flow: 'password',
      //   scopes: {
      //     'write:access_token': 'write access_token',
      //     'read:access_token': 'read access_token',
      //   },
      // },
    },
    enableSecurity: false, // 是否启用授权,默认 false(不启用)。
    // enableValidate: true,    // 是否启用参数校验,默认 true(启用)。
    routerMap: true, // 是否启用自动生成路由,默认 true (启用)。
    enable: true, // 默认 true (启用)。
  };

在/config/plugin.js配置

module.exports.swaggerdoc = {
    enable: true,   // 启用 swagger-ui 默认启用
    package: 'egg-swagger-doc', // 指定 第三方插件 包名称
};

在/app/controller层配置

'use strict';

const Controller = require('egg').Controller;

/**
* @controller ConfigController 注释必写,swagger-doc是根据这段注释来生成接口的 )。
*/
class ConfigController extends Controller {
  async single() {
       /**
        * @summary configsingle
        * @description configsingle
        * @router get /configsingle
        * @request query integer limit
        * @request query integer page
        */
        //上面的注释一定要,get请求,参数是int类型的limit和page
        const result = await this.ctx.service.config.single()
        this.ctx.body = {
            data:result
        }
  }
  async edit() {
       /**
        * @summary configedit
        * @description configedit
        * @router post /configedit
        * @request body config value 传入参数
        */
        //上面的注释一定要,post请求,json对象为config,post请求必须创建app/contract/type.js 文件
        const params = this.ctx.request.body
        await this.ctx.service.config.edit(params)
        this.ctx.body = {
            msg:'edit successful'
        }
  }
}

module.exports = ConfigController;

app/contract/type.js 文件

module.exports = {
  // 默认接口类型
  config: {
    id: { type: 'string', required: true, example: '' },
    flag: { type: 'boolean', required: true, example: '' },
    refreshtime: { type: 'integer', required: true, example: '' },
    days: { type: 'integer', required: true, example: '' },
  },
}

egg-swagger-doc文档地址:https://www.npmjs.com/package/egg-swagger-doc

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Egg.js 中使用 MongoDB 来生成表的过程如下: 1. 首先,确保已经在你的项目中安装了 MongoDB 相关的依赖。可以使用 npm 或者 yarn 进行安装,如下所示: ``` npm install egg-mongoose --save ``` 2. 在 Egg.js 的配置文件 (`config/config.default.js` 或者 `config/config.prod.js`) 中进行相关配置。你需要添加以下内容: ```javascript // config/config.default.js exports.mongoose = { client: { url: 'mongodb://localhost:27017/your_database_name', options: {}, }, }; ``` 注意将 `'mongodb://localhost:27017/your_database_name'` 替换为你实际的 MongoDB 连接地址和数据库名称。 3. 创建 Model 文件,在 Egg.js 中,可以使用 Mongoose 来定义模型。在 `app/model` 目录下创建一个新的 JavaScript 文件,例如 `user.js`,并编写以下代码: ```javascript // app/model/user.js module.exports = app => { const mongoose = app.mongoose; const Schema = mongoose.Schema; const UserSchema = new Schema({ name: { type: String }, age: { type: Number }, email: { type: String }, }); return mongoose.model('User', UserSchema); }; ``` 4. 使用 Model 进行增删改查操作。在 Controller 中引入 Model,并使用相应的方法来操作数据库。例如,在 `app/controller/user.js` 文件中编写以下代码: ```javascript // app/controller/user.js const Controller = require('egg').Controller; class UserController extends Controller { async create() { const { ctx } = this; const { name, age, email } = ctx.request.body; const user = new ctx.model.User({ name, age, email, }); try { await user.save(); ctx.body = 'User created successfully'; } catch (error) { ctx.body = error; } } async find() { const { ctx } = this; try { const users = await ctx.model.User.find(); ctx.body = users; } catch (error) { ctx.body = error; } } // 其他操作方法类似 } module.exports = UserController; ``` 这样,你就可以在 Egg.js 中使用 MongoDB 来生成表,并进行相应的增删改查操作了。需要注意的是,上述代码仅为示例,你可能需要根据你的实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值