document.cookie和res.cookie之间的区别 [英] difference between document.cookie and res.cookie

查看:393
本文介绍了document.cookie和res.cookie之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在客户端计算机上制作Cookie时,

When making cookies in the client computer,

我所知道的只是res.cookie

All I know was res.cookie

例如(在nodejs-express中),

for example (in nodejs-express),

app.get('/', (req, res) => {
    res.cookie('count', 1);
    res.send('something');
}

但我发现我也可以使用document.cookie制作cookies。

but I found that I can also make cookies with using document.cookie.

那么这两者之间的主要区别是什么制作饼干?

So what is the major difference between those two when making cookies?

(同样在阅读cookies时,req.cookies vs document.cookie)

(likewise when reading cookies, req.cookies vs document.cookie)

是文件。 cookie比res.cookies或req.cookies更大的概念?
不需要任何req,res参数,所以每当想要制作cookie时我就可以使用document.cookie?

Is document.cookie more bigger concept than res.cookies or req.cookies? without need of any req, res parameter, so whenever want to make cookies then I can just using document.cookie?

我已经阅读了w3school的文章,关于该文件是什么,req.cookies,res.cookies。但是无法弄清楚它们之间存在的差异。

I`ve already read the articles of w3school about what the document.cookies, req.cookies, res.cookies. but cannot figure out the differences existing between those.

谢谢。

解决方案

Express.js 的上下文中,节点服务器无权访问文档对象。通常使用 cookie-parser 中间件,我们可以访问请求cookie。

In the context of Express.js, the Node server does not have access to the document object. So usually with the cookie-parser middleware, we are able to access the request cookies.

基于Express .js文档,

Based on the Express.js documentation,

res.cookie(名称,价值[,选项]):


将cookie名称设置为value。 value参数可以是转换为JSON的字符串或
对象。

Sets cookie name to value. The value parameter may be a string or object converted to JSON.

req.cookies:


使用cookie-parser中间件时,此属性是
包含请求发送的cookie的对象。如果请求中不包含
cookie,则默认为{}。

When using cookie-parser middleware, this property is an object that contains cookies sent by the request. If the request contains no cookies, it defaults to {}.

这篇关于document.cookie和res.cookie之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆