1.0.0 • Published 5 years ago

egg-ratelimit v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

egg-ratelimit

NPM version build status Test coverage David deps Known Vulnerabilities npm download

灵感来自于egg-ratemiter,优化支持 url search

Rate limiter for Egg.js backed by Redis.

  • 支持配置路由限制访问速率 支持url search
  • 支持Controller控制器内配置访问速率限制

依赖说明

依赖的 egg 版本

egg-ratelimit 版本egg 1.x
1.x😁
0.x

依赖的插件

开启插件

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

使用说明

1. 路由配置

exports.ratelimit = {
  // 1. 开启配置
  app: true

  // 2. 配置路由
  router: [
    {
      path: '/index.html',  // 路径
      max: 3,   // 单位时间最多访问次数
      time: '1s', // 时间单位 s m h d y ...
      message: '请求太频繁,请稍候再试' // 自定义错误信息
    }
  ]
}

2. Controller单独配置

// ...

if (await ctx.Limit({ max: 5, time: '5s', message: '请求太频繁,请稍候再试' })) {
  return
}

// 正常请求
return {
  // ...
}

// ...

详细配置

请到 config/config.default.js 查看详细配置项说明。

提问交流

请到 egg issues 异步交流。

License

MIT

1.0.0

5 years ago