Rate Limiting
Rate limiting restricts how many requests a client can make in a given time window, protecting your API from abuse, brute-force attacks, and accidental overload.
The `express-rate-limit` package makes it easy to add rate limiting middleware to an Express app.
Configuring a limiter
You define a time window and a maximum number of requests; clients exceeding the limit receive a 429 Too Many Requests response.
Applying selectively
Rate limiting can be applied globally or only to sensitive routes like login, where brute-force protection matters most.
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 100 });
app.use(limiter);429 Too Many Requests (after exceeding the limit)This allows a maximum of 100 requests per IP every 15 minutes.
app.post('/login', loginLimiter, (req, res) => {
// login logic
});429 if too many login attemptsApplying a stricter limiter just to /login helps prevent brute-force password guessing.
Key points
- Rate limiting caps requests per client within a time window.
- express-rate-limit is a common middleware for this in Express.
- Exceeding the limit typically returns HTTP 429.
- Apply stricter limits to sensitive endpoints like login.
