Learn how to implement effective rate limiting in your Express APIs to enhance security and prevent abuse. Step-by-step tutorial with code examples.
Table of content
APIs are crucial in modern web development, but without proper safeguards, they are vulnerable to abuse. Rate limiting helps to:
Rate limiting restricts the number of API requests a user can make within a specific timeframe. For example, you might allow 100 requests per 15 minutes per IP address.
The easiest way to implement rate limiting with Express.js is by using the express-rate-limit
middleware.
npm install express-rate-limit
const express = require('express');
const rateLimit = require('express-rate-limit');
const app = express();
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
message: 'Too many requests from this IP, please try again after 15 minutes',
});
app.use(limiter);
app.get('/', (req, res) => {
res.send('Welcome to the API!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
You can customize the limiter to suit your needs. For example, apply rate limiting only to specific routes:
app.use('/api/', limiter); // Apply only to API routes
Or set different limits for different endpoints:
const authLimiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 5, // Limit to 5 requests per window
});
app.use('/api/auth/', authLimiter);
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 10,
handler: (req, res) => {
res.status(429).json({ error: 'Rate limit exceeded. Please wait.' });
},
keyGenerator: req => req.user ? req.user.id : req.ip,
headers: true
});
Implementing rate limiting in your Express APIs is straightforward and essential for security and stability. The express-rate-limit
middleware provides flexibility and ease of use. By fine-tuning your configuration, you can ensure your API serves users reliably, fairly, and securely.