node 怎么操作cookie 服务端和客户端都要,求方法!
cookie: console.log(req.headers);
@tulayang 我是这样写的 但是输出的是undefiend
因为客户端没有cookie发送,这时候headers对象就默认不设定cookie属性。 可以cookie = req.headers.cookie || null;<br/>
res.setHeader('Set-Cookie', 'name=lili; path=/; max-age=360000');
从服务器向客户端设定cookie内容
@tulayang 这是我再服务端 设置coolie的代码 var mycookie = cookie.serialize(‘username’, req.body.username,{maxAge:1000}); res.setHeader(“Set-Cookie”,mycookie ); console.log(req.headers); 最后面我输出了headers: host: ‘localhost:3000’, connection: ‘keep-alive’, ‘content-length’: ‘26’, accept: ‘text/plain, /; q=0.01’, origin: ‘http://localhost:3000’, ‘x-requested-with’: ‘XMLHttpRequest’, ‘user-agent’: ‘Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Ge ko) Chrome/35.0.1916.153 Safari/537.36’, ‘content-type’: ‘application/x-www-form-urlencoded; charset=UTF-8’, referer: ‘http://localhost:3000/’, ‘accept-encoding’: ‘gzip,deflate,sdch’, ‘accept-language’: ‘zh-CN,zh;q=0.8’ }
没有cookie这个属性。然后我在前段 输出了document.cookie;是可以输出我的cookie的 内容是:“username=huangchao”
你用的模块我不清楚。
你可以res.setHeader(‘Set-Cookie’, ‘name=lili; path=/; max-age=360000’);
然后测试下 console.log(req.headers);
path一定要设置。
@tulayang 结果是一样的 代码如下: res.setHeader(‘Set-Cookie’, ‘username=’+eq.body.username+’; path=/; max-age=360000’); console.log(req.headers); 输出结果: { host: ‘localhost:3000’, connection: ‘keep-alive’, ‘content-length’: ‘26’, accept: ‘text/plain, /; q=0.01’, origin: ‘http://localhost:3000’, ‘x-requested-with’: ‘XMLHttpRequest’, ‘user-agent’: ‘Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Ge cko) Chrome/35.0.1916.153 Safari/537.36’, ‘content-type’: ‘application/x-www-form-urlencoded; charset=UTF-8’, referer: ‘http://localhost:3000/’, ‘accept-encoding’: ‘gzip,deflate,sdch’, ‘accept-language’: ‘zh-CN,zh;q=0.8’ }
@tulayang 哦 有了,刚才是我自己弄错了