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

@wah33d/rate-limiter-middleware

v1.0.1

Published

A rate limiting middleware for Express.js with support for in-memory and Redis stores

Downloads

122

Readme

rate-limiter-middleware

A simple and flexible rate-limiting middleware for Node.js that supports both Redis and in-memory storage. This package can be used to limit the number of requests made by users within a certain time frame, preventing abuse and protecting your application from being overwhelmed by traffic.

Features

  • Redis-based rate limiting (for distributed systems)
  • In-memory rate limiting (for local environments)
  • Customizable limit and window duration
  • Middleware integration with Express.js
  • Easy setup and usage

Installation

Install the package via npm:

npm install @wah33d/rate-limiter-middleware

Usage

You can use the middleware in two modes: Redis and In-memory.

Example File Structure

.
├── src
│   ├── middleware
│   │   └── rateLimiter.ts
│   └── server.ts
└── example
    └── exampleServer.ts

1. In-memory rate limiting

// src/middleware/rateLimiter.ts
import rateLimiter from "@wah33d/rate-limiter-middleware";

const limiter = rateLimiter({
  limit: 100, // Limit of requests
  window: 60 * 1000, // Time window in milliseconds (1 minute)
});

export default limiter;

// src/server.ts
import express from "express";
import limiter from "./middleware/rateLimiter";

const app = express();

app.use(limiter); // Apply rate limiting middleware to all routes

app.get("/", (req, res) => {
  res.send("Welcome! You are within rate limits.");
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

2. Redis-based rate limiting

For Redis-based rate limiting, make sure you have Redis installed and running. You will also need the redis package.

npm install redis
// src/middleware/rateLimiter.ts
import rateLimiter from "@wah33d/rate-limiter-middleware";
import Redis from "redis";

// Initialize Redis client
const redisClient = Redis.createClient({
  url: "redis://localhost:6379",
});

const limiter = rateLimiter({
  limit: 100, // Limit of requests
  window: 60 * 1000, // Time window in milliseconds (1 minute)
  redisClient, // Redis client for distributed rate limiting
});

export default limiter;

3. Example Application

// example/exampleServer.ts
import express from "express";
import limiter from "../src/middleware/rateLimiter";

const app = express();

// Apply rate limiter middleware
app.use(limiter);

app.get("/", (req, res) => {
  res.send("Rate limit applied. Welcome!");
});

app.listen(3000, () => {
  console.log("Example server running on port 3000");
});

Configuration

The rateLimiter function accepts the following options:

| Option | Type | Default | Description | | ------------- | ------------- | ---------------------------------------------- | --------------------------------------------------------- | | limit | number | 60 | Maximum number of requests allowed within the time window | | window | number | 60000 | Time window in milliseconds (default is 60 seconds) | | redisClient | RedisClient | null | An optional Redis client for distributed rate limiting | | message | string | "Too many requests, please try again later." | Custom message for when the limit is exceeded |

In-Memory Example:

const limiter = rateLimiter({
  limit: 50, // 50 requests
  window: 30000, // 30 seconds
});

Redis Example:

const limiter = rateLimiter({
  limit: 100, // 100 requests
  window: 60 * 1000, // 1 minute
  redisClient, // Redis client for distributed storage
});

Custom Error Message

You can specify a custom error message when the request limit is exceeded:

const limiter = rateLimiter({
  limit: 100,
  window: 60000,
  message: "Too many requests from this IP, please try again after 1 minute.",
});

Redis Configuration

If you use Redis for distributed rate limiting, ensure your Redis server is running and configured properly. You can pass the redisClient as part of the options.

import Redis from "redis";

const redisClient = Redis.createClient({
  url: "redis://localhost:6379",
});

const limiter = rateLimiter({
  limit: 100,
  window: 60000,
  redisClient, // Pass the Redis client
});

Example Application

To run the example server:

  1. Clone the repository
  2. Install dependencies:
npm install
  1. Navigate to the example folder:
cd example
  1. Run the example server:
node exampleServer.js

License

This project is licensed under the MIT License.


This `README.md` provides details on installation, configuration, and examples for using your `rate-limiter-middleware` package. You can further adjust it to match your specific use cases!