skip to Main Content

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


  1. import { createClient } from 'redis';
    
    const client = createClient({
        password: '*******',
        socket: {
            host: 'redis-10352.c292.ap-southeast-1-1.ec2.cloud.redislabs.com',
            port: 10352
        }
    });
    

    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.

    
    import RedisStore from 'connect-redis';
    import express from 'express';
    import session from 'express-session';
    import { createClient } from 'redis';
    
    const redisURL = 'redis://localhost:6379';
    const app = express();
    const redisClient = createClient({ url: redisURL });
    redisClient.connect().catch(console.error);
    app.use(
      session({
        store: new RedisStore({
          client: redisClient,
        }),
        resave: false,
        saveUninitialized: false,
        secret: 'yoursecrethereabcxyz',
        cookie: {
          httpOnly: true,
          path: '/',
          sameSite: 'strict',
          expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
        },
      })
    );
    
    app.get('/', (req, res) => {
      res.send('Running');
    });
    
    const PORT = 1234;
    
    app.listen(PORT, () => {
      console.log(`Server is running on http://localhost:${PORT}`);
    });
    
    

    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

    Login or Signup to reply.
  2. Node Redis receives it’s configuration in the call to createClient, not the call to .connect. You need to connect like this instead:

    const redisClient = createClient({ url: redisURL });
    await redisClient.connect();
    

    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 of 6379.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search