在 Node.js 中设置 cookie 值 [英] Set a cookie value in Node.js
本文介绍了在 Node.js 中设置 cookie 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 node.js 和 express 开发一个网站.如何设置 cookie 值?
I'm developing a website with node.js and express. How can I set a cookie value?
推荐答案
由于 Express 建立在 Connect 之上,您可以使用cookieParser
中间件 和 req.cookies
阅读和res.cookie()
写 cookie:
As Express is built on Connect, you can use the cookieParser
middleware and req.cookies
to read and res.cookie()
to write cookies:
// configuration
app.use(express.cookieParser());
// or `express.cookieParser('secret')` for signed cookies
// routing
app.get('/foo', function (req, res) {
res.cookie('bar', 'baz');
// ...
});
app.get('/bar', function (req, res) {
res.send(req.cookies.bar);
});
<小时>
[更新]
[Update]
从 Express 4.0 开始,将不再包含连接与 Express 和默认中间件 已移入它们自己的包,包括 cookie-parser
.
As of Express 4.0, Connect will no longer be included with Express and the default middleware have been moved into their own packages, including cookie-parser
.
这篇关于在 Node.js 中设置 cookie 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文