在使用 Cookie 之前,需要给 Express 加载中间件,cookie-parser
:
npm i cookie-parser
Express 使用中间件:
import express from "express";
import cookieParser from "cookie-parser";
const app = express();
app.use(cookieParser());
res.cookie()
设置 Cookie,封装在响应头发送给客户端:
app.post("/login", (req, res) => {
res.cookie("USER_INFO", "SHIRAMASHIRO");
});
app.post("/send", (req, res) => {
let cookies = req.cookies;
console.log(cookies.USER_INFO); // => SHIRAMASHIRO
})
标签:cookies,res,app,Express,cookie,使用,Cookie
From: https://www.cnblogs.com/shiramashiro/p/16590245.html