import ms from 'ms';
import {RateLimiter, RateLimitExceededError} from 'rateman';
const rateLimiter = new RateLimiter({
name: 'user',
windows: [
{span: ms('1m'), limit: 10},
{span: ms('1h'), limit: 100},
],
});
try {
await rateLimiter.attempt('[u]');
} catch (error) {
if (error instanceof RateLimitExceededError) {
// ...
}
}
Express
app.use((req, _res, next) => {
const ip = req.headers['x-forwarded-for'] || req.ip;
void rateLimiter.attempt(ip).then(
() => next(),
error => next(error),
);
});