simple-rate-limiter-middleware
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Simple Rate Limiter Middleware

npm version npm downloads

A simple in memory ip based rate limiter middleware for express apps.

Installation

# Using npm
> npm install simple-rate-limiter-middleware
# Using yarn or pnpm
> yarn/pnpm add simple-rate-limiter-middleware

Usage

Importing

This library is provided in ESM as well as CJS forms, and works with both Javascript and Typescript projects.

Import it in a CommonJS project (type: commonjs or no type field in package.json) as follows:

const { rateLimit, MemoryStore } = require("simple-rate-limiter-middleware");

Import it in a ESM project (type: module in package.json) as follows:

import { rateLimit, MemoryStore } from "simple-rate-limiter-middleware";

Examples

To use it in an API-only server where the rate-limiter should be applied to all requests:

const { rateLimit, MemoryStore } = require("simple-rate-limiter-middleware");

const rateLimiter = rateLimit({
  statusCode: 429,
  message: "Example response message.",
  legacyHeaders: false,
  standardHeaders: true,
  store: new MemoryStore(60, 10),
});

app.use(rateLimiter);

You can also create multiple instance and apply then to different places of your API.

Troubleshooting Proxy Issues

If you are behind a proxy/load balancer (usually the case with most hosting services, e.g. Heroku, Bluemix, AWS ELB, Nginx, Cloudflare, Akamai, Fastly, Firebase Hosting, Rackspace LB, Riverbed Stingray, etc.), the IP address of the request might be the IP of the load balancer/reverse proxy (making the rate limiter effectively a global one and blocking all requests once the limit is reached) or undefined. To solve this issue, add the following line to your code (right after you create the express application):

app.set("trust proxy", numberOfProxies);

Where numberOfProxies is the number of proxies between the user and the server.

For more information about the trust proxy setting, take a look at the official Express documentation.

Issues and Contributing

If you encounter a bug or want to see something added/changed, please go ahead and open an issue! If you need help with something, feel free to start a discussion!

Fell free to contribute to the library.

Package Sidebar

Install

npm i simple-rate-limiter-middleware

Weekly Downloads

1

Version

1.0.6

License

MIT

Unpacked Size

19.8 kB

Total Files

5

Last publish

Collaborators

  • myonim