Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dotcypress/micro-ratelimit
Rate-limiting middleware for micro
https://github.com/dotcypress/micro-ratelimit
Last synced: 4 months ago
JSON representation
Rate-limiting middleware for micro
- Host: GitHub
- URL: https://github.com/dotcypress/micro-ratelimit
- Owner: dotcypress
- License: mit
- Created: 2017-03-30T18:22:08.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-04T16:45:56.000Z (over 6 years ago)
- Last Synced: 2024-10-14T14:18:44.147Z (4 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 74
- Watchers: 4
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-micro - micro-ratelimit - Rate limiting middleware for Micro. (Modules / Middlewares)
README
[![Build Status](https://img.shields.io/travis/dotcypress/micro-ratelimit.svg?branch=master&style=flat-square)](https://travis-ci.org/dotcypress/micro-ratelimit)
[![NPM Version](https://img.shields.io/npm/v/micro-ratelimit.svg?style=flat-square)](https://www.npmjs.com/package/micro-ratelimit)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)# Micro Rate Limit
Rate-limiting middleware for [micro](https://github.com/zeit/micro).
## Installation
```js
$ npm install micro-ratelimit
```## Examples
```js
const rateLimit = require('micro-ratelimit')module.exports = rateLimit((req, res) => {
return 'Hello world'
})```
```js
const rateLimit = require('micro-ratelimit')// Limit example: 2 requests per 10 sec
module.exports = rateLimit({ window: 10000, limit: 2, headers: true }, (req, res) => {
return 'Hello world'
})```
## API
### Options
* `window`: how long to keep records of requests in memory in ms (default: 1 second)
* `limit`: max number of requests during window (default: 1)
* `keyGenerator`: key generator function (req -> client id)
* `headers`: send rate limit headers (default: false)Default implementation of `keyGenerator`:
```js
function keyGenerator (req) {
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress
}
```