🚀 Express.js Intermediate

How do you implement rate limiting in Express.js?

Answer

Rate limiting protects your API from abuse, brute force attacks, and DoS. Use the express-rate-limit package: const rateLimit = require('express-rate-limit'); const limiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 100 }); app.use(limiter); — limits each IP to 100 requests per 15 minutes. For login endpoints, apply a stricter limiter. Combine with express-slow-down to progressively slow responses before outright blocking. For distributed systems where multiple server instances share an IP counter, use a Redis store: express-rate-limit supports pluggable stores. Rate limit headers (X-RateLimit-Limit, X-RateLimit-Remaining) inform clients of their quota.