How to use koa-ratelimit - 3 common examples

To help you get started, we’ve selected a few koa-ratelimit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github johndatserakis / koa-vue-notes-api / src / index.js View on Github external
import logger from './logs/log'
import userAgent from 'koa-useragent'
import error from 'koa-json-error'
import ratelimit from 'koa-ratelimit'
import redis from 'ioredis'

//Routes
import userActionsRouter from './routes/userActions'
import notesRouter from './routes/notes'

//Initialize app
const app = new Koa()

//Here's the rate limiter
app.use(
    ratelimit({
        db: new redis(),
        duration: 60000,
        errorMessage:
            "Hmm, you seem to be doing that a bit too much - wouldn't you say?",
        id: ctx => ctx.ip,
        headers: {
            remaining: 'Rate-Limit-Remaining',
            reset: 'Rate-Limit-Reset',
            total: 'Rate-Limit-Total',
        },
        max: 100,
    })
)

//Let's log each successful interaction. We'll also log each error - but not here,
//that's be done in the json error-handling middleware
github lionsharecapital / lionshare-api / app.js View on Github external
import Koa from "koa";
import ratelimit from "koa-ratelimit";
import compress from "koa-compress";
import logger from "koa-logger";
import mount from "koa-mount";
import cors from "./middlewares/cors";
import redis from "./db/redis";

import api from "./api";

const app = new Koa();

if (process.env.NODE_ENV === "production") {
  app.use(
    ratelimit({
      db: redis,
      duration: 60000,
      errorMessage:
        "Please stop hitting us so hard. Please deploy your own instance of the API",
      id: ctx => ctx.get("X-Real-IP"),
      headers: {
        remaining: "Rate-Limit-Remaining",
        reset: "Rate-Limit-Reset",
        total: "Rate-Limit-Total"
      },
      max: 50
    })
  );
}
app.use(compress());
app.use(cors());
github garbin / koapi / src / koapi.es View on Github external
throttle(options){
    if (options) this.koa.use(convert(throttle(options)));

    return this;
  }

koa-ratelimit

Rate limiter middleware for koa

MIT
Latest version published 6 months ago

Package Health Score

74 / 100
Full package analysis

Popular koa-ratelimit functions