Your API is getting hammered. Some script kiddie is brute-forcing your /login
endpoint. Your cloud bill is skyrocketing because of one rogue client. Youโre this close to rage-quitting DevOps forever.
Breathe. express-rate-limit
is here to save your sanityโwith just 5 lines of code. Letโs lock things down.
Why Rate Limit? (The Brutal Truth)
-
Stop brute-force attacks โ No more
admin:password
attempts. - Prevent DDoS โ Avoid becoming a victim of someone elseโs script.
- Save $$$ โ Fewer requests = lower cloud bills.
- Fair usage โ Protect your API for real users.
Installation (Quick Start)
npm install express-rate-limit
Basic Protection (5 Lines of Code)
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per window
message: 'Too many requests, please try again later.'
});
app.use(limiter); // Apply to all routes
โ Instantly blocks:
- Brute-force attacks
- Runaway scripts
- API spam
Advanced Tactics (For Paranoid Devs)
1. Targeted Rate Limiting
// Only limit /login
app.post('/login', limiter, (req, res) => { ... });
2. Stricter Rules for Sensitive Routes
const strictLimiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 10, // 10 requests max
message: 'Slow down! Too many login attempts.'
});
app.post('/login', strictLimiter);
3. Bypass for Trusted IPs (Internal Services)
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
skip: (req) => req.ip === '192.168.1.1' // Bypass for internal IP
});
4. Redis Backend (For Distributed Apps)
npm install rate-limit-redis
const RedisStore = require('rate-limit-redis');
const limiter = rateLimit({
store: new RedisStore({
redisURL: 'redis://localhost:6379'
}),
windowMs: 15 * 60 * 1000,
max: 100
});
Real-World Rules of Thumb
Endpoint | Rate Limit | Why |
---|---|---|
/login |
10 requests/5 mins | Stop brute force attacks |
/api/search |
100 requests/15 mins | Prevent scraping |
/public/data |
500 requests/1 hour | Fair usage for open APIs |
What Rate Limiting Wonโt Fix
- Sophisticated DDoS attacks โ Use Cloudflare/WAF.
- Auth bypass exploits โ Validate inputs properly.
- Bots pretending to be humans โ Add CAPTCHA.
TL;DR:
-
npm install express-rate-limit
- Copy-paste the 5-line snippet.
- Sleep better knowing your API isnโt being abused.
Your Move:
- Add rate-limiting to your most abused endpoint today.
- Test with
curl -X POST http://localhost:3000/login -v
(watch HTTP 429s!).
Tag the dev whose API is being used as a punching bag. They need this.
Free Toolkit:
Rate limiting war story? Share below! Letโs swap battle scars. ๐ฅ
Top comments (0)