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 🙏

© 2025 – Pkg Stats / Ryan Hefner

dynamic-rate-limiter-middleware

v1.0.2

Published

Intelligent rate limiting middleware with MongoDB integration and caching for Node.js applications

Downloads

6

Readme

Dynamic Rate Limiter Middleware

An intelligent rate-limiting and caching middleware for Node.js applications that helps manage API calls to external services with rate limits. This middleware provides smart scheduling, caching, and monitoring capabilities to optimize API usage.

Features

  • 🚀 Intelligent rate limiting based on daily limits
  • 💾 Built-in caching system with configurable TTL
  • 📊 Traffic pattern analysis and smart scheduling
  • 🔄 Automatic retry mechanism with exponential backoff
  • 📈 Flexible MongoDB integration
  • ⚡ In-memory caching for fast response times
  • 🎯 Configurable for multiple APIs and endpoints
  • 📝 Detailed logging and monitoring

Installation

npm install dynamic-rate-limiter-middleware

Quick Start

const express = require('express');
const RateLimiterMiddleware = require('dynamic-rate-limiter-middleware');

const app = express();

// Initialize middleware with custom MongoDB configuration
const rateLimiter = await RateLimiterMiddleware({
    maxHitsPerDay: 500,
    baseUrl: 'https://api.example.com',
    cacheTTL: 3600, // 1 hour in seconds
    mongodb: {
        url: 'mongodb://your-mongodb-url',
        dbName: 'your-database-name',
        options: {
            // Additional MongoDB options
            user: 'username',
            pass: 'password'
        }
    }
});

// Apply middleware to specific routes
app.use('/api', rateLimiter);

MongoDB Configuration

The middleware supports flexible MongoDB configuration:

// Using environment variables (recommended)
const rateLimiter = await RateLimiterMiddleware({
    mongodb: {
        url: process.env.MONGODB_URI,
        dbName: process.env.MONGODB_DB
    }
});

// Using direct configuration
const rateLimiter = await RateLimiterMiddleware({
    mongodb: {
        url: 'mongodb://localhost:27017',
        dbName: 'apiHits',
        options: {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            user: 'username',
            pass: 'password'
        }
    }
});

// Using existing mongoose connection
const mongoose = require('mongoose');
await mongoose.connect('mongodb://your-mongodb-url/dbname');
const rateLimiter = await RateLimiterMiddleware();

Configuration Options

const options = {
    // Rate limiting
    maxHitsPerDay: 500,
    
    // External API configuration
    baseUrl: 'https://api.example.com',
    
    // Caching
    cacheTTL: 3600, // seconds
    
    // MongoDB configuration
    mongodb: {
        url: 'mongodb://localhost:27017',
        dbName: 'apiHits',
        options: {
            useNewUrlParser: true,
            useUnifiedTopology: true
        }
    },
    
    // Traffic patterns
    trafficPatterns: {
        peakHours: {
            start: 9,  // 9 AM
            end: 17    // 5 PM
        },
        hitDistribution: {
            peak: 0.7,    // 70% of hits during peak hours
            offPeak: 0.3  // 30% during off-peak
        }
    },
    
    // Retry configuration
    retryAttempts: 3,
    retryDelay: 1000 // milliseconds
};

Environment Variables

The middleware supports the following environment variables:

MONGODB_URI=mongodb://localhost:27017
MONGODB_DB=apiHits

Advanced Usage

Manual MongoDB Initialization

const { initializeMongoDB } = require('dynamic-rate-limiter-middleware');

// Initialize MongoDB connection manually
await initializeMongoDB({
    url: 'mongodb://localhost:27017',
    dbName: 'customDb',
    options: {
        // Your MongoDB options
    }
});

Custom Cache Implementation

const { CacheManager } = require('dynamic-rate-limiter-middleware');

const cache = new CacheManager({
    cacheTTL: 1800, // 30 minutes
});

Traffic Pattern Analysis

const { HitScheduler } = require('dynamic-rate-limiter-middleware');

const scheduler = new HitScheduler({
    maxHitsPerDay: 1000,
    trafficPatterns: {
        peakHours: { start: 8, end: 18 }
    }
});

Error Handling

The middleware handles various error scenarios:

try {
    const rateLimiter = await RateLimiterMiddleware({
        mongodb: {
            url: 'invalid-url'
        }
    });
} catch (error) {
    console.error('Failed to initialize middleware:', error);
}

Best Practices

  1. Use environment variables for MongoDB credentials
  2. Set appropriate rate limits based on your API provider's constraints
  3. Configure cache TTL based on data freshness requirements
  4. Monitor analytics to optimize traffic patterns
  5. Implement proper error handling in your application

License

MIT License - see LICENSE file for details