Gathering detailed insights and metrics for @sid3945/rate-limiter
Gathering detailed insights and metrics for @sid3945/rate-limiter
Gathering detailed insights and metrics for @sid3945/rate-limiter
Gathering detailed insights and metrics for @sid3945/rate-limiter
npm install @sid3945/rate-limiter
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A flexible and robust rate limiting middleware for Node.js applications with support for both in-memory and Redis-based storage. This rate limiter can be used in both single-server and distributed environments.
1npm install @sid3945/rate-limiter
1import express from 'express'; 2import RateLimiter from '@yourusername/rate-limiter'; 3 4const app = express(); 5 6const rateLimiter = new RateLimiter({ 7 window: 3000, // 3 seconds from the first request from this source 8 maxHits: 3 // 3 requests per window 9}); 10 11app.use(rateLimiter.guard()); 12 13app.get('/_healthz', (req, res) => { 14 res.json({ message: 'Status Ok!' }); 15}); 16 17app.listen(3000);
1const rateLimiter = new RateLimiter({ 2 window: 3000, // Time window in milliseconds 3 maxHits: 3, // Maximum number of requests per window 4 identifier: 'ip', // Identifier strategy ('ip', 'authToken', 'cookie') 5 storage: 'memory' // Storage strategy ('in-memory' or 'redis') 6});
1const rateLimiter = new RateLimiter({ 2 window: 3000, 3 maxHits: 3, 4 storage: 'redis', 5 redisConfig: { 6 host: 'localhost', 7 port: 6379, 8 password: 'your-password', db: 0 // Redis database number 9 } 10});
1const rateLimiter = new RateLimiter({ 2 window: 3000, 3 maxHits: 3, 4 customIdentifierExtractor: (req) => { 5 return req.headers['x-custom-id'] || req.ip; 6 } 7});
When rate limit is exceeded, the middleware returns:
1{ 2 error: "Too many requests", 3 retryAfter: 3 4}
With HTTP status code 429 (Too Many Requests).
1const apiLimiter = new RateLimiter({ 2 window: 60000, // 1 minute 3 maxHits: 60 // 60 requests per minute 4}); 5 6app.use('/api/', apiLimiter.guard());
1const publicLimiter = new RateLimiter({ 2 window: 60000, 3 maxHits: 30 4}); 5 6const authenticatedLimiter = new RateLimiter({ 7 window: 60000, 8 maxHits: 100, 9 identifier: 'authToken' 10}); 11 12app.use('/api/public', publicLimiter.guard()); 13 14app.use('/api/private', authenticatedLimiter.guard());
The middleware includes built-in error handling:
Contributions are welcome! Please feel free to submit a Pull Request.
MIT
Siddharth Upadhyay
No vulnerabilities found.
No security vulnerabilities found.