Created
May 1, 2013 02:00
-
-
Save jrf0110/5493304 to your computer and use it in GitHub Desktop.
Simple ass rate-limiting express middleware - Probably very naive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Rate Limiter | |
**/ | |
var questers = {}; | |
module.exports = function(limit){ | |
return function(req, res, next){ | |
if (questers[req.ip]) return res.status(403).end(); | |
questers[req.ip] = setTimeout(function(){ | |
delete questers[req.ip]; | |
}, limit); | |
next(); | |
}; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rateLimit = require('./middleware/rate-limiter'); | |
app.post('/something' | |
, rateLimit(2000) // Limit requests to every 2 seconds | |
, myLittleHandler | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment