
Learn how to protect your Node.js and Express APIs from brute-force attacks and abuse using robust rate limiting strategies.
When I build web applications and APIs for clients, performance and uptime are always at the top of the priority list. One of the most common vulnerabilities I see in backend systems is a total lack of request throttling. Without proper controls, a single malicious script or even an accidental infinite loop in a frontend app can flood your server with thousands of requests per second. This leads to API crashes, degraded user experience, and potential downtime.
Today, let us look at how you can secure your Node.js applications against abuse by implementing effective rate limiting using Express and Redis. As a full-stack developer based in Bangladesh, I often help clients scale their backends to handle unpredictable traffic spikes safely. Rate limiting is one of the easiest and most reliable defenses you can add to your stack.
Rate limiting restricts the number of times a user or IP address can hit your API within a specific time window. Without it, your application becomes an easy target for several common attacks:
By implementing rate limits, you ensure that legitimate users get fast responses while automated scrapers and attackers get blocked or slowed down.
The quickest way to add basic rate limiting to an Express app is by using the express-rate-limit package. Let us install it in your project:
npm install express-rate-limit
Next, configure it in your main server file. Here is a standard configuration that limits each IP to 100 requests per 15 minutes on general API routes:
const express = require('express');
const rateLimit = require('express-rate-limit');
const app = express();
// Define the limiter rule
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
message: {
status: 429,
error: 'Too many requests, please try again later.'
}
});
// Apply the rate limiter to all API requests
app.use('/api/', limiter);
app.get('/api/data', (req, res) => {
res.json({ message: 'Secure data delivered successfully.' });
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
The basic memory store in express-rate-limit works well for single-server setups. However, if you deploy your Node.js app across multiple containers or serverless instances, in-memory counters will not sync. Each server instance will track its own count, rendering your limits ineffective.
To solve this, use Redis as a centralized store for your rate limiter. Install the Redis store package:
npm install rate-limit-redis redis
Here is how you connect it to your Express application:
const express = require('express');
const rateLimit = require('express-rate-limit');
const { RedisStore } = require('rate-limit-redis');
const { createClient } = require('redis');
const app = express();
// Create Redis client
const redisClient = createClient({
url: process.env.REDIS_URL || 'redis://localhost:6379'
});
redisClient.connect().catch(console.error);
const redisLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 50,
standardHeaders: true,
legacyHeaders: false,
store: new RedisStore({
sendCommand: (...args) => redisClient.call(...args),
}),
message: {
status: 429,
error: 'Too many requests from this IP, please try again later.'
}
});
app.use('/api/auth/', redisLimiter);
app.post('/api/auth/login', (req, res) => {
res.json({ message: 'Login endpoint' });
});
app.listen(3000);
When building production APIs, keep these additional security principles in mind:
/login, /register, and /forgot-password need much stricter limits (e.g., 5 requests per 15 minutes) to stop brute-force attacks.app.set('trust proxy', 1) so Express reads the real client IP instead of the proxy IP.Whether I am building a content platform like Goldmines Picture Website or a complex logistics tool like JobsiteX, protecting backend endpoints from abuse is a fundamental step I never skip.
Need help building secure, high-performance APIs or web applications for your business? Get in touch to discuss your project.
Tagged with
Leave a comment