Rate-limiting middleware for micro
npm install micro-ratelimit


Rate-limiting middleware for micro.
``js`
$ npm install micro-ratelimit
js
const rateLimit = require('micro-ratelimit')module.exports = rateLimit((req, res) => {
return 'Hello world'
})
``js
const rateLimit = require('micro-ratelimit')// Limit example: 2 requests per 10 sec
module.exports = rateLimit({ window: 10000, limit: 2, headers: true }, (req, res) => {
return 'Hello world'
})
`API
$3
*
window: how long to keep records of requests in memory in ms (default: 1 second)
* limit: max number of requests during window (default: 1)
* keyGenerator: key generator function (req -> client id)
* headers: send rate limit headers (default: false)Default implementation of
keyGenerator:`js
function keyGenerator (req) {
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress
}
``