//cookie之后转换为对象
req.cookie={};
const cookieStr=req.headers.cookie;
cookieStr.split(';').forEach(item=>{
if(!item)
{
return
}
const arr=item.split('=')
const key=arr[0]
const val =arr[1]
req.coolie[key]=val
})
服务器端操作cookie
res.setHeader('Set-Cookie','username=${data.username};path=/;httponly')//httponly只允许后端修改,不允许前端修改
设置cookie过期时间
const getCookieExpires=()=>{
const d=new Date()
d.setTime(d.getTiem()+(24*60*60*1000))//当前时间加一天
return d.toGMTString()
}
之后在上面httponly后面加上expires=${getCookieExpires()}