1import { Request, Response, NextFunction } from "express";
2
3// Sliding-window rate limiter, keyed by client IP
4const WINDOW_MS = 60_000;
5const MAX_REQUESTS = 100;
6const hits = new Map<string, number[]>();
7
8export function rateLimit(req: Request, res: Response, next: NextFunction) {
+ const key = `ratelimit:${req.ip}`;
+ const count = await redis.incr(key);
+ if (count === 1) await redis.pexpire(key, WINDOW_MS);
12 if (count > MAX_REQUESTS) {
13 return res.status(429).json({ error: "Too many requests" });
14 }
15 next();
16}