节点 js express 中的持久性 cookie

San*_*ndy 2 cookies node.js express

我正在尝试在我的 express js 代码中实现记住我的用户。在这种情况下,如果用户选中记住我用户,那么我们需要记住用户凭据 14 天。

我在 cookie 中设置了凭据详细信息,并尝试从 cookie 中获取凭据以供下次访问。

//set cookie value
res.cookie('email', emailId);

//retrieve cookie value
req.cookies.email;
Run Code Online (Sandbox Code Playgroud)

如果我关闭浏览器并再次打开它,那么这些 cookie 值不会持久存在,并且我将获得未定义的凭据。

我们使用了以下模块:

var express      = require('express')
var cookieParser = require('cookie-parser')
var app = express()
app.use(cookieParser())
Run Code Online (Sandbox Code Playgroud)

请让我知道如何在重新启动浏览器时保留 cookie 值。

谢谢,--桑迪

Mic*_*kan 5

有两件事需要研究:

首先,您是否设置了 cookie 过期时间?尝试设置为:

res.cookie('email', emailId, { maxAge: 900000, httpOnly: true });
Run Code Online (Sandbox Code Playgroud)

其次,确保在浏览器关闭时您没有清除 cookie 等(我清除了我的)。