mirror of
https://github.com/scratchfoundation/node-redis-rate-limiter.git
synced 2024-12-05 05:21:01 -05:00
20 lines
400 B
JavaScript
20 lines
400 B
JavaScript
|
var rateLimiter = require('./rate-limiter');
|
||
|
|
||
|
module.exports = function(opts) {
|
||
|
var limiter = rateLimiter(opts);
|
||
|
return function(req, res, next) {
|
||
|
limiter(req, function(err, rate) {
|
||
|
if (err) {
|
||
|
next();
|
||
|
} else {
|
||
|
if (rate.current > rate.limit) {
|
||
|
res.writeHead(429);
|
||
|
res.end();
|
||
|
} else {
|
||
|
next();
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
};
|