1.安装cookie-parser中间件
const express = require('express')
const cookieParser = require('cookie-parser')
// 本次 http 请求的实例
const app = express()
app.use(cookieParser()) // 配置cookie,会在req对象上生成cookie属性
// 设置cookie
app.use((req, res, next) => {
req.cookie = {
id: 3
}
next()
})
// 读取cookie
app.get('/', (req, res, next) => {
res.json({
cookie: req.cookie
})
})
app.listen(3000, () => {
console.log('server is running on port 3000')
})