[英]how to create a cookie in nodejs socket.io
im trying to create a cookie while using socket.io.我试图在使用 socket.io 时创建一个 cookie。 I am able to read cookies but cannot find any information on how to create one.
我能够读取 cookie,但找不到有关如何创建 cookie 的任何信息。
socket.on('test', async function(){
// view set cookies (works!)
console.log('cookies', socket.handshake.headers.cookie);
// create cookie here...
});
thank you谢谢
您可以像在 mdn 文档中使用 setcookies() 那样设置 cookie 并使用 cookie api 读取它
You can do it the same way as document.cookie
, but instead of:您可以使用与
document.cookie
相同的方法,但不是:
document.cookie = ";mycookie=myval";
you do:你做:
socket.handshake.headers.cookie = ";mycookie=myval; expires=cookieDate; path=cookiePathOnWebsite"
for example:例如:
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = date.toGMTString();
socket.on('test',async function(){
socket.handshake.headers.cookie = ";foo=bar; expires="+date+";path=/"
});
would set a cookie at the main page of your website, with foo
defined as bar
!将在您网站的主页上设置一个 cookie,将
foo
定义为bar
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.