การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี
ป้องกัน API ของ Node.js จากการใช้งานในทางที่ผิด การโจมตีปฏิเสธการให้บริการ และการโจมตีด้วยข้อมูลรับรองที่ขโมยมา โดยใช้การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี
การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Limit Requests?
Without limits, a single client can hammer your API thousands of times per second — scraping data, guessing passwords, or simply overloading the server.
Rate limiting caps how many requests a client may make in a time window.
Attacks Rate Limiting Prevents
Rate limiting is a frontline defense against:
- Brute-force login attempts
- Credential stuffing with leaked passwords
- Denial-of-service floods
- Scraping and API abuse
How Counting Works
A rate limiter tracks a counter per client (usually keyed by IP). Each request increments it; when the count exceeds the limit within the window, further requests are rejected with 429 Too Many Requests.
express-rate-limit
The express-rate-limit package adds rate limiting as middleware in a few lines. Configure the window and max requests.
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100
});Applying the Limiter
Apply globally with app.use, or to specific routes. Once over the limit, clients automatically receive a 429 response.
app.use(limiter);
// or just protect one route:
app.use('/api/', limiter);Stricter Limits on Login
Login endpoints are prime brute-force targets, so give them a tighter limit than the rest of your API.
const loginLimiter = rateLimit({
windowMs: 10 * 60 * 1000,
max: 5,
message: 'Too many login attempts'
});
app.post('/login', loginLimiter, handler);Shared Store for Multiple Servers
The default in-memory store does not work when you run multiple instances behind a load balancer — each has its own counter. Use a shared store like Redis so limits apply across all servers.
const RedisStore = require('rate-limit-redis');
const limiter = rateLimit({
store: new RedisStore({ /* client */ }),
max: 100,
windowMs: 60000
});Trusting the Real Client IP
Behind a proxy, every request appears to come from the proxy's IP. Tell Express to trust the proxy so the limiter keys on the real client IP from X-Forwarded-For.
app.set('trust proxy', 1);Account Lockout
Beyond IP limits, track failed logins per account. After several failures, temporarily lock the account or require a CAPTCHA — defeating distributed brute-force from many IPs.
if (user.failedAttempts >= 5) {
return res.status(423).json({ error: 'Account locked' });
}Slowing Down Instead of Blocking
An alternative to hard blocks is progressive delay: each repeated request waits a little longer. The express-slow-down package adds latency rather than rejecting outright.
const slowDown = require('express-slow-down');
const speedLimiter = slowDown({
windowMs: 60000,
delayAfter: 50,
delayMs: () => 500
});Informing Clients
Good limiters send RateLimit headers telling clients their remaining quota and reset time, so well-behaved apps can back off gracefully.
const limiter = rateLimit({
max: 100,
windowMs: 60000,
standardHeaders: true
});Quick Check
Test your rate-limiting knowledge.
Recap
You learned to protect APIs from abuse:
- Rate limiting caps requests per client and returns
429when exceeded express-rate-limitadds it as middleware; use stricter limits on login- Use a Redis store across multiple servers and set
trust proxyfor real IPs - Add account lockout, progressive slow-down, and informative headers
These layers thwart brute-force, scraping, and DoS attacks.
คำถามที่พบบ่อย
บทเรียน “การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี”
ป้องกัน API ของ Node.js จากการใช้งานในทางที่ผิด การโจมตีปฏิเสธการให้บริการ และการโจมตีด้วยข้อมูลรับรองที่ขโมยมา โดยใช้การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจ OWASP Top 10
- แนวทางการเขียนโค้ดที่ปลอดภัยใน Node.js
- การเข้ารหัสและการทำแฮชข้อมูล
- การจำกัดอัตราและการป้องกันการเดาแบบลองทุกกรณี