118 lines
3.1 KiB
Markdown
118 lines
3.1 KiB
Markdown
# Rate Limiter
|
|
|
|
A customizable rate-limiting and throttling middleware for Node.js applications. It allows you to limit the number of requests a client can make to your server within a specified time window.
|
|
|
|
## Features
|
|
|
|
- Flexible request limiting based on time windows.
|
|
- Easily configurable for IP-based or user-based rate limiting.
|
|
- Simple integration as middleware in Node.js apps (e.g., with Express).
|
|
- Supports both **rate limiting** and **throttling**.
|
|
|
|
## Installation
|
|
|
|
You can install the package via npm:
|
|
|
|
```bash
|
|
npm install rate-limiter
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Basic Usage
|
|
|
|
The package can be used as middleware in your Node.js/Express applications to limit requests. Here's an example to allow **10 requests per second**.
|
|
|
|
```javascript
|
|
const express = require('express');
|
|
const rateLimiter = require('rate-limiter');
|
|
|
|
const app = express();
|
|
|
|
// Apply rate limiter middleware
|
|
app.use(rateLimiter({
|
|
windowMs: 1000, // 1 second
|
|
maxRequests: 10 // Limit each client to 10 requests per windowMs
|
|
}));
|
|
|
|
app.get('/', (req, res) => {
|
|
res.send('Hello, world!');
|
|
});
|
|
|
|
app.listen(3000, () => {
|
|
console.log('Server is running on port 3000');
|
|
});
|
|
```
|
|
|
|
### Options
|
|
|
|
The `rateLimiter` function accepts an options object to configure the behavior:
|
|
|
|
- **`windowMs`**: The time frame (in milliseconds) to allow `maxRequests`. For example, `1000` will enforce the limit per second.
|
|
- **`maxRequests`**: The maximum number of requests allowed during the `windowMs` period.
|
|
|
|
### Example: 100 Requests per 15 Minutes
|
|
|
|
```javascript
|
|
app.use(rateLimiter({
|
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
maxRequests: 100 // Limit each client to 100 requests per windowMs
|
|
}));
|
|
```
|
|
|
|
## Advanced Features
|
|
|
|
### IP-Based Limiting
|
|
|
|
By default, the middleware identifies clients by their IP address (`req.ip` in Express). This ensures that requests from each client IP are tracked separately.
|
|
|
|
### User-Based Limiting
|
|
|
|
You can also implement user-based rate limiting by passing a user identifier (such as an API key or user ID) to the rate limiter.
|
|
|
|
```javascript
|
|
app.use((req, res, next) => {
|
|
const clientId = req.user.id; // Assuming you have user authentication
|
|
rateLimiter({
|
|
windowMs: 60000, // 1 minute
|
|
maxRequests: 30 // Limit to 30 requests per minute per user
|
|
})(req, res, next);
|
|
});
|
|
```
|
|
|
|
## Development
|
|
|
|
If you'd like to contribute or modify the package, you can clone the repository and run it locally:
|
|
|
|
```bash
|
|
git clone https://git.digimantra.com/abad_dml/rate-limiter
|
|
cd rate-limiter
|
|
npm install
|
|
```
|
|
|
|
### Testing Locally
|
|
|
|
To run the Express example:
|
|
|
|
1. Create a file `app.js` as shown in the example above.
|
|
2. Run the server:
|
|
|
|
```bash
|
|
node app.js
|
|
```
|
|
|
|
## Contributing
|
|
|
|
Contributions are welcome! Please open issues and submit pull requests on GitHub if you'd like to improve the package.
|
|
|
|
## Issues
|
|
|
|
If you encounter any problems or have any questions, feel free to open an issue on GitHub.
|
|
|
|
## Links
|
|
|
|
- **GitHub**: [https://git.digimantra.com/abad_dml/rate-limiter](https://git.digimantra.com/abad_dml/rate-limiter)
|
|
- **NPM**: [https://www.npmjs.com/package/rate-limiter-digimantra](https://www.npmjs.com/package/rate-limiter-digimantra)
|
|
|
|
---
|