1.0.0-beta.7 • Published 1 year ago

koa-util-limit v1.0.0-beta.7

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

koa-util-limit

Install

$ npm install koa-util-limit

Usage

const koaRateLimit = require("koa-util-limit");
const Redis = require("ioredis");
const koa = require("koa");
const app = koa();
app.use(
  koaRateLimit({
    //memory or redis
    driver: "redis",
    //If it is memory, it can be ignored
    db: new Redis(),
    //100 calls at most in 60 * 1000ms
    duration: 60 * 1000,
    max: 100,
    //id unique identification
    id: (ctx) => ctx.ip,
    //whitelist
    white: async (ctx) => {
      //return boolean
    },
    //blacklist
    black: async (ctx) => {
      //return boolean
    },
    //namespace
    namespace: "limit",
    //error response
    error: {
      code: 429,
      msg: "too many requests",
    },
  })
);