Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tobe01/micro-rate-limiter

Repository files navigation

micro-rate-limiter

A lightweight, dependency-free in-memory rate limiter for Node.js.

Ideal for small to medium projects where you need to control request frequency without pulling in heavy external packages or databases.


Features

  • In-memory rate limiting (no database required)
  • Works with Express.js
  • Easy to integrate and customize
  • Example project with browser test client

Related Article

You can read the full tutorial here:


Installation

Clone this repository:
git clone https://github.com/tobe01/micro-rate-limiter.git
cd micro-rate-limiter
npm install
---

Basic Setup

// server.js
const express = require('express');
const rateLimiter = require('../src/rateLimiter');
const app = express();
// Limit: 10 requests per 60 seconds per IP
app.use(rateLimiter({ windowMs: 60 * 1000, max: 10 }));
app.get('/', (req, res) => {
 res.json({ message: 'Hello, you are within the rate limit.' });
});
app.listen(3000, () => {
 console.log('Server running on http://localhost:3000');
});
---

Testing in Browser An HTML + CSS test client is included in the /example folder.

Start the server:

node example/server.js
Open example/test-limiter.html in your browser.
---

Click Start Test to fire multiple requests and see the limiter in action, including a live countdown until reset.

Folder Structure:

micro-rate-limiter/
│
├── src/
│ └── rateLimiter.js # Core rate limiter logic
│
├── example/
│ ├── server.js # Express server example
│ ├── test-limiter.html # Browser test page
│ └── styles.css # CSS for test page
│
├── package.json
└── README.md
---

Customization

The rateLimiter function accepts an options object:

Option Type Default Description windowMs Number 60000 Time window in milliseconds max Number 10 Max requests per IP within the window

Example:

javascript rateLimiter({ windowMs: 30000, max: 5 }) // 5 requests every 30 seconds
---

Free to use, modify, and share.

Made in Node.js with ❤️ by Tobechi

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /