0

I am trying to connect to Redis using typescript and nodejs but I keep getting ** error TS2693: 'RedisStore' only refers to a type, but is being used as a value here.**

    let redisCLient = createClient({legacyMode: true});
redisCLient.connect().catch(console.error);

declare module "express-session" {
  interface SessionData {
    isLoggedIn: boolean;
  }
}


// middleware
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(cors());
app.use(
  session({
    secret: "reddit_apples_should_be_next",
    resave: false,
    saveUninitialized: false,
    store: new RedisStore({client: redisCLient}),
  })
);
1
  • The error states that you can't use RedisStore to instantiate a new RedisStore, since it's only a type, not a class or interface. If you're using connect-redis, you have to declare a variable in this fashion: let RedisStore = require("connect-redis")(session) Commented Jan 27, 2023 at 1:39

1 Answer 1

1

You can do stuff like this

Basic Redis setup

Import statements

const express = require('express');
const session = required('express-session');
const redis = require('redis');
const connectRedis = require('connect-redis');
const app = express();
app.use(express.json());

connection

const RedisStore = connectRedis(session);

const redisClient = redis.createClient(
port: 6379,
host: 'localhost'
});

configure session middleware

app.use(session({
store: new RedisStore({client: redisClient}),
secret: 'mySecret',
saveUninitialized: false,
resave: false,
name:'sessionId',
cookie: {

//secure false for non https request , keep false in production

secure: false,

// if true, prevents client side JS from reading the cookie

httpOnly: true,

//session age in millisec // 30 mins

maxAge: 1000 * 60 * 30
}
}));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.