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

@lbanc/express-rate-limiter

v1.0.0

Published

A customizable and flexible middleware for Express.js to limit repeated requests to your API endpoints.

Downloads

5

Readme

Express-rate-limiter

A customizable and flexible middleware for Express.js to limit repeated requests to your API endpoints. Supports memory-based or Redis-based storage to keep track of request counts, providing a way to rate-limit clients per IP or other identifiers.

Features

  • Configurable rate limits per window (time window in milliseconds).
  • Multiple storage options (in-memory or Redis).
  • Customizable messages and responses when rate limits are exceeded.
  • Includes headers for rate-limit status (X-RateLimit-Limit, X-RateLimit-Remaining).

Installation

npm install express-rate-limiter

If you want to use Redis for storage, you’ll need to install the Redis client as well:

npm install redis

Usage

Basic Usage with In-Memory Store

Here’s how to set up the rate limiter using the default in-memory store.

import express from 'express';
import RateLimiter from 'express-rate-limiter';

const app = express();

const rateLimiter = new RateLimiter({
    windowMs: 60000,  // 1 minute window
    max: 100,  // Limit each IP to 100 requests per windowMs
});

app.use(rateLimiter.limit());

app.get('/', (req, res) => {
    res.send('Hello, World!');
});

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

Using Redis for Storage

If you’re running multiple instances of your app or want to persist the rate-limit data, you can use Redis as the storage backend.

import express from 'express';
import RateLimiter from 'express-rate-limiter';
import RedisStore from 'express-rate-limiter/lib/redisStore.js';
import redis from 'redis';

// Set up Redis client
const redisClient = redis.createClient();

const rateLimiter = new RateLimiter({
    windowMs: 60000,  // 1 minute window
    max: 100,  // Limit each IP to 100 requests per windowMs
    store: new RedisStore(redisClient),  // Use Redis store
});

const app = express();

app.use(rateLimiter.limit());

app.get('/', (req, res) => {
    res.send('Hello, World!');
});

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

Configuration Options

  • windowMs: Duration of the rate-limiting window in milliseconds (default: 60000 ms or 1 minute).
  • max: Maximum number of requests allowed per window per IP (default: 100).
  • store: Storage backend to use for keeping track of request counts. By default, it uses in-memory storage, but you can use Redis by providing a RedisStore.
  • message: Custom message to send when the rate limit is exceeded (default: "Too many requests, please try again later.").
  • headers: If true, includes X-RateLimit-Limit and X-RateLimit-Remaining headers in responses.

Example of Custom Response

You can customize the response message when the rate limit is exceeded.

const rateLimiter = new RateLimiter({
    windowMs: 60000,
    max: 5,
    message: 'You have exceeded the number of requests allowed. Please wait before trying again.'
});

License

MIT License

This version is fully ready for publishing in plain text and uses code blocks for clarity. Let me know if you'd like any further modifications!