Skip to content

About

Full-stack MERN API Rate Limiter with sliding-window rate limiting, JWT-based user identification, configurable IP/domain/user rules, breach notifications, admin dashboard, and automated testing.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ API Rate Limiter

A MERN Stack Application for API Rate Limiting & Traffic Monitoring

Configure Limits • Monitor Requests • Detect Breaches • Test APIs

React Node.js Express MongoDB Jest


📖 Overview

API Rate Limiter is a full-stack MERN application that controls how frequently clients can access an API. It allows administrators to configure request limits, monitor violations, and analyze API activity through an interactive dashboard.

The application uses a Sliding Window Counter algorithm to track requests within a specified time period. When a client exceeds the configured limit, the system can reject further requests with an HTTP 429 Too Many Requests response.

✨ Features

  • ⚙️ Configurable Rate Limits — Define request limits by IP address, domain, or user identity and by minute, hour, or day.
  • 🚦 Sliding Window Counter — Reduce boundary-burst issues associated with fixed-window rate limiting.
  • 📊 Admin Dashboard — Monitor request statistics and rate-limit breach activity.
  • 📝 Breach Logs — Record and review requests that exceed configured limits.
  • 🔔 Notifications — Display in-app alerts and optionally send email notifications through SMTP.
  • 🧪 Interactive API Tester — Generate requests and observe rate-limit enforcement in action.
  • 🗄️ Automatic Counter Cleanup — Use MongoDB TTL indexes to expire eligible request-counter documents.
  • ✅ Automated Testing — Includes 65 documented unit and integration tests using Jest and Supertest.

🏗️ How It Works

       Client Requests
              │
              ▼
      ┌───────────────┐
      │ Express Server│
      └───────┬───────┘
              │
              ▼
      ┌───────────────┐
      │ Rate Limiter  │
      │   Middleware  │
      └───────┬───────┘
              │
              ▼
      ┌───────────────┐
      │ Check Rules & │
      │ Request Count │
      └───────┬───────┘
              │
        ┌─────┴─────┐
        ▼           ▼
   Within Limit   Limit Exceeded
        │           │
        ▼           ▼
   Allow Request  HTTP 429
                    │
                    ▼
             Breach Log &
             Notification

🛠️ Tech Stack

Component Technology
Frontend React 18, Vite, React Router, Axios
Backend Node.js, Express.js
Database MongoDB, Mongoose
Testing Jest, Supertest, mongodb-memory-server
Email Notifications Nodemailer, SMTP
Version Control Git & GitHub

⚙️ Installation & Setup

Prerequisites

  • Node.js 18 or higher
  • npm
  • MongoDB running locally or an accessible MongoDB instance

1️⃣ Clone the Repository

git clone https://github.com/baraiyash/API-Rate-Limiter.git
cd API-Rate-Limiter

2️⃣ Install Dependencies

npm run install:all

3️⃣ Configure Environment Variables

Create a .env file inside the server/ directory:

PORT=5000
MONGODB_URI=mongodb://localhost:27017/rateLimiter

# Optional: Email notifications
SMTP_HOST=
SMTP_PORT=587
SMTP_USER=
SMTP_PASS=
ADMIN_EMAIL=[email protected]

4️⃣ Start the Application

npm run dev

Open the frontend URL displayed in the terminal. The expected URL is http://localhost:3000 if Vite is configured to use that port.


🚦 Example Usage

Suppose an administrator configures a rule allowing 10 requests per minute per IP address.

  1. Create the rate-limit rule from the dashboard.
  2. Open the API Tester and send multiple requests.
  3. Observe successful responses while requests remain within the configured limit.
  4. Exceed the limit and observe HTTP 429 responses.
  5. Review the recorded violations in Breach Logs.
  6. Check in-app notifications or configure SMTP for email alerts.

🔌 API Endpoints

Method Endpoint Description
GET /api/rules Retrieve rate-limit rules
POST /api/rules Create a rule
PUT /api/rules/:id Update a rule
DELETE /api/rules/:id Delete a rule
GET /api/test Test rate-limit enforcement
GET /api/breaches Retrieve breach logs
DELETE /api/breaches Clear breach logs
GET /api/notifications Retrieve notifications
PATCH /api/notifications/:id/read Mark a notification as read
PATCH /api/notifications/read-all Mark all notifications as read
GET /api/stats Retrieve dashboard statistics
GET /api/health Check server health

🧪 Automated Testing

The project includes unit and integration tests covering utility functions, rule management, rate-limiting middleware, and notifications.

Test Suite Tests
helpers.test.js 17
rules.test.js 16
rateLimiter.test.js 21
notification.test.js 11
Total 65

Run the test suite:

npm test

Generate a test coverage report:

npm run test:coverage

The counts reflect the documented test suite. Run the tests to verify the current results.

🧠 Key Technical Decisions

  • Sliding Window Counter: Helps reduce sudden request bursts around time-window boundaries.
  • Atomic Counter Updates: Uses MongoDB's findOneAndUpdate and $inc for atomic counter increments.
  • TTL Indexes: Automatically remove eligible expired counter documents.
  • Fail-Open Strategy: Allows requests to continue when the database is unavailable, prioritizing availability over strict rate-limit enforcement.
  • Notification Cooldown: Uses a five-minute cooldown to reduce repetitive alerts during sustained breaches.

🤖 AI-Assisted Development

AI tools, including Google Antigravity and Claude, assisted with requirement analysis, architecture planning, code generation, test design, documentation, and troubleshooting.

🧠 Key Learnings

  • REST API development using Express.js
  • Middleware-based request processing
  • Rate-limiting algorithms and request tracking
  • MongoDB data modeling and atomic operations
  • TTL-based data expiration
  • HTTP status codes and error handling
  • Unit and integration testing

👨‍💻 Author

Yash Barai


⭐ If you find this project interesting, consider giving the repository a star!

About

Full-stack MERN API Rate Limiter with sliding-window rate limiting, JWT-based user identification, configurable IP/domain/user rules, breach notifications, admin dashboard, and automated testing.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages