readme modified
This commit is contained in:
parent
f435228f00
commit
f95774f321
6
README.md
Normal file → Executable file
6
README.md
Normal file → Executable file
|
@ -14,7 +14,7 @@ A customizable rate-limiting and throttling middleware for Node.js applications.
|
||||||
You can install the package via npm:
|
You can install the package via npm:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install rate-limiter
|
npm install rate-limitx
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
@ -25,7 +25,7 @@ The package can be used as middleware in your Node.js/Express applications to li
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const rateLimiter = require('rate-limiter');
|
const rateLimiter = require('rate-limitx');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
@ -112,6 +112,6 @@ If you encounter any problems or have any questions, feel free to open an issue
|
||||||
## Links
|
## Links
|
||||||
|
|
||||||
- **GitHub**: [https://git.digimantra.com/abad_dml/rate-limiter](https://git.digimantra.com/abad_dml/rate-limiter)
|
- **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)
|
- **NPM**: [https://www.npmjs.com/package/rate-limitx](https://www.npmjs.com/package/rate-limitx)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
6
index.js
6
index.js
|
@ -1,21 +1,19 @@
|
||||||
const RateLimiter = require('./lib/rateLimiter');
|
const RateLimiter = require('./lib/rateLimiter');
|
||||||
|
|
||||||
// Middleware wrapper for Express.js or any other Node.js framework
|
|
||||||
const rateLimiterMiddleware = (options) => {
|
const rateLimiterMiddleware = (options) => {
|
||||||
const limiter = new RateLimiter(options);
|
const limiter = new RateLimiter(options);
|
||||||
|
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
const clientId = req.ip; // Use IP-based rate limiting
|
const clientId = req.ip;
|
||||||
const result = limiter.handleRequest(clientId);
|
const result = limiter.handleRequest(clientId);
|
||||||
|
|
||||||
if (!result.allowed) {
|
if (!result.allowed) {
|
||||||
return res.status(429).json({
|
return res.status(429).json({
|
||||||
message: 'Too many requests. Please try again later.',
|
message: 'Too many requests. Please try again later.',
|
||||||
retryAfter: result.remainingTime / 1000, // Retry after (seconds)
|
retryAfter: result.remainingTime / 1000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// If allowed, proceed to the next middleware or route handler
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,6 @@ class RateLimiter {
|
||||||
this.clients = new Map();
|
this.clients = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to handle a request
|
|
||||||
handleRequest(clientId) {
|
handleRequest(clientId) {
|
||||||
const currentTime = Date.now();
|
const currentTime = Date.now();
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "rate-limiter-digimantra",
|
"name": "rate-limitx",
|
||||||
"version": "1.1.0",
|
"version": "1.4.0",
|
||||||
"description": "Custom rate limiting middleware for Node.js",
|
"description": "Custom rate limiting middleware for Node.js",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|
Loading…
Reference in a new issue