router.get("/init",(req,res)=>{ res.header("Content-Type", "application/json; charset=utf-8"); var cookies = req.headers.cookie; console.log(cookies) })
运行结果:
前端cookie:
把cookie以字符串的形式切割成了对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
router.get("/init",(req,res)=>{ res.header("Content-Type", "application/json; charset=utf-8"); var cookies = req.headers.cookie; console.log(cookies) // 判断cookie是否存在 if (cookies) { let arr = cookies.split(";") let cookiesObj = {}; //对象形式的cookie arr.forEach(item => { let objArr = item.split("="); cookiesObj[objArr[0].trim()] = objArr[1].trim() }) console.log(cookiesObj) } })