node.js&express操作cookie

const experss = require('express');
const path = require('path');
const cookieparser = require('cookie-parser');
const app = experss();
app.listen(3000, () => {
    console.log(`web服务器工作在3000端口`);
});
//使用第三方cookie-parser中间件来获取cookie:
app.use(cookieparser());
//设置/定义cookie: res.cookie('cookie名称',值[,选项]);
app.get('/setcookie', (req, res) => {
    //maxAge:用来设置cookie的有效时间,时间单位为毫秒
    res.cookie('TEL', 110, { maxAge: 9 * 1000 });
    res.cookie('xingming', 'zhagnsan');
    res.cookie('email', 'zhangsan@163.com');
    res.cookie('fenshu', [60, 90, 80]);
    res.send(`<script>alert('cookie设置成功');</script>`);
});
app.get('/mytest', (req, res) => {
    res.send(`获取cookie值:${JSON.stringify(req.cookies)}`);
});
//获取cookie:
app.get('/getcookie', (req, res) => {
    console.log(req.cookies);
    let { xingming = '', email = '' } = req.cookies;
    res.send(`获取cookie ${xingming}  ${email}`);
});
上一篇:C++核心准则边译边学 In.1: Express ideas directly in code 直接在代码中表达想法


下一篇:[webpack-cli] Invalid options object. Dev Server has been initialized using an options object