redis-rate-limit-express
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

redis-rate-limit-express

redis-rate-limit-express 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 redis-rate-limit-express

Usage

To use redis-rate-limit-express in your Express application, follow these steps:

  1. Import the necessary modules:

    import express from "express";
    import rateLimiter from "redis-rate-limit-express";
    import ioredis from "ioredis";
  2. Create an instance of the Redis client:
    2.1 Using ioredis

    const client = ioredis.createClient();

    2.2 Using node-redis

    const client = ioredis.createClient();
    await client.connect();
  3. Configure and create the rate limiter middleware:

    3.1 Creating an rate limiter usign ioredis

    const limiter = rateLimiter({
      expiresIn: 60 * 60, // 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
      // @ts-expect-error - Avoid ts errors when using ioredis libary
      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
    });

    3.2 Creating an rate limiter usign ioredis

    const limiter = rateLimiter({
      expiresIn: 60 * 60, // 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
    });
  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.

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 = rateLimiter({
  expiresIn: 60 * 60,
  key: (req) => req.ip + req.originalUrl,
  max: 15,
  // With ioredis
  // @ts-expect-error - Avoid ts errors when using ioredis libary
  store: (...args) => client.call(...args),
  message: "You have reached the maximum number of login attempts.", // Optional message to send when rate limit is exceeded
});

app.post("/login", loginLimiter, (req, res

Resetting the Rate Limiter

You can reset the rate limiter for a specific client by accessing the resetKey function within the request object. Here's an example of resetting the rate limiter for a /reset route:

app.post("/reset", limiter, async (req, res) => {
  await req.resetKey(req.ip);
  res.send("Counter reset!");
});

Response Headers

When using redis-rate-limit-express, 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.

Readme

Keywords

none

Package Sidebar

Install

npm i redis-rate-limit-express

Weekly Downloads

1

Version

1.0.5

License

ISC

Unpacked Size

9.78 kB

Total Files

6

Last publish

Collaborators

  • luccazvps