Cov Rate Limit
A lightweight Rate limiter middleware for Express and Koa. Use to limit repeated requests to public APIs.
Install
$ npm install --save cov-rate-limit
Example
Koa
const Koa = require('koa')
const RateLimit = require('cov-rate-limit')
const app = new Koa()
const rateLimiter = RateLimit({
type: 'koa',
max: 100,
duration: 1000 * 60 * 10,
key (ctx) {
return ctx.ip
}
})
app.use(rateLimiter)
Express
RateLimit with redis
const express = require('express')
const RateLimit = require('cov-rate-limit')
const Redis = require('redis')
const redis = Redis.createClient()
const rateLimiter = RateLimit({
type: 'express',
max: 100,
duration: 1000 * 60 * 10,
key (req) {
return req.ip
},
cache: redis
})
const app = express()
app.set('trust proxy', 1)
const data = {
data: {
message: '11111111'
},
list: Array.from({ length: 10000 }).map((t, i) => i)
}
app.get('/api1', rateLimiter, (req, res) => {
res.send(data)
})
Options
{
type: 'koa'
CacheKey: 'C0V_RATE:',
key (req) {
return req.ip
},
max: 500,
duration: 1000 * 60 * 15,
setHeader: true,
cache: redis
endSender (req, res) {},
}
License
MIT © Awe
inspiration by express-rate-limit