Package Exports
- @canmertinyo/rate-limit-express
- @canmertinyo/rate-limit-express/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@canmertinyo/rate-limit-express) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Express Rate Limiter
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 as storage options.
To install :
npm install express @canmertinyo/rate-limit-express ioredis mongooseExample usage :
import express from "express";
import { rateLimiter } from "@canmertinyo/rate-limit-express";
const app = express();
// Apply rate limiter middleware
app.use(
rateLimiter({
ms: 60000, // Time in milliseconds
maxRequest: 5, // Maximum requests allowed within the time
//DEFAULT IS IN MEMORY
})
);
app.get("/", (req, res) => {
res.send("Welcome to the API!");
});
app.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});Using Redis As A Store Manager
import express from "express";
import { rateLimiter, RedisStorage } from "@canmertinyo/rate-limit-express";
const app = express();
const port = 3001;
// Configure the rate limiter with Redis storage
app.use(
rateLimiter({
ms: 5000, // Time window in milliseconds
maxRequest: 2, // Maximum requests allowed in the time window
storage: new RedisStorage({ host: "127.0.0.1", port: 6379 }), // Redis configuration
})
);
// Sample route
app.get("/", (req, res) => {
res.send("Hello World!");
});
// Start the server
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});Using Mongo As A Store Manager
import express from "express";
import { MongoStorage, rateLimiter } from "@canmertinyo/rate-limit-express";
const app = express();
const port = 3001;
// MongoDB connection string (replace with your MongoDB URL)
const mongoUrl = "mongodb://your-mongodb-url";
app.use(
rateLimiter({
ms: 5000, // Time window in milliseconds
maxRequest: 2, // Maximum requests allowed in the time window
storage: new MongoStorage(mongoUrl), // MongoDB configuration
})
);
// Sample route
app.get("/", (req, res) => {
res.send("Hello World!");
});
// Start the server
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});