在本地开发环境中使用cookie,可以在Node.js中使用cookie-parser中间件。
安装cookie-parser:
npm install cookie-parser
在Express应用程序中使用cookie-parser中间件:
const express = require('express');
const app = express();
const cookieParser = require('cookie-parser');
app.use(cookieParser());
添加cookie:
app.get('/', (req, res) => {
res.cookie('name', 'value');
});
获取cookie:
app.get('/', (req, res) => {
req.cookies.name
});
删除cookie:
app.get('/', (req, res) => {
res.clearCookie('name');
});
注意:cookie-parser中间件应该在其他使用cookie的路由中间件之前使用,以确保完整的功能性。
上一篇:本地开发环境的日志中出现警告
下一篇:本地开发环境问题不断出现