connect-redis is a Redis session store backed by node_redis, and is insanely fast :). Requires redis >= 2.0.0 for the SETEX command.
Note: connect-redis
>= 2.0.0only supports express>= 4.0.0. Use connect-redis1.4.7for express 3.x.
$ npm install connect-redis
A Redis client is required. An existing client can be passed directly using the client param or created for you using the host, port, or socket params.
clientAn existing client created usingredis.createClient()hostRedis server hostnameportRedis server portnosocketRedis server unix_socket
The following additional params may be included:
ttlRedis session TTL (expiration) in secondsdisableTTLdisables setting TTL, keys will stay in redis until evicted by other means (overidesttl)dbDatabase index to usepassPassword for Redis authenticationprefixKey prefix defaulting to "sess:"unrefSettrueto unref the Redis client. Warning: this is an experimental feature.
Any options not included in this list will be passed to the redis createClient() method directly.
Due to express >= 4 changes, we now need to pass express-session to the function connect-redis exports in order to extend session.Store:
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
app.use(session({
store: new RedisStore(options),
secret: 'keyboard cat'
}));
Since node_redis which this library wraps does not include the ability to create a client from a URL. Neither does this library. However, there's a separate module that can be used in conjunction to get this behavior.
By default, the node_redis client will auto-reconnect when a connection is lost. But requests may come in during that time. In express, one way this scenario can be handled is including a "session check" after setting up a session (checking for the existence of req.session):
app.use(session( /* setup session here */ ))
app.use(function (req, res, next) {
if (!req.session) {
return next(new Error('oh no')) // handle error
}
next() // otherwise continue
})MIT