0.1.0 • Published 7 years ago

cov-rate-limit v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
7 years ago

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, // 10 min
    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' // 'express'
    CacheKey: 'C0V_RATE:',
    key (req) {
        return req.ip
    },
    max: 500, // max requests within duration [500]
    duration: 1000 * 60 * 15, // of limit in milliseconds [15 * 60 * 1000]
    setHeader: true,
    cache: redis // redis client [in memory cache]
    endSender (req, res) {},
    // endSender (ctx, next) {}
}

License

MIT © Awe

inspiration by express-rate-limit