Consider this Express.js middleware using the express-rate-limit package:
import rateLimit from 'express-rate-limit';
const limiter = rateLimit({
windowMs: 60000, // 1 minute
max: 5,
standardHeaders: true,
legacyHeaders: false
});
app.use('/api/', limiter);What will the server respond with if a client makes 6 requests within one minute to /api/?
import rateLimit from 'express-rate-limit'; const limiter = rateLimit({ windowMs: 60000, // 1 minute max: 5, standardHeaders: true, legacyHeaders: false }); app.use('/api/', limiter);
Think about what HTTP status code is standard for rate limiting.
The express-rate-limit middleware blocks requests exceeding the max limit within the time window. It responds with HTTP 429 Too Many Requests and a JSON message by default.
Choose the correct code snippet to create a rate limiter with express-rate-limit that allows 10 requests every 10 seconds.
Check the exact option names and units expected by express-rate-limit.
The windowMs option expects milliseconds, so 10 seconds is 10000 ms. The max option sets the max requests allowed.
Review this code snippet:
import rateLimit from 'express-rate-limit';
const limiter = rateLimit({
windowMs: 60000,
max: 3
});
app.get('/data', limiter, (req, res) => {
res.send('Data response');
});Clients report they can make unlimited requests without being blocked. What is the most likely cause?
import rateLimit from 'express-rate-limit'; const limiter = rateLimit({ windowMs: 60000, max: 3 }); app.get('/data', limiter, (req, res) => { res.send('Data response'); });
Think about how Express detects client IP addresses behind proxies.
If the app is behind a proxy (like Nginx), Express needs app.set('trust proxy', 1) to correctly identify client IPs. Without it, all requests appear from the proxy IP, so rate limiting does not work per client.
When deploying a Node.js app with multiple server instances, why should you use a distributed store (like Redis) for rate limiting data?
Think about what happens when multiple servers handle requests independently.
Without a shared store, each server tracks limits separately. Clients can bypass limits by hitting different servers. A distributed store centralizes counters to enforce limits globally.
Given this Express.js setup:
import rateLimit from 'express-rate-limit';
const limiter = rateLimit({
windowMs: 60000,
max: 5,
standardHeaders: true,
legacyHeaders: false
});
app.use('/api/', limiter);If a client makes 3 requests in the current window, what will the X-RateLimit-Remaining header value be on the 3rd response?
import rateLimit from 'express-rate-limit'; const limiter = rateLimit({ windowMs: 60000, max: 5, standardHeaders: true, legacyHeaders: false }); app.use('/api/', limiter);
Remember the header shows how many requests remain before hitting the limit.
The limit is 5 requests per window. After 3 requests, 2 remain, so the header value is 2.