I’m trying to do exactly this but my project is using ES6 and I’m having problems. here is how I’m declaring and starting:
import session from 'express-session';
import connectRedis from 'connect-redis';
import { createClient } from 'redis';
const app = express();
async function startServer() {
const redisURL = 'redis://localhost:6379';
const redisClient = createClient();
const RedisStore = connectRedis(session);
await redisClient.connect({ url: redisURL });
const store = new RedisStore({ redisClient });
app.use(session({
store: store ,
secret: process.env.SESSION_SECRET
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
path: '/',
sameSite: 'strict',
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)
}
}));
}
and I’m getting the error:D:AI ProjectsHplayer – Copyserverserver.js:33
RedisStore = (0, _connectRedis["default"])(_expressSession["default"]);
^
TypeError: Class constructor RedisStore cannot be invoked without ‘new’
any ideas?
connect redis and express session
2
Answers
I used Cloud Redis DB. The connection was successful. I used the cloud because I don’t have a local Redis installation. You will have to create client like this.
here is the solution. You don’t need to call connectRedis with the session as an argument. it will work like this. If this dont work then there might be an issue with url or redis permissions
Node Redis receives it’s configuration in the call to
createClient
, not the call to.connect
. You need to connect like this instead:This is probably not heart of your problem, but it is something that is incorrect. You’re probably getting away with it because if you pass nothing the createClient, it assumes a host of
localhost
and a port of6379
.