What is @types/connect-redis?
@types/connect-redis provides TypeScript type definitions for the connect-redis package, which is a Redis session store for Express. This package allows developers to use Redis as a session store in their Express applications with type safety provided by TypeScript.
Setting up Redis session store
This code demonstrates how to set up a Redis session store in an Express application using the connect-redis package. The RedisStore is created with a Redis client and used as the session store in the Express app.
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const redisClient = require('redis').createClient();
const app = require('express')();
app.use(session({
store: new RedisStore({ client: redisClient }),
secret: 'your-secret-key',
resave: false,
saveUninitialized: false
}));
Configuring Redis store options
This code sample shows how to configure various options for the Redis store, such as host, port, TTL (time to live), database index, password, and key prefix. These options allow for more fine-grained control over the Redis session store.
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const redisClient = require('redis').createClient();
const app = require('express')();
app.use(session({
store: new RedisStore({
client: redisClient,
host: 'localhost',
port: 6379,
ttl: 260,
disableTTL: false,
db: 0,
pass: 'your-password',
prefix: 'sess:'
}),
secret: 'your-secret-key',
resave: false,
saveUninitialized: false
}));