如何在 localhost 中 运行 多个 express/Nodejs 应用程序?

How to run multiple express/Nodejs application in localhost?

我是 运行 我的客户端(前端)app express app 在端口 3000 和另一个 admin express app 在 8080。

但是当我在 client express 应用程序中导航任何页面或刷新任何页面时,管理应用程序中的会话丢失并重定向到登录。

我正在为管理员使用 Express-session npm,前端就像一个 cms 前端,即没有会话或任何复杂的东西。谁能告诉我为什么会这样?

运行 在不同的浏览器中 :

在不同的浏览器中尝试其中一种,例如 chrome 中的一种和 mozilla 中的另一种。这是由于那边的会话冲突而发生的。

Changing the browser might be a good solution for you.

想在同一个浏览器中运行:

只有 运行 一项服务在 normal mode 和 运行 其他服务在 incognito mode

根据提供的信息,我建议您查看初始化快速会话的方式。如果 secure 标志设置为 true,则需要为本地主机设置有效的 HTTPS 证书,否则不会创建会话,这可能会导致重定向问题。

来自文档:

secure: Specifies the boolean value for the Secure Set-Cookie attribute. When truthy, the Secure attribute is set, otherwise it is not. By default, the Secure attribute is not set. Please note that secure: true is a recommended option. However, it requires an https-enabled website, i.e., HTTPS is necessary for secure cookies. If secure is set, and you access your site over HTTP, the cookie will not be set.

端口 3000 上的应用程序 运行 的 Cookie 也会发送到端口 8080 上的应用程序 运行(反之亦然)。

我猜你没有给每个应用一个唯一的 cookie name,所以你可能会在两个应用的会话处理之间受到干扰。

因此,为每个使用不同的 cookie 名称:

app.use(session({
  name : 'frontend.sid', // and, say, 'admin.sid' for the admin app
  ...
}));