npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodejs-redis-rate-limitter

v1.0.2

Published

`nodejs-redis-rate-limitter` is a middleware library for rate limiting in Express applications using Redis as the storage backend. It allows you to control the rate of incoming requests from clients and protect your server from excessive traffic.

Downloads

6

Readme

nodejs-redis-rate-limitter

nodejs-redis-rate-limitter is a middleware library for rate limiting in Express applications using Redis as the storage backend. It allows you to control the rate of incoming requests from clients and protect your server from excessive traffic.

Installation

Install the package via npm:

npm install nodejs-redis-rate-limitter

Usage

To use nodejs-redis-rate-limitter in your Express application, follow these steps:

  1. Import the necessary modules:

    import express from "express";
    import { RateLimiter } from "nodejs-redis-rate-limitter";
    import { createClient } from "redis"; // For node-redis
    import ioredis from "ioredis"; // For ioredis
  2. Create an instance of the Redis client and connect to the Redis server:

    // For node-redis
    const client = createClient();
    await client.connect();
    
    // For ioredis
    const client = new ioredis();
  3. Configure and create the rate limiter middleware:

    3.1 Creating a rate limiter using node-redis:

    const limiter = new RateLimiter({
      expiresIn: 3600, // Rate limiter will expire after 3600 seconds (1 hour)
      key: (req) => req.ip, // Use the IP address of the request as the key to identify the client
      max: 300, // Maximum number of requests allowed per client within the defined duration
      store: (...args) => client.sendCommand(args), // A callback function to execute Redis commands for storing and retrieving information about the client in the rate limiter
      message: "You have exceeded the maximum number of requests.", // Optional message to send when rate limit is exceeded
      algorithm: "token-bucket", // Select the rate limiting algorithm (optional, defaults to "token-bucket")
    });

    3.2 Creating a rate limiter using ioredis:

    const limiter = new RateLimiter({
      expiresIn: 3600, // Rate limiter will expire after 3600 seconds (1 hour)
      key: (req) => req.ip, // Use the IP address of the request as the key to identify the client
      max: 300, // Maximum number of requests allowed per client within the defined duration
      store: (...args) => client.call(...args), // A callback function to execute Redis commands for storing and retrieving information about the client in the rate limiter
      message: "You have exceeded the maximum number of requests.", // Optional message to send when rate limit is exceeded
      algorithm: "fixed-window", // Select the rate limiting algorithm (optional, defaults to "token-bucket")
    });
  4. Apply the rate limiter middleware to your Express application:

    const app = express();
    
    app.use(limiter);
    
    // Define your routes and application logic here...
    
    app.listen(3000, () => {
      console.log("Server is running on port 3000");
    });

Rate Limiter Options

The following options are available when configuring the rate limiter:

  • expiresIn (number): The expiration time of the rate limiter in milliseconds.
  • key ((req: Request) => string): A function that returns the key to identify the client in the rate limiter storage.
  • max (number): The maximum number of requests allowed per client within the defined duration.
  • store ((...args: string[]) => any): A callback function to execute Redis commands for storing and retrieving information about the client in the rate limiter.
  • message (string, optional): An optional message to send along with the error response when the rate limit is exceeded.
  • algorithm (string, optional): The algorithm used for rate limiting. Supported values are: "token-bucket", "fixed-window", "sliding-window", "leaky-bucket", "sliding-log".

Rate Limiting Algorithms

  • Token Bucket: Distributes a fixed number of tokens at a constant rate.
  • Fixed Window: Resets the request count at regular intervals.
  • Sliding Window: Tracks the request count over a sliding time window.
  • Leaky Bucket: Empties the bucket at a constant rate, allowing bursts of requests.
  • Sliding Log: Tracks requests using a logarithmic sliding window.

Example: Applying Rate Limiter to a Specific Route

You can also apply the rate limiter middleware to specific routes. Here's an example of applying the rate limiter to a login route:

const loginLimiter = new RateLimiter({
  expiresIn: 3600,
  key: (req) => req.ip + req.originalUrl,
  max: 15,
  store: (...args) => client.sendCommand(args),
  message: "You have reached the maximum number of login attempts.", // Optional message to send when rate limit is exceeded
});

app.post("/login", loginLimiter.middleware(), (req, res) => {
  // Route logic
});

Response Headers

When using nodejs-redis-rate-limitter, several custom headers can be added to the response to provide information about the rate limit. These headers can be used by the client to understand the rate limiting status and adjust their requests accordingly.

  • X-Rate-Limit-Limit: Represents the maximum number of requests allowed per client within the defined duration (max value from the rate limiter options).
  • X-Rate-Limit-Remaining: Indicates the remaining number of requests that the client can make within the defined duration. If the value is negative, it means the client has exceeded the rate limit and no more requests are allowed.
  • X-Rate-Limit-Duration: Specifies the total duration of the rate limit in seconds. It represents the length of time for which the rate limit is set.