rill-js / rate-limit

RIll middleware to rate-limit an API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rill
@rill/rate-limit
API stability Standard NPM version Downloads Gitter Chat

Rate limit middleware for Rill.

Uses Keyv for persistance. Supports caching in memory, mongodb, redis, sqllite, mysql and postgre.

Installation

npm install @rill/rate-limit

Example

app.js

const app = rill()
const limit = require("@rill/rate-limit")

// Setup the middleware. (1000 req/hr max)
app.use(limit({
  max: 1000,
  duration: '1 hour'
}))

Options

{
  /**
   * Key used for keyv namespace.
   */
  key: '@rill/rate-limit',
  /**
   * The maximum amount of requests from a user per period.
   */
  max: 2500,
  /**
   * The duration of a period. (in ms or as a string).
   */
  duration: '1 hour',
  /**
   * Overrride the identifier for the users (default is their ip addresss).
   */
  id: ctx => ctx.req.ip,
  /**
   * Passed to keyv, do not send this to client.
   * Mongo db example. (must have installed keyv-mongo).
   */
  cache: !process.brower && {
    uri: 'mongodb://user:pass@localhost:27017/dbname' // Default is in memory (see keyv uri).
  }
}

Contributions

  • Use npm test to run tests.

Please feel free to create a PR!

About

RIll middleware to rate-limit an API.


Languages

Language:JavaScript 100.0%