Last active
December 11, 2019 04:11
-
-
Save maapteh/e79e5dc2818ba16d262c1a3c7233249c to your computer and use it in GitHub Desktop.
rate limiter example
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
function (limit) { | |
const RateLimiter = require('limiter').RateLimiter; | |
const isUserAnonymous = require('./helper/is-anonymous'); | |
const limiter = new RateLimiter(limit, 'second', true); | |
return (req, res, next) => { | |
const isAnonymous = isUserAnonymous(req); | |
if (isAnonymous) { | |
next(); | |
} | |
if (limiter.tryRemoveTokens(1)) { | |
next(); | |
} else { | |
res.locals.ratelimited = true; | |
next(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment