express-rate-limit
Rate limiting middleware for Express that caps requests per IP or key with standard headers and custom handlers.
Official Documentation ↗v8.7.3. Check the official docs for the latest API surface.Installation
npm install express-rate-limitpnpm add express-rate-limitMethods
No methods match this filter.
Rate limiting middleware for Express that caps requests per IP or key with standard headers and custom handlers.
Overview
express-rate-limit is the standard middleware for capping request rates in Express apps. Each limiter owns a window, a limit, and a key; requests over the limit receive a 429 with standard headers describing remaining quota and retry timing.
Keys default to the client IP but can become users or tenants through keyGenerator, and separate limiters can guard login, API, and health routes independently. Everything is configurable through handler, skip, and store hooks, which is where teams add observability and distributed counters.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
rateLimit— Creates middleware counting requests per key inside a rolling window and rejecting the excess with 429.keyGenerator— Derives the bucket key per request; defaulting to the IP, it can switch to user ID or API tenant.handler— Replaces the default 429 response for rate-limited requests, for logging or custom error envelopes.skip / validate— Exempts requests such as health checks, and warns about proxy header trust misconfiguration at startup.
Usage Notes
- Mount different limiters per route class; a global limit that fits page views starves API bursts.
- Emit the standard headers so well-behaved clients can self-throttle using Retry-After.
- Combine with slowDown (express-slow-down) for progressive delay on sensitive endpoints before hard rejection.
Common Pitfalls
- In-memory counters reset on deploy and diverge across replicas; treat that as acceptable noise only for abuse protection.
- Keying by raw req.socket address behind a proxy collapses all traffic into one bucket and trips the limiter for everyone.
- Exempting localhost or internal ranges for convenience creates an unauthenticated bypass if those ranges are spoofable.
Security notes
Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/express-rate-limit/express-rate-limit@latest/readme.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:30.406Z.
- Use to limit repeated requests to public APIs and/or endpoints such as password reset. (https://cdn.jsdelivr.net/gh/express-rate-limit/express-rate-limit@latest/readme.md)
FAQ
- Why is every visitor counted once behind a reverse proxy?
- Without trust proxy, req.ip is the proxy address, so all clients share one bucket. Set app.set("trust proxy", ...) to the hop count or provider range so req.ip reflects the real client, and watch the validate warnings.
- How do I share limits across multiple server instances?
- The default MemoryStore is per-process. Use a store package backed by Redis, such as rate-limit-redis, passed through store, so all instances count against the same buckets.
- What changed between v6 and v7?
- v7 renamed the max option to limit, defaults standardHeaders to draft-6 rate limit headers, and removed deprecated behaviors. Update option names when upgrading; old max values silently stop applying.
Report a problem with this API docOpens a pre-filled issue on GitHub.