在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?
推荐答案
As Express建立在 Connect ,则可以使用 cookieParser
middleware 和 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起, Connect将不再包含在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屋!
查看全文