2.1.2 • Published 6 months ago

@radioac7iv/rate-limiter v2.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Rate Limiter

A highly configurable rate-limiting middleware for Express.js applications.

Overview

The Rate Limiter is an open-source NPM package designed for managing request rates in Express.js applications. It helps prevent abuse and ensures fair usage of server resources through customizable rate-limiting logic.

Features

  • Highly configurable and flexible
  • Supports in-memory and custom stores
  • Automatic cleanup of expired rate data
  • Customizable headers and response messages
  • Optional logging for requests
  • Seamless integration with Express.js

Installation

npm i @radioac7iv/rate-limiter

Usage

Basic Example

import express from "express";
import { expressRateLimiter } from "@radioac7iv/rate-limiter";

const app = express();

const rateLimit = expressRateLimiter({
  key: (req, res) => req.ip as string,
  limitOptions: () => {
    return { max: 5, window: 10 };
  },
  headersType: "draft-7",
  logs: {
    directory: "./logs",
  },
});

app.use(rateLimit);

app.get("/", (request, response) => {
  response.status(200).send({ message: "Hello!" });
});

app.listen(3000, () => {
  console.log("Server started at PORT: 3000");
});

Configuration

The rate limiter can be configured using the following options:

OptionTypeDescriptionDefault
keyfunctionUnique key to identify each client (e.g., IP address or an API Key).Client's IP address
skipstring[]Keys to skip from rate-limiting.[]
skipFailedRequestsbooleanWhether to exclude failed requests from rate limits.false
messagestringCustom message when the rate limit is exceeded.Rate limit exceeded
statusCodenumberHTTP status code to send when the rate limit is exceeded.429
headersTypeHeadersTypeChoose between "legacy", "draft-6", "draft-7" or "draft-8"legacy
logsobjectConfiguration for logging requestsundefined
limitOptionsobjectFunction returning Object defining max (maximum requests) and window (time in seconds).None (required)
storeTypeStoreTypeChoose between "memory", "redis" or "mongodb"memory
externalStoreRedisClientType | DbExternal store instance for storing rate-limiting data.undefined
2.1.2

6 months ago

2.1.1

8 months ago

2.1.0

8 months ago

2.0.9

8 months ago

2.0.8

8 months ago

2.0.7

8 months ago

2.0.6

8 months ago

2.0.5

8 months ago

2.0.4

9 months ago

2.0.3

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.0.0

11 months ago