koa2-rate-limit

Simple rate limiting for Koa2, supports clustered apps, blacklisting and whitelisting.

Usage no npm install needed!

<script type="module">
  import koa2RateLimit from 'https://cdn.skypack.dev/koa2-rate-limit';
</script>

README

koa2-rate-limit

Simple rate limiting for Koa2, supports clustered apps, blacklisting and whitelisting.

Install

npm install koa2-rate-limit

Usage

Import the module:

const rateLimit = require('koa2-rate-limit').rateLimit;

OR:

import {rateLimit} from 'koa2-rate-limit';

Set up an application-wide middleware for rate limiting:

app.use(rateLimit(
{
    routes:
    [
        {method: "POST", path: "/v1/authenticate"}
    ],
    interval: 5 * 60 * 1000,
    max: 10,
    whitelist: [],
    blacklist: []
}))
  • routes {Object[]} The routes to apply rate limiting.
    • method {String|RegExp} A string or regular expression to match for the route method.
    • path {String|RegExp} A string or regular expression to match for the route path.
  • interval {Integer} The default rate limiting window (in milliseconds).
  • max {Integer} The default maximum number of requests before rate limiting is applied.
  • whitelist {String[]} An array of default IP addresses to always allow (you can use CIDR notation).
  • blacklist {String[]} An array of default IP addresses to always deny (you can use CIDR notation).

Note

You can optionally specify interval, max, whitelist and blacklist for each route in routes. If you do this it the values you set against the route will be used instead of the defaults.