Rate limiting helps control how many times someone can use a service in a set time. It stops too many requests that can slow down or break your app.
To stop users from sending too many requests to your server quickly.
To protect your app from being overwhelmed by bots or attacks.
To make sure everyone gets fair access to your service.
To avoid extra costs from too many API calls.
To keep your app running smoothly during high traffic.
Syntax
Node.js
import rateLimit from'express-rate-limit';
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // time window in milliseconds
max: 100, // max requests per window per IP
message: 'Too many requests, please try again later.',
});
app.use(limiter);
windowMs sets the time frame for counting requests.
max is how many requests are allowed in that time.
This simple Express server limits each user to 3 requests per minute. If they try more, they get a message telling them to wait.
Node.js
import express from'express';
import rateLimit from'express-rate-limit';
const app = express();
const limiter = rateLimit({
windowMs: 60000, // 1 minute
max: 3, // limit each IP to 3 requests per windowMs
message: 'Too many requests, please wait a minute.',
});
app.use(limiter);
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
OutputSuccess
Important Notes
Rate limiting works best with user IP addresses to track requests.
Adjust windowMs and max to fit your app's needs.
Use custom messages to inform users politely when they hit the limit.
Summary
Rate limiting protects your app from too many requests.
It controls traffic by setting request limits per time window.
Easy to add in Node.js apps using middleware like express-rate-limit.
Practice
(1/5)
1. What is the main purpose of rate limiting in a Node.js application?
easy
A. To store user data securely
B. To speed up the server response time
C. To control the number of requests a user can make in a set time
D. To automatically restart the server on errors
Solution
Step 1: Understand rate limiting concept
Rate limiting restricts how many requests a user can send to prevent overload.
Step 2: Identify the main goal in Node.js apps
It protects the app by limiting requests per time window, avoiding abuse.
Final Answer:
To control the number of requests a user can make in a set time -> Option C
Quick Check:
Rate limiting = request control [OK]
Hint: Rate limiting controls request count per time window [OK]
Common Mistakes:
Confusing rate limiting with speeding up server
Thinking it stores user data
Assuming it restarts the server automatically
2. Which of the following is the correct way to import the express-rate-limit middleware in a Node.js app?
easy
A. require rateLimit from 'express-rate-limit';
B. const rateLimit = require('express-rate-limit');
C. const rateLimit = import('express-rate-limit');
D. import rateLimit from 'express-rate-limit';
Solution
Step 1: Identify import syntax in Node.js
Node.js commonly uses CommonJS syntax with const rateLimit = require('express-rate-limit'); unless using ES modules.
Step 2: Check each option for syntax correctness
const rateLimit = require('express-rate-limit'); uses correct CommonJS require syntax; import rateLimit from 'express-rate-limit'; is ES module syntax which requires specific setup.
Final Answer:
const rateLimit = require('express-rate-limit'); -> Option B
windowMs expects a number representing milliseconds, not a string.
Step 2: Identify the error in the code
windowMs is given as 'one minute' (string), which causes incorrect behavior or error.
Final Answer:
windowMs should be a number, not a string -> Option A
Quick Check:
windowMs type = number [OK]
Hint: windowMs must be a number, not quoted string [OK]
Common Mistakes:
Passing windowMs as string
Confusing max type
Thinking app.use can't use middleware
5. You want to apply rate limiting only to API routes starting with /api/ in your Express app. Which code snippet correctly applies the limiter only to those routes?
hard
A. app.use('/api/', limiter);
B. app.use(limiter);
C. app.use('/api/*', limiter);
D. app.use('/api', limiter);
Solution
Step 1: Understand Express route prefix matching
Using app.use('/api', limiter); applies middleware to all routes starting with '/api'.
Step 2: Evaluate other options
app.use('/api', limiter); correctly applies limiter to all routes starting with '/api'. app.use('/api/', limiter); applies only to routes starting with '/api/' (with trailing slash), which may exclude '/api' itself.
Final Answer:
app.use('/api', limiter); -> Option D
Quick Check:
Prefix '/api' applies limiter correctly [OK]
Hint: Use app.use('/api', limiter) for route-specific limiting [OK]
Common Mistakes:
Applying limiter globally by mistake
Using invalid route patterns
Confusing trailing slash effects
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.