Consider this Express app using express-rate-limit:
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({ windowMs: 60000, max: 3 });
app.use(limiter);
app.get('/', (req, res) => res.send('Hello'));What response will the client receive after making 4 requests within one minute?
const rateLimit = require('express-rate-limit'); const limiter = rateLimit({ windowMs: 60000, max: 3 }); app.use(limiter); app.get('/', (req, res) => res.send('Hello'));
Think about what max and windowMs control in rate limiting.
The max option limits the number of requests per windowMs. After 3 requests, the 4th triggers a 429 status with a default message.
Which option contains a syntax error when creating a rate limiter with express-rate-limit?
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 100 });
app.use(limiter);const rateLimit = require('express-rate-limit'); const limiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 100 }); app.use(limiter);
Look carefully at the object syntax inside the parentheses.
Option D is missing a comma between windowMs and max, causing a syntax error.
Given this rate limiter:
const limiter = rateLimit({ windowMs: 60000, max: 5 });
app.use(limiter);
After a client makes 2 requests within the window, what will the X-RateLimit-Remaining header value be in the response?
const limiter = rateLimit({ windowMs: 60000, max: 5 });
app.use(limiter);Remember the header shows how many requests remain before limit is hit.
With max 5, after 2 requests, 3 remain, so the header is '3'.
Consider this code snippet:
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({ windowMs: 60000, max: 2 });
app.get('/api', limiter, (req, res) => res.send('OK'));
app.get('/api', (req, res) => res.send('Fallback'));Why might the rate limiter not block requests after 2 hits?
const rateLimit = require('express-rate-limit'); const limiter = rateLimit({ windowMs: 60000, max: 2 }); app.get('/api', limiter, (req, res) => res.send('OK')); app.get('/api', (req, res) => res.send('Fallback'));
Think about how Express matches routes in order.
Express matches routes in order. The second app.get('/api') overrides the first, so the limiter middleware is never called.
You want to apply a strict rate limit on /login (max 5 requests per 10 minutes) and a looser limit on /api (max 100 requests per 15 minutes). Which setup correctly achieves this?
Think about how middleware can be applied per route.
To have different limits per route, create separate limiter instances with their own configs and apply them to the specific routes.