Full setup for Redis session storage
masterTo use connect-redis with express-session, initialize a redis client, create a new RedisStore instance passing the client and an optional prefix, and then pass that store to the express-session middleware.
Note that resave: false is required to ensure lightweight session keep-alive (touch) functionality works correctly.
import {RedisStore} from "connect-redis"
import session from "express-session"
import {createClient} from "redis"
// Initialize client.
let redisClient = createClient()
redisClient.connect().catch(console.error)
// Initialize store.
let redisStore = new RedisStore({
client: redisClient,
prefix: "myapp:",
})
// Initialize session storage.
app.use(
session({
store: redisStore,
resave: false, // required: force lightweight session keep alive (touch)
saveUninitialized: false, // recommended: only save session when data exists
secret: "keyboard cat",
}),
)