node.js - 覆盖/更新浏览器 cookie

标签 node.js express cookies web browser

我有一个 Express 4.x 应用程序,我将一个 cookie 传递给浏览器

res.cookie('foo','bar1', {maxAge:99999999999});

它在遥远的 future 到期。然而,5 分钟后,我收到同一用户的另一个请求,我想给他们一个新的 cookie。

 res.cookie('foo','bar2', {maxAge:99999999999});

从我的调试来看,新的 cookie 似乎没有覆盖旧的 cookie?是这样吗? 如何用新的 cookie 更新/覆盖旧的 cookie?

最佳答案

我大约 99% 确定使用 {overwrite: true} 属性可以解决问题 - 从这里开始:

https://www.npmjs.com/package/cookies

它说:

cookies.set( name, [ value ], [ options ] ) This sets the given cookie in the response and returns the current context to allow chaining.

If the value is omitted, an outbound header with an expired date is used to delete the cookie.

If the options object is provided, it will be used to generate the outbound cookie header as follows:

maxAge: a number representing the milliseconds from Date.now() for expiry expires: a Date object indicating the cookie's expiration date (expires at the end of session by default). ... overwrite: a boolean indicating whether to overwrite previously set cookies of the same name (false by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie.

那么,用 Node.js Express 的说法就是:

res.cookie('cdt_app_token', encoded, {maxAge: 90000000, httpOnly: true, secure: false, overwrite: true});

请注意,如果您使用的是非 SSL 或非 TLS 连接,如果 secure 为 true,cookie 可能无法工作。

关于node.js - 覆盖/更新浏览器 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44401785/

相关文章:

javascript - 快速验证器错误 : expressValidator is not a function

mysql - 我的 Firebase 云函数给出 SequelizeConnectionError : connect ETIMEDOUT error whenever I query my MySql DB

node.js - 如果 header 中不存在 JWT token ,如何执行某些逻辑?

javascript - Node.js:使用顺序+异步输入/输出进行并行处理

javascript - 打包应用程序后的 Electron 和 sqlite3 问题

mysql - 更新 ReactJS 中检索的 SQL 数据的有效方法

javascript - 我们如何使用express在nodeJS中实现管理面板?

perl - 为什么 Safari 不从我的 Perl CGI 脚本设置 cookie?

python - 如何在 python 上生成安全 session_id ?

java - JAX-WS 客户端 : maintain session/cookies across multiple services