api-rate-limiter
Rate limiter package for Express server API requests
API ✍
api-rate-limiter API
init(...)
Parameter | Description | Required | Default |
---|---|---|---|
payload.client | RedisClient instance (promisified) | ✔ | ✘ |
payload.serverName | API server name | ✔ | ✘ |
Sample:
const RateLimiter =const Promise =const redis =Promiseconst redisClient = redisRateLimiter
RateLimiter should be initialized before initializing API routes.
Errors
Error name | Description |
---|---|
RateLimiterInvalidRedisClientError | Error thrown in case of invalid redis client |
RateLimiterInvalidServerNameError | Error thrown in case of invalid server name parameter |
preventRateLimitAbuse(...)
Parameter | Description | Required | Default |
---|---|---|---|
payload.maxNumberOfRequests | Maximum allowed requests per time window | ✔ | ✘ |
payload.rateLimitWindowInSeconds | Rate limit time window in seconds | ✘ | 300 seconds |
Sample:
const express =const router = expressconst RateLimiter =router
Errors
Error name | Description |
---|---|
RateLimitExceededError | Error passed to the next() middleware function in case of rate limit abuse |
RateLimiterMissingRedisClientError | Error thrown in case of missing redis client for rate limiter |
RateLimiterMissingServerNameError | Error thrown in case of missing server name for rate limiter |
HTTP Headers
Header | Description |
---|---|
X-RateLimit-Limit | Requests permitted to make per time window |
X-RateLimit-Remaining | Requests remaining in the current time window |
X-RateLimit-Reset | UNIX timestamp detailing when the rate limit will reset |
Sample:
X-RateLimit-Limit: 50
X-RateLimit-Remaining: 42
X-RateLimit-Reset: 1541169284467