node-rate-limiting
Version:
A lightweight, dependency-free rate limiter for Node.js using a sliding window
70 lines (45 loc) • 1.71 kB
Markdown
[](https://npmjs.com/package/node-rate-limiting)
A lightweight, dependency-free rate limiter for Node.js using a sliding window algorithm. Supports both CommonJS and ES Modules.
```bash
npm install node-rate-limiting
```
```javascript
const RateLimiter = require("node-rate-limiting");
// Create a rate limiter: max 5 actions per 60 seconds
const limiter = new RateLimiter({ limit: 5, windowMs: 60 * 1000 });
console.log(limiter.check("user1")); // true
console.log(limiter.check("user1")); // true
// ... after 5 calls
console.log(limiter.check("user1")); // false
limiter.reset("user1");
console.log(limiter.check("user1")); // true
```
```javascript
import RateLimiter from "node-rate-limiting";
// Create a rate limiter: max 5 actions per 60 seconds
const limiter = new RateLimiter({ limit: 5, windowMs: 60 * 1000 });
console.log(limiter.check("user1")); // true
console.log(limiter.check("user1")); // true
// ... after 5 calls
console.log(limiter.check("user1")); // false
limiter.reset("user1");
console.log(limiter.check("user1")); // true
```
- **limit**: Maximum number of actions allowed in the time window.
- **windowMs**: Time window in milliseconds.
- **identifier**: A string to identify the entity (e.g., user ID, IP address).
- Returns `true` if the action is allowed, `false` if the limit is exceeded.
### `limiter.reset(identifier)`
- Resets the rate limit for the given identifier.
### `limiter.resetAll()`
- Resets all rate limits.
## License
MIT