readme modified

This commit is contained in:
Sumit Kumar 2024-09-27 13:06:09 +05:30
parent f435228f00
commit f95774f321
4 changed files with 9 additions and 12 deletions

6
README.md Normal file → Executable file
View 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)
---

View file

@ -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();
};
};

View file

@ -8,7 +8,6 @@ class RateLimiter {
this.clients = new Map();
}
// Function to handle a request
handleRequest(clientId) {
const currentTime = Date.now();

View file

@ -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",