Skip to content

Commit

Permalink
Add max function example
Browse files Browse the repository at this point in the history
Relates to #240, #227, #231 (comment), probably some others.
  • Loading branch information
nfriedly committed Oct 1, 2021
1 parent 8cc6b78 commit 87e4c83
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Expand Up @@ -112,6 +112,31 @@ May be a number, or a function that returns a number or a promise. If `max` is a

Defaults to `5`. Set to `0` to disable.

Example of using a function:

```js
const rateLimit = require("express-rate-limit");

function isPremium(req) {
//...
}

const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes

// max could also be an async function or return a promise
max: function(req, res) {
if (isPremium(req)) {
return 10;
}
return 5;
}
});

// apply to all requests
app.use(limiter);
```

### windowMs

Timeframe for which requests are checked/remembered. Also used in the Retry-After header when the limit is reached.
Expand Down

0 comments on commit 87e4c83

Please sign in to comment.