What is connect-redis?
The connect-redis npm package is a Redis session store for Express and Connect. It allows you to store session data in a Redis database, which can help with scaling applications by providing a centralized session store.
What are connect-redis's main functionalities?
Basic Setup
This code demonstrates how to set up a basic Express application with connect-redis as the session store. It configures the session middleware to use Redis for storing session data.
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const express = require('express');
const app = express();
app.use(session({
store: new RedisStore({
host: 'localhost',
port: 6379
}),
secret: 'your secret',
resave: false,
saveUninitialized: false
}));
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Custom Redis Client
This code demonstrates how to use a custom Redis client with connect-redis. This can be useful if you need to configure the Redis client with specific options or use an existing Redis client instance.
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const express = require('express');
const redis = require('redis');
const app = express();
const redisClient = redis.createClient({
host: 'localhost',
port: 6379
});
app.use(session({
store: new RedisStore({ client: redisClient }),
secret: 'your secret',
resave: false,
saveUninitialized: false
}));
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Advanced Configuration
This code demonstrates advanced configuration options for connect-redis, such as setting the time-to-live (ttl) for sessions and enabling error logging.
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const express = require('express');
const app = express();
app.use(session({
store: new RedisStore({
host: 'localhost',
port: 6379,
ttl: 260,
logErrors: true
}),
secret: 'your secret',
resave: false,
saveUninitialized: false
}));
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Other packages similar to connect-redis
express-session
express-session is a general-purpose session middleware for Express. It supports various session stores, including in-memory, file-based, and database-backed stores. Unlike connect-redis, it does not provide a Redis-specific store out of the box but can be extended with other packages.
connect-mongo
connect-mongo is a MongoDB session store for Express and Connect. It provides similar functionality to connect-redis but uses MongoDB as the backend store instead of Redis. It is useful for applications that already use MongoDB and want to keep session data in the same database.
express-mysql-session
express-mysql-session is a MySQL session store for Express. It provides similar functionality to connect-redis but uses MySQL as the backend store. It is useful for applications that use MySQL and want to store session data in a relational database.
Connect Redis
connect-redis is a Redis session store backed by node_redis, and is insanely fast :). Requires redis >= 1.3.10
for the SETEX command.
connect-redis >= 1.0.0
support only connect >= 1.0.0
.
Installation
via npm:
$ npm install connect-redis
Options
host
Redis server hostnameport
Redis server portnodb
Database index to usepass
Password for Redis authentication- ... Remaining options passed to the redis
createClient()
method.
Example
var connect = require('connect')
, RedisStore = require('connect-redis');
connect.createServer(
connect.cookieParser(),
// 5 minutes
connect.session({ store: new RedisStore, secret: 'keyboard cat' })
);