当快速会话使用 connect-redis 存储时,Axios get 请求未返回

发布于 2025-01-14 21:19:24 字数 1778 浏览 3 评论 0原文

所以我遇到了一件奇怪的事情,我从前端发出的 axios 请求没有得到我从服务器发送的响应,但只有当我在快速会话中设置商店时才会发生这种情况。

我正在向登录路由发出发布请求,就像在这个函数中一样

const logIn = async (formData) => {
    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };

    const response = await axios.post("/login", formData, config);
    // do something with the response
  };

,当我发出请求时,服务器会以状态 200 和用户对象进行响应。当我检查浏览器开发工具中的网络选项卡时,我看到它得到了正确的响应,并且有效负载也显示了正确的用户对象

浏览器开发工具中的服务器响应

但是 axios 永远不会返回并永远挂起。

现在最关键的地方来了。仅当我在 app.js 中的快速会话上设置商店时,才会发生这种情况

import express from "express";
import router from "./routes/router.js";
import session from "express-session";
import "dotenv/config";
import redis from "redis";
import connectRedis from "connect-redis";

const PORT = process.env.PORT || 5000;

const app = express();

const redisClient = redis.createClient();
redisClient.connect();

redisClient.on("connect", () => {
  console.log("redis client connected");
});

const RedisStore = connectRedis(session);

app.use(
  session({
    name: "qid",
    store: new RedisStore({ client: redisClient }), // RIGHT HERE
    cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 365,
      httpOnly: true,
      sameSite: "lax",
      secure: false,
    },
    saveUninitialized: false,
    secret: process.env.SESSION_SECRET,
    resave: false,
  })
);

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use("/", router);

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

一旦我注释掉设置商店的行,Axios 就会得到响应,并且一切正常。所以我必须假设会话存储以某种方式阻止服务器响应到达 axios。

你知道为什么会这样吗?

So I have this weird thing going on where the axios requests I make from the front end do not get the response I'm sending from the server, but it only happens when I set the store in express session.

I am making a post request to the login route like in this function

const logIn = async (formData) => {
    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };

    const response = await axios.post("/login", formData, config);
    // do something with the response
  };

When I make the request the server responds with status 200 and a user object. When I check the network tab in the browser devtools, I see that it is getting the correct response and the payload is showing the right user object as well

server response in browser devtools

However axios never returns and is left hanging forever.

Now here's the kicker. This only happens if I set the store on the express session in my app.js

import express from "express";
import router from "./routes/router.js";
import session from "express-session";
import "dotenv/config";
import redis from "redis";
import connectRedis from "connect-redis";

const PORT = process.env.PORT || 5000;

const app = express();

const redisClient = redis.createClient();
redisClient.connect();

redisClient.on("connect", () => {
  console.log("redis client connected");
});

const RedisStore = connectRedis(session);

app.use(
  session({
    name: "qid",
    store: new RedisStore({ client: redisClient }), // RIGHT HERE
    cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 365,
      httpOnly: true,
      sameSite: "lax",
      secure: false,
    },
    saveUninitialized: false,
    secret: process.env.SESSION_SECRET,
    resave: false,
  })
);

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use("/", router);

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

As soon as I comment out the line where I set the store, Axios gets the response and everything works perfectly. So I have to assume that the session store is somehow blocking the server response from getting to axios.

Do you have any idea why that might be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-01-21 21:19:25

我遇到了完全相同的问题,但我最终可以使用 ioredis< 来解决我的设置问题/code>而不是 redis

我相应地更新了你的 app.js

import express from "express";
import router from "./routes/router.js";
import session from "express-session";
import "dotenv/config";
import Redis from 'ioredis';
import connectRedis from "connect-redis";

const PORT = process.env.PORT || 5000;

const app = express();

const redisClient = new Redis();

redisClient.on("connect", () => {
  console.log("redis client connected");
});

const RedisStore = connectRedis(session);

app.use(
  session({
    name: "qid",
    store: new RedisStore({ client: redisClient }), // RIGHT HERE
    cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 365,
      httpOnly: true,
      sameSite: "lax",
      secure: false,
    },
    saveUninitialized: false,
    secret: process.env.SESSION_SECRET,
    resave: false,
  })
);

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use("/", router);

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

I ran into the exact same issue, but I could finally solve this for my setup by using ioredis instead of redis.

I updated your app.js accordingly

import express from "express";
import router from "./routes/router.js";
import session from "express-session";
import "dotenv/config";
import Redis from 'ioredis';
import connectRedis from "connect-redis";

const PORT = process.env.PORT || 5000;

const app = express();

const redisClient = new Redis();

redisClient.on("connect", () => {
  console.log("redis client connected");
});

const RedisStore = connectRedis(session);

app.use(
  session({
    name: "qid",
    store: new RedisStore({ client: redisClient }), // RIGHT HERE
    cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 365,
      httpOnly: true,
      sameSite: "lax",
      secure: false,
    },
    saveUninitialized: false,
    secret: process.env.SESSION_SECRET,
    resave: false,
  })
);

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use("/", router);

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文