在使用 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
})
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/280827.html