This middleware helps to restrict the number of requests a client can make within a given time period. It's ideal for lightweight use cases and can be easily extended with in-memory, Redis, or MongoDB, Memcached as storage options. https://www.npmjs.com/package/@canmertinyo/rate-limit-express
Installations
npm install @canmertinyo/rate-limiter-memcached
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
22.11.0
NPM Version
10.9.0
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
c4nzin
Download Statistics
Total Downloads
364
Last Day
7
Last Week
13
Last Month
35
Last Year
364
GitHub Statistics
1 Stars
25 Commits
1 Watching
1 Branches
1 Contributors
Bundle Size
33.55 kB
Minified
10.58 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.1.9
Package Id
@canmertinyo/rate-limiter-memcached@2.1.9
Unpacked Size
18.24 kB
Size
4.45 kB
File Count
9
NPM Version
10.9.0
Node Version
22.11.0
Publised On
03 Dec 2024
Total Downloads
Cumulative downloads
Total Downloads
364
Last day
0%
7
Compared to previous day
Last week
116.7%
13
Compared to previous week
Last month
-89.4%
35
Compared to previous month
Last year
0%
364
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
2
Express Rate Limiter
This set of packages provides flexible and highly customizable rate-limiting solutions for Node.js applications. The core package, @canmertinyo/rate-limiter-core, includes the in-memory implementation. To enable storage-backed rate limiting, you can integrate it with either @canmertinyo/rate-limiter-mongo for MongoDB, @canmertinyo/rate-limiter-redis for Redis, or @canmertinyo/rate-limiter-memcached for Memcached.
Rate Limiter Options
Option | Type | Description | Default Value | Example |
---|---|---|---|---|
ms | number | Time window in milliseconds for rate limiting. | 60000 (1 minute) | ms: 60000 |
maxRequest | number | Maximum requests allowed within the time window. | 10 | maxRequest: 10 |
storage | object | Storage manager for rate limits (e.g., in-memory, MongoDB, Redis, Memcached). | undefined (in-memory) | storage: mongoStorage |
message | string | Custom message returned when rate limit is exceeded. | "Too many requests" | message: "Too many requests, please try again later." |
statusCode | number | HTTP status code for rate limit responses. | 429 | statusCode: 429 |
keyGenerator | function | Function to generate a unique key for rate limiting (e.g., based on req.ip or headers). | (req) => req.ip | keyGenerator: (req) => req.ip |
skip | function | Function to bypass rate limiting for certain requests (e.g., based on user role). | undefined | skip: (req) => req.headers["x-user-role"] === "admin" |
errorHandler | function | Error handling function for issues from the storage layer. | Logs error and proceeds. | errorHandler: (req, res, next) => next() |
passOnStoreError | boolean | Whether to allow requests to pass even if the storage fails. | false | passOnStoreError: true |
To install (Core version only in memory):
1npm install @canmertinyo/rate-limiter-core
To install (Mongo store):
1npm install @canmertinyo/rate-limiter-mongo mongoose
To install (Redis store)
1npm install @canmertinyo/rate-limiter-redis ioredis
To install (Memcached store)
1npm install @canmertinyo/rate-limiter-memcached memcached
Example usage :
1import express from "express"; 2import { rateLimiter } from "@canmertinyo/rate-limiter-core"; 3 4const app = express(); 5 6// Apply rate limiter middleware 7app.use( 8 rateLimiter({ 9 ms: 60000, // Time in milliseconds 10 maxRequest: 5, // Maximum requests allowed within the time 11 //DEFAULT IS IN MEMORY 12 }) 13); 14 15app.get("/", (req, res) => { 16 res.send("Welcome to the API!"); 17}); 18 19app.listen(3000, () => { 20 console.log("Server is running on http://localhost:3000"); 21});
Using Redis As A Store Manager
1import express from "express"; 2import { RedisStorage } from "@canmertinyo/rate-limiter-redis"; 3import { rateLimiter } from "@canmertinyo/rate-limiter-core"; 4 5const app = express(); 6const port = 3001; 7 8// Configure the rate limiter with Redis storage 9app.use( 10 rateLimiter({ 11 ms: 5000, // Time window in milliseconds 12 maxRequest: 2, // Maximum requests allowed in the time window 13 storage: new RedisStorage({ host: "127.0.0.1", port: 6379 }), // Redis configuration 14 }) 15); 16 17// Sample route 18app.get("/", (req, res) => { 19 res.send("Hello World!"); 20}); 21 22// Start the server 23app.listen(port, () => { 24 console.log(`Server listening on port ${port}`); 25});
Using Mongo As A Store Manager
1import express from "express"; 2import { MongoStorage } from "@canmertinyo/rate-limiter-mongo"; 3import { rateLimiter } from "@canmertinyo/rate-limiter-core"; 4 5const app = express(); 6const port = 3001; 7 8// MongoDB connection string (replace with your MongoDB URL) 9const mongoUrl = "mongodb://your-mongodb-url"; 10 11app.use( 12 rateLimiter({ 13 ms: 5000, // Time window in milliseconds 14 maxRequest: 2, // Maximum requests allowed in the time window 15 storage: new MongoStorage(mongoUrl), // MongoDB configuration 16 }) 17); 18 19// Sample route 20app.get("/", (req, res) => { 21 res.send("Hello World!"); 22}); 23 24// Start the server 25app.listen(port, () => { 26 console.log(`Server listening on port ${port}`); 27});
1app.use( 2 rateLimiter({ 3 ms: 60000, // Time window in milliseconds 4 maxRequest: 10, // Maximum requests allowed 5 storage: mongoStorage, // Use MongoDB or Redis as storage or just leave it as empty. it will behave in memory storage 6 message: "Too many requests, please try again later.", // Custom rate limit message 7 statusCode: 429, // OPTIONAL: You can fully optimize HTTP status code for rate limit response 8 keyGenerator: (req) => req.ip, //OPTIONAL : Custom key generator 9 skip: (req) => { 10 //OPTIONAL : 11 const userRole = req.headers["x-user-role"]; // Assume user role is passed in headers 12 return userRole === "admin"; // Skip rate limiting for admin users 13 }, 14 errorHandler: (req, res, next) => { 15 console.error("Rate limiter error"); 16 next(); 17 }, // Handle errors from storage 18 passOnStoreError: true, // Pass requests even if storage fails 19 }) 20);
Using memcached as a store manager
1import express from "express"; 2import { MemcachedStore } from "@canmertinyo/rate-limiter-memcached"; 3import { rateLimiter } from "@canmertinyo/rate-limiter-core"; 4 5const app = express(); 6const port = 3001; 7 8// Configure the rate limiter with Memcached storage 9app.use( 10 rateLimiter({ 11 ms: 5000, // Time window in milliseconds 12 maxRequest: 2, // Maximum requests allowed in the time window 13 storage: new MemcachedStore("127.0.0.1:11211", { //optons for customize db behaivor }), // Memcached configuration 14 }) 15); 16 17// Sample route 18app.get("/", (req, res) => { 19 res.send("Hello World!"); 20}); 21 22// Start the server 23app.listen(port, () => { 24 console.log(`Server listening on port ${port}`); 25}); 26
No vulnerabilities found.
No security vulnerabilities found.