Node如何获取前端的cookie

实现代码

下面的代码实现使用的是express框架

获取cookie

核心代码req.headers.cookie

1
2
3
4
5
router.get("/init",(req,res)=>{
res.header("Content-Type", "application/json; charset=utf-8");
var cookies = req.headers.cookie;
console.log(cookies)
})

运行结果:
1.png
前端cookie:
2.png

把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)
}
})