node.js 我什么时候应该将 cookie-parser 与 express-session 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27961320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
when should I use cookie-parser with express-session?
提问by surenyonjan
In most ExpressJs example, I found using cookie-parserwith express-session.
在大多数 ExpressJs 示例中,我发现使用cookie-parserwith express-session.
If I could access session data with req.session.namewithout it, in what case ( or benefits ) should I be using cookie-parser?
如果我可以在req.session.name没有它的情况下访问会话数据,在什么情况下(或好处)我应该使用cookie-parser?
回答by Max Yari
For future humble coders, that will stumble upon this - I'm posting an up-to-date answer:
对于未来谦虚的编码员,这会偶然发现这一点 - 我发布了一个最新的答案:
As the official description of express-sessionmiddleware says here: express-session
正如express-session这里对中间件的官方描述所说:express-session
Since version 1.5.0, the
cookie-parsermiddleware no longer needs to be used for this module to work. This module now directly reads and writes cookies on req/res. Usingcookie-parsermay result in issues if the secret is not the same between this module andcookie-parser.
从 1.5.0 版本开始,
cookie-parser此模块不再需要使用中间件才能工作。该模块现在直接在 req/res 上读取和写入 cookie。使用cookie-parser可能会导致问题,如果这个秘密是不是该模块之间的相同cookie-parser。
Therefore, just use express-session middleware and have a nice day.
因此,只需使用 express-session 中间件并度过愉快的一天。
回答by Ben
In addition to providing simple cookie parsing functionality, the cookie-parsermiddleware enables signed cookies which can be referenced by other middleware components, using an optional secretattribute.
除了提供简单的 cookie 解析功能外,cookie-parser中间件还使用可选secret属性启用签名 cookie,其他中间件组件可以引用这些 cookie 。
Why would you want signed cookies? This question addresses that well
为什么要签名 cookie?这个问题很好地解决了这个问题

