A distributed rate limiting store for the hono-rate-limiter
and express-rate-limit
middleware.
Sign up for a free account at rlimit.com and create a namespace, or use example
as namespace ID to test the functionality.
From the npm registry:
# Using npm
> npm install @rlimit/storage
# Using yarn or pnpm
> yarn/pnpm add @rlimit/storage
This library is provided in ESM as well as CJS forms, and works with both Javascript and Typescript projects.
This package requires you to use Node 16 or above.
Import it in a CommonJS project (type: commonjs
or no type
field in
package.json
) as follows:
const { Store } = require("@rlimit/storage");
Import it in a ESM project (type: module
in package.json
) as follows:
import { Store } from "@rlimit/storage";
To use it with a hono-rate-limiter
middleware:
Interactive demo on ratelimit.new/hono-rate-limiter
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { Store } from "@rlimit/storage";
import honoRateLimiter from "hono-rate-limiter";
const limiter = honoRateLimiter.rateLimiter({
windowMs: 60 * 1000, // 1 minute
limit: 20, // Limit each key (from keyGenerator) to 20 requests per `window` (here, per 1 minute)
standardHeaders: "draft-6", // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
keyGenerator: () => "<unique_key>", // Method to generate custom identifiers for clients, default to IP address
store: new Store({
namespace: "example", // your rlimit.com namespace ID
}),
});
const app = new Hono();
app.use(limiter);
app.get("/", (c) => {
// your application logic here
return c.json({ message: "Hello Hono!" });
});
serve(app);
To use it with a express-rate-limit
middleware:
Interactive demo on rlimit.com/demos/nodejs-express
import express from "express";
import { rateLimit } from "express-rate-limit";
import { Store } from "@rlimit/storage";
const app = express();
let rlimit = rateLimit({
// limit IP address to 3 requests every 5 seconds
limit: 3,
windowMs: 5_000,
store: new Store({
namespace: "example", // your rlimit.com namespace ID
}),
});
app.use(rlimit);
app.get("/", async (req, res) => {
res.send("Hello World!");
});
app.listen(3000, () => {
console.log("Express application started");
});