DEV Community

Shyam Tala
Shyam Tala

Posted on

šŸš€ Introducing rate-bouncer: A Powerful Rate Limiting Middleware for Node.js

Introduction

In today's fast-paced digital world, APIs are the backbone of web applications, enabling seamless data exchange. However, APIs are vulnerable to abuse, such as Denial-of-Service (DoS) attacks, excessive bot traffic, and accidental request floods. To combat these challenges, rate limiting is essential. This is where rate-bouncer comes into play! šŸŽÆ

rate-bouncer is a lightweight and flexible rate-limiting middleware for Node.js, designed to protect your API from excessive requests while ensuring legitimate users have a smooth experience.

Why Use rate-bouncer? šŸ¤”

āœ… Prevents API Abuse: Stops malicious bots and excessive traffic from overwhelming your server.
āœ… Customizable: Offers global and per-route configurations, allowing fine-tuned control.
āœ… Lightweight & Fast: Minimal footprint with in-memory storage for quick lookups.
āœ… Easy Integration: Works effortlessly with Express and similar frameworks.
āœ… Automatic Cleanup: Optimizes memory usage by periodically removing old request data.

Getting Started šŸš€

Installation

You can install rate-bouncer using npm:

npm install rate-bouncer
Enter fullscreen mode Exit fullscreen mode

Basic Usage

Setting up rate-bouncer in an Express app is straightforward:

const express = require("express");
const { setGlobalRateLimitConfig, rateLimitConfig } = require("rate-bouncer");

const app = express();

// Set global rate limit (applies to all routes unless overridden)
setGlobalRateLimitConfig({
  duration: 15 * 60 * 1000, // 15 minutes
  maxRequests: 100, // Max 100 requests per 15 minutes
  startCleanupInterval: 50000, // Cleanup interval (optional)
});

// Apply rate limiter globally
app.use(rateLimitConfig());

app.get("/api/data", (req, res) => {
  res.send("This route is protected by rate limiting.");
});

app.listen(3000, () => console.log("Server running on port 3000"));
Enter fullscreen mode Exit fullscreen mode

Advanced Configuration āš™ļø

Per-Route Customization

Override global settings for specific routes when needed:

app.get(
  "/api/special",
  rateLimitConfig({ duration: 10 * 60 * 1000, maxRequests: 50 }),
  (req, res) => {
    res.send("Limited to 50 requests per 10 minutes.");
  }
);
Enter fullscreen mode Exit fullscreen mode

Disabling Rate Limiting

Need to exclude certain routes? Simply disable rate limiting:

app.get("/api/open", rateLimitConfig({ disabled: true }), (req, res) => {
  res.send("This route has no rate limit.");
});
Enter fullscreen mode Exit fullscreen mode

Handling Exceeded Limits āš ļø

When a user exceeds the allowed request count, they receive a 429 Too Many Requests response:

{
  "message": "Too many requests",
  "retryAfter": "10.0 seconds"
}
Enter fullscreen mode Exit fullscreen mode

Best Practices for Using rate-bouncer šŸ”„

  • Choose the Right Limits: Set appropriate request limits based on your API's needs.
  • Use Different Limits for Different Routes: Critical routes may need stricter limits than public endpoints.
  • Combine with Authentication: Rate limiting works best when combined with authentication and IP-based restrictions.
  • Monitor Traffic: Regularly check logs to fine-tune rate limits based on real-world usage.

Conclusion šŸŽÆ

Rate limiting is a crucial defense against API abuse and traffic overloads. rate-bouncer makes it incredibly easy to implement rate limiting in Node.js applications with minimal setup and maximum flexibility. By integrating this lightweight middleware, you can protect your API, enhance security, and provide a smooth user experience.

Give rate-bouncer a try today! šŸš€

šŸ‘‰ Check it out on GitHub: rate-bouncer

Happy coding! šŸ’»šŸ”„

Top comments (0)