Trying to use redis store with express-session with the help of connect-redis official docs

Trying to use redis store with express-session with the help of connect-redis official docs


0

Here’s what I’m trying to do. I’m trying to use redis as store with express-session but get an error :

src/index.ts(14,20): error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
src/index.ts(15,5): error TS2353: Object literal may only specify known properties, and 'client' does not exist in type '(options?: SessionOptions | undefined) => RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

Here is my code:

import express, { Request } from "express";
import { expressMiddleware } from "@apollo/server/express4";
import startApolloServer from "./graphql";
import RedisStore from "connect-redis";
import session from "express-session";
import { createClient } from "redis";

async function init() {
  const app = express();

  let redisClient = createClient();
  redisClient.connect().catch(console.error);

  let redisStore = new RedisStore({
    client: redisClient,
  });

  const context = async ({ req }: { req: Request }) => ({
    session: req.session,
  });

  app.use(express.json());
  app.use(
    session({
      store: redisStore,
      name: "connect.sid",
      secret: "secret",
      resave: false,
      saveUninitialized: false,
      cookie: {
        httpOnly: true,
        secure: false,
        sameSite: "lax",
        maxAge: 1000 * 60 * 60 * 24 * 7,
      },
    })
  );
  app.use(
    "/graphql",
    expressMiddleware(await startApolloServer(), { context })
  );

  app.listen(4000, () => {
    console.log(
      "Server running on port 4000 on: https://localhost:4000/graphql"
    );
  });
}

init();

I tried using

import Redis from 'ioredis';
import RedisSession from 'connect-redis';

const RedisClient = new Redis();
const RedisStore = new RedisSession(session);

 app.use(
    session({
      store: new RedisStore({ client: RedisClient }),

instead and ended up getting this error:

'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.ts(7009)

I’m also using graphql with Apollo Server on the backend and routing all my requests from the frontend to /graphql which runs the apollo graphql server. Trying to get redis store working but running into the above mentioned problems


Load 5 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *