Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wutility/rate-limit
👉 Simple IP rate-limiting middleware for Express.
https://github.com/wutility/rate-limit
expressjs nodejs rate-limit
Last synced: about 1 month ago
JSON representation
👉 Simple IP rate-limiting middleware for Express.
- Host: GitHub
- URL: https://github.com/wutility/rate-limit
- Owner: wutility
- License: apache-2.0
- Created: 2023-02-02T22:21:22.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-02T22:44:54.000Z (almost 2 years ago)
- Last Synced: 2024-09-29T12:40:57.003Z (about 2 months ago)
- Topics: expressjs, nodejs, rate-limit
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/rateli
- Size: 11.7 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Installation
```shell
# Using npm
> npm install rateli
# Using yarn or pnpm
> yarn/pnpm add rateli
```## Usage
import it in a CommonJS project (type: commonjs or no type field in
package.json) as follows:```js
const { rateLimit } = require("rateli");
```Import it in a ESM project (type: module in package.json) as follows:
```js
import { rateLimit } from "rateli";
```## Methods
```js
rateLimit(config: Config) : FunctionRateLimitBanHandler() : Function
```## Examples
```js
import { rateLimit, RateLimitBanHandler } from "rateli";const config = {
windowMs: 60000, // Time window in milliseconds (1 min)
max: 5, // Max number of requests allowed in a time window
maxReputation: 1000, // Max number of reputations allowed (auto insert to blacklist)
message: 'Too Many Requests',
headers: true, // Include the rate limit in the response headers// Generate a unique key based on the client's IP or x-api-key header
keyGenerator: (req: Request) => string
}const limiter = rateLimit(config);
// Apply the rate limiting middleware to all requests
app.use(limiter);
app.use(RateLimitBanHandler()) // when reputations (of IP) exceed limits, server returns 503
```## License
[Apache 2.0](LICENSE)