Skip to content

Commit 50b8e88

Browse files
authored
Merge pull request #23 from JoeDoyle23/support-redis-url
Add support for redis connection string
2 parents 66ef759 + 2543b78 commit 50b8e88

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ $ npm install --save rate-limit-redis
1616
## Usage
1717

1818
```js
19-
var RateLimit = require('express-rate-limit');
20-
var RedisStore = require('rate-limit-redis');
19+
const RateLimit = require('express-rate-limit');
20+
const RedisStore = require('rate-limit-redis');
2121

22-
var limiter = new RateLimit({
22+
const limiter = new RateLimit({
2323
store: new RedisStore({
2424
// see Configuration
2525
}),
@@ -32,15 +32,16 @@ app.use(limiter);
3232
```
3333

3434
## Connect to UDP Socket
35-
```
36-
var RateLimit = require('express-rate-limit');
37-
var RedisStore = require('rate-limit-redis');
38-
var Redis = require('ioredis');
39-
var client = new Redis('/tmp/redis.sock');
4035

41-
var limiter = new RateLimit({
36+
```js
37+
const RateLimit = require('express-rate-limit');
38+
const RedisStore = require('rate-limit-redis');
39+
const Redis = require('ioredis');
40+
const client = new Redis('/tmp/redis.sock');
41+
42+
const limiter = new RateLimit({
4243
store: new RedisStore({
43-
client: client,
44+
client: client
4445
}),
4546
max: 100, // limit each IP to 100 requests per windowMs
4647
delayMs: 0 // disable delaying - full speed until the max limit is reached
@@ -49,10 +50,11 @@ var limiter = new RateLimit({
4950

5051
## Configuration
5152

52-
* **expiry**: seconds - how long each rate limiting window exists for. Defaults to `60`.
53-
* **resetExpiryOnChange**: boolean - if the expiry time should be reset every time a key is incremented/decremented. This means that when the limit is reached and the user is given a 429 response, the rate limit window is extended. Defaults to `false`.
54-
* **prefix**: string - prefix to add to entries in Redis. Defaults to `rl:`.
55-
* **client**: [Redis Client](https://github.com/NodeRedis/node_redis) or [ioredis Client](https://github.com/luin/ioredis)- A Redis Client to use. Defaults to `require('redis').createClient();`.
53+
- **expiry**: seconds - how long each rate limiting window exists for. Defaults to `60`.
54+
- **resetExpiryOnChange**: boolean - if the expiry time should be reset every time a key is incremented/decremented. This means that when the limit is reached and the user is given a 429 response, the rate limit window is extended. Defaults to `false`.
55+
- **prefix**: string - prefix to add to entries in Redis. Defaults to `rl:`.
56+
- **client**: [Redis Client](https://github.com/NodeRedis/node_redis) or [ioredis Client](https://github.com/luin/ioredis)- A Redis Client to use. Defaults to `require('redis').createClient();`.
57+
- **redisURL**: string - a Redis connection string to be used for the default client connection. Ignored when the `client` option is provided. [Redis Client connection string format and options](https://github.com/NodeRedis/node_redis#rediscreateclient).
5658

5759
## License
5860

lib/redis-store.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ var RedisStore = function(options) {
66
options = defaults(options, {
77
expiry: 60, // default expiry is one minute
88
prefix: "rl:",
9-
resetExpiryOnChange: false
9+
resetExpiryOnChange: false,
10+
redisURL: undefined,
1011
});
1112

1213
var expiryMs = Math.round(1000 * options.expiry);
1314

1415
// create the client if one isn't provided
15-
options.client = options.client || redis.createClient();
16+
options.client = options.client || redis.createClient(options.redisURL);
1617

1718
var setExpire = function(replies, rdskey) {
1819
// if this is new or has no expiry

0 commit comments

Comments
 (0)