← Back to editvolt.com
πŸ—‚οΈ
πŸ”
πŸ”€
▢️
⚑
βš™οΈ
Explorer Β· my-app
β–Ύ πŸ“ src
β–Ύ πŸ“ middleware
rateLimit.ts
auth.ts
api.ts
server.ts
utils.ts
β–Έ πŸ“ tests
πŸ“„ package.json
πŸ“„ tsconfig.json
πŸ“„ .editvoltignore
rateLimit.ts ●
api.ts
server.ts
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) {
⚑ Edit with EditVolt ⌘K
+ 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}
⚑ EditVolt
ChatAgent
Add rate limiting to all API routes and cover it with tests
Plan
1. Create src/middleware/rateLimit.ts
2. Wire it into the router in api.ts
3. Add tests in tests/rateLimit.test.ts
βœ“ Created rateLimit.ts +64
βœ“ Edited api.ts +3 βˆ’1
βœ“ Created rateLimit.test.ts +41
βœ“ Tests passed β€” 27/27

All API routes now return 429 after 100 requests/minute per IP. Review the staged diff when ready.
switch it to Redis so it works across instances
Good call for multi-instance deploys. Updating the middleware to use a Redis sliding window β€” see the inline diff in rateLimit.ts…
Describe a task for the agent… ⌘L
πŸ”€ main βœ“ 0 errors Β· 1 warning
TypeScript 5.6 UTF-8 · LF Ln 9, Col 14 ⚑ EditVolt: ready