【问题标题】:Express app closing after any connection request after adding Redis to it将 Redis 添加到应用程序后,在任何连接请求后关闭快速应用程序
【发布时间】:2022-01-26 20:17:09
【问题描述】:

所以,我刚刚将 Redis 添加到我的 expressjs 应用程序中(这是我第一次使用 redis),这就是我添加 Redis 所做的一切:

import express, { Application } from 'express';
import session from 'express-session';
import {createClient} from 'redis';
import connectRedis from 'connect-redis'

const app: Application = express();
const RedisStore = connectRedis(session);
const redisClient = createClient({
    url: 'redis://localhost:6379'
});

app.use(session({
    secret: SESSION_SECRET,
    resave: false,
    saveUninitialized: true,
    store: new RedisStore({ client: redisClient }), // added this line
    cookie: {
        maxAge: 1000 * 60 * 2,
        httpOnly: false,
        secure: false,
    }
}));

export default app;

但是,当我向我的任何快速路线发出任何请求时,我会在控制台上立即收到错误消息,并且该应用程序已关闭以等待任何进一步的请求。这是错误:

如您所见,屏幕截图顶部有一行Server is listening on port 3000,其余字符串仅在我第一次向api发出请求时出现。

我在这里错过了什么,我做错了什么?因为互联网上的每个视频/文章都有相同的确切代码...

【问题讨论】:

  • 你使用的是什么节点和Redis版本?
  • 嗨@ApoorvaChikara,Node版本是v16.13.0,redis版本是^4.0.1和connect-redis ^6.0.0
  • @JoãoCasarin 如你所见here,connect-redis 不支持 node-redis v4(目前)
  • 嘿@LeibaleEidelman 刚刚检查过这里,显然就是这样!我尝试使用 node-redis v3.1.2 并且成功了,谢谢!

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


【解决方案1】:

好吧,正如@Apoorva Chikara 注意到的和@Leibale Eidelman 提到的那样,connect-redis 还不支持最新的node-redis,所以要修复它,我只是将我的redis 包降级到版本3.1.2 使用$ yarn add redis@3.1.2.

谢谢!

【讨论】:

    猜你喜欢
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多