【问题标题】:How to use Redis as a store for Express Session NestJS如何使用 Redis 作为 Express Session NestJS 的存储
【发布时间】:2021-10-24 23:01:05
【问题描述】:

我正在使用 NestJS 创建一个 API,并试图为我的快速会话设置一个会话存储,但我从这一行得到一个错误。我确实在我创建的一个新项目中使用了与 Redis 的 express-session,只是事先使用 express 来了解 Redis 和 express 会话是如何工作的,但是当我尝试将它移植到 NestJS 时它不起作用。

Main.ts

import connectRedis from 'connect-redis';
import { redis } from './redis';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const RedisStore = connectRedis(session);

这是我得到的错误

const RedisStore = connectRedis(session);
                               ^
TypeError: connect_redis_1.default is not a function

在调用任何其他与 redis 相关的功能或 express-session 之前发生错误。如果需要,我仍然会介绍如何设置 Redis 和 Express-Session。

Redis.ts

import Redis from 'ioredis';

export const redis = new Redis(
  port,
  'hostName',
  { password: 'password' },
);

Main.ts 内部的会话

  app.use(
    session({
      store: new RedisStore({ client: redis }),
      cookie: {
        maxAge: 60000 * 60 * 24,
      },
      secret: 'mysecret',
      saveUninitialized: false,
      resave: false,
    }),
  );

我确实从 NestJS 文档中读到,我可以将 Redis 设置为微服务,但是我真的只需要 Redis 用于我的 Express-Session 并且如果我能解决这个问题,我不想设置 redis 微服务。

我还使用 Mongoose 连接到我的 MongoDB,我将其用于 NestJS 内的存储库。以前在其他项目中而不是使用 Redis,我会使用 ORMSession 在 TypeORM 中设置我的商店,如果有人可以使用 Mongoose 的替代方案,那么这也可以。

const sessionRepo = getRepository(TypeORMSession);

...

store: new TypeormStore().connect(sessionRepo),

【问题讨论】:

    标签: node.js express redis nestjs express-session


    【解决方案1】:

    错误就在那里说明。 connect_redis1.default 不是函数。相反,您应该使用import * as conectRedis from 'connect-redis'I've got an example here 看起来像这样:

    import { Inject, Logger, MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
    import * as RedisStore from 'connect-redis';
    import * as session from 'express-session';
    import { session as passportSession, initialize as passportInitialize } from 'passport';
    import { RedisClient } from 'redis';
    
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { AuthModule } from './auth';
    import { REDIS, RedisModule } from './redis';
    
    @Module({
      imports: [AuthModule, RedisModule],
      providers: [AppService, Logger],
      controllers: [AppController],
    })
    export class AppModule implements NestModule {
      constructor(@Inject(REDIS) private readonly redis: RedisClient) {}
      configure(consumer: MiddlewareConsumer) {
        consumer
          .apply(
            session({
              store: new (RedisStore(session))({ client: this.redis, logErrors: true }),
              saveUninitialized: false,
              secret: 'sup3rs3cr3t',
              resave: false,
              cookie: {
                sameSite: true,
                httpOnly: false,
                maxAge: 60000,
              },
            }),
            passportInitialize(),
            passportSession(),
          )
          .forRoutes('*');
      }
    }
    

    【讨论】:

      【解决方案2】:

      你需要安装@types/connect-redis

      然后做

      import * as _connectRedis from 'connect-redis';

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-15
        • 2020-07-13
        • 2012-03-20
        • 2017-12-28
        • 2015-05-18
        • 2019-09-13
        • 1970-01-01
        相关资源
        最近更新 更多