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

digi-rate-limiter

v1.1.0

Published

Custom rate limiting middleware for Node.js

Downloads

9

Readme

Rate Limiter

A customizable rate-limiting and throttling middleware for Node.js applications. It allows you to limit the number of requests a client can make to your server within a specified time window.

Features

  • Flexible request limiting based on time windows.
  • Easily configurable for IP-based or user-based rate limiting.
  • Simple integration as middleware in Node.js apps (e.g., with Express).
  • Supports both rate limiting and throttling.

Installation

You can install the package via npm:

npm install digi-rate-limiter

Usage

Basic Usage

The package can be used as middleware in your Node.js/Express applications to limit requests. Here's an example to allow 10 requests per second.

import express from 'express';
import rateLimiter from 'digi-rate-limiter';

const app = express();

// Apply rate limiter middleware
app.use(rateLimiter({
  windowMs: 1000,   // 1 second
  maxRequests: 10   // Limit each client to 10 requests per windowMs
}));

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

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

Options

The rateLimiter function accepts an options object to configure the behavior:

  • windowMs: The time frame (in milliseconds) to allow maxRequests. For example, 1000 will enforce the limit per second.
  • maxRequests: The maximum number of requests allowed during the windowMs period.

Example: 100 Requests per 15 Minutes

app.use(rateLimiter({
  windowMs: 15 * 60 * 1000,  // 15 minutes
  maxRequests: 100           // Limit each client to 100 requests per windowMs
}));

Advanced Features

IP-Based Limiting

By default, the middleware identifies clients by their IP address (req.ip in Express). This ensures that requests from each client IP are tracked separately.

User-Based Limiting

You can also implement user-based rate limiting by passing a user identifier (such as an API key or user ID) to the rate limiter.

app.use((req, res, next) => {
  const clientId = req.user.id; // Assuming you have user authentication
  rateLimiter({
    windowMs: 60000,   // 1 minute
    maxRequests: 30    // Limit to 30 requests per minute per user
  })(req, res, next);
});

Development

If you'd like to contribute or modify the package, you can clone the repository and run it locally:

git clone https://git.digimantra.com/abad_dml/rate-limiter
cd rate-limiter
npm install

Testing Locally

To run the Express example:

  1. Create a file app.js as shown in the example above.
  2. Run the server:
node app.js

Contributing

Contributions are welcome! Please open issues and submit pull requests on GitHub if you'd like to improve the package.

Issues

If you encounter any problems or have any questions, feel free to open an issue on GitHub.

Links