Rate limit middleware for Rill.
Uses Keyv for persistance. Supports caching in memory, mongodb, redis, sqllite, mysql and postgre.
npm install @rill/rate-limit
const app = rill()
const limit = require("@rill/rate-limit")
// Setup the middleware. (1000 req/hr max)
app.use(limit({
max: 1000,
duration: '1 hour'
}))
{
/**
* 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).
}
}
- Use
npm test
to run tests.
Please feel free to create a PR!