function rateLimitMiddleware
rateLimitMiddleware(options: RateLimitOptions): MiddlewareFunction

Rate limiting middleware factory.

On each request, increments the counter for the resolved key. If count > max, short-circuits with a 429 response (Retry-After and RateLimit-* headers set, next() NOT called). Otherwise sets the headers and proceeds to next().

Registered globally — the usage below, and the one the README shows — the limiter sees the operational probes too, so RateLimitOptions.exclude exempts them by default. Widen or narrow that list rather than dropping it: an exhausted bucket that refuses /live gets the container restarted.

Examples

Example 1

app.middleware.add(rateLimitMiddleware({
  windowMs: 60000,
  max: 100,
  // The six operational paths are exempt by default; add your own.
  exclude: [...DEFAULT_RATE_LIMIT_EXCLUDED_PATHS, /^\/internal\//],
}));

Parameters

Return Type

MiddlewareFunction

Usage

import { rateLimitMiddleware } from "auth-plugin/src/index.ts";