express-light-limiter
Smart express lightweight middleware for rate-limiting based on Zones to prevent memory leaks
Install
- Install module from npm:
$ npm install --save express-light-limiter
Usage
;;let app = ;const limiterConfig =quantum: 10 * 1000 // 10 secondsmaxRequestsPerQuantum: 20 // requests restriction is 20 for a single quantumlookup: 'connection.remoteAddress' // it can be an array or a functionerror: 'Too many requests' 429 // your custom error object;app
API options
-
lookup
:Function|String|Array.<String>
value lookup on the request object. Can be a single value, array or function. See examples below. Default isconnection.remoteAddress
. -
quantum
:Number
unit of time for counting requests. Default is5 * 1000
(5 sec). -
maxRequestsPerQuantum
:Number
number of requests that allowed in a single quantum of time. -
error
:Object
optional param allowing throw a custom error innext
function
Examples
// limit by IP address// or if you are behind a trusted proxy (like nginx)// by user (assuming a user is logged in with a valid id)// limit users by their id or IP (if user's id is not available)// with a function
Separate methods
// limiter with default optionsapp;app
License
MIT © 2016 Alexander Belov