Express rate limiter for apps using mongoose
npm i @markkuhn/express-rate-limiter
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var rateLimiter = require('@markkuhn/express-rate-limiter');
app.use(rateLimiter({
requestsPerMinute: 100,
identifier: function(req) {
return req.headers['x-forwarded-to'];
},
onBlocked: function(req, res) {
res.sendStatus(429);
console.log('Too many requests');
}
}));
rateLimiter(options)
-
requestsPerMinute
:Number
amount of requests allowed per minute (default:60
). -
identifier
:Function
returns an identifier for a request (default:req.headers['x-forwarded-to'] || req.connection.remoteAddress
). -
onBlocked
:Function
called when limit has been reached (default:res.sendStatus(429)
).