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:
|
||||
|
||||
```bash
|
||||
npm install rate-limiter
|
||||
npm install rate-limitx
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
@ -25,7 +25,7 @@ The package can be used as middleware in your Node.js/Express applications to li
|
|||
|
||||
```javascript
|
||||
const express = require('express');
|
||||
const rateLimiter = require('rate-limiter');
|
||||
const rateLimiter = require('rate-limitx');
|
||||
|
||||
const app = express();
|
||||
|
||||
|
@ -112,6 +112,6 @@ If you encounter any problems or have any questions, feel free to open an issue
|
|||
## 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)
|
||||
- **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');
|
||||
|
||||
// Middleware wrapper for Express.js or any other Node.js framework
|
||||
const rateLimiterMiddleware = (options) => {
|
||||
const limiter = new RateLimiter(options);
|
||||
|
||||
return (req, res, next) => {
|
||||
const clientId = req.ip; // Use IP-based rate limiting
|
||||
const clientId = req.ip;
|
||||
const result = limiter.handleRequest(clientId);
|
||||
|
||||
if (!result.allowed) {
|
||||
return res.status(429).json({
|
||||
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();
|
||||
};
|
||||
};
|
||||
|
|
|
@ -8,7 +8,6 @@ class RateLimiter {
|
|||
this.clients = new Map();
|
||||
}
|
||||
|
||||
// Function to handle a request
|
||||
handleRequest(clientId) {
|
||||
const currentTime = Date.now();
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
{
|
||||
"name": "rate-limiter-digimantra",
|
||||
"version": "1.1.0",
|
||||
"name": "rate-limitx",
|
||||
"version": "1.4.0",
|
||||
"description": "Custom rate limiting middleware for Node.js",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
"rate-limiting",
|
||||
"rate-limiter",
|
||||
"secure-api",
|
||||
|
|
Loading…
Reference in a new issue