UNPKG

@mark01/express-utils

Version:

npm package that contains utilities for express.js

21 lines (20 loc) 670 B
import rateLimiter from 'express-rate-limit'; import { RedisStore } from 'rate-limit-redis'; import { AppError } from '../error'; export const rateLimit = (redis, max, prefix = 'common', minutes = 15) => { const store = new RedisStore({ prefix: `rl-${prefix}:`, sendCommand: (...args) => redis.call(...args), }); return rateLimiter({ store, max, windowMs: minutes * 60 * 1000, standardHeaders: true, legacyHeaders: false, handler(_, __, next) { next(new AppError(`Too many requests! Please try again in ${minutes} minute(s)!`, 429)); }, }); }; export default rateLimit;