//参考链接https://segmentfault.com/a/1190000004139342?_ea=504710
//后端示例
// npm install cookie-parser
var express = require('express')
var cookieParser = require('cookie-parser')
var app = express()
app.use(cookieParser())
app.get('/', function(req, res) {
console.log('Cookies: ', req.cookies)
})
//cookie的创建(存储到cookie中) res.cookie(name, value [, options])
//cookie的读取 req.cookies/req.cookies.name
//cookie的删除res.clearCookie(name [, options])
app.listen(8080)
//前端示例
//通过 document.cookie 属性,可以创建、维护和删除 cookie
//document.cookie="name=Nicholas;domain=nczonline.net;path=/";
<script type="text/javascript">
var cookie = document.cookie;
// cookie 默认的有效期是 会话时间 session 关闭浏览器就失效
var date = new Date();
var timeStamp = date.getTime() + 60*60*1000;
date.setTime(timeStamp)
// 毫秒 + 14 * 24 * 60 * 60 * 1000
console.log("expiresDate:",date);
document.cookie ="name=david; expires="+date.toUTCString();
console.log(cookie);
document.write(document.cookie);
</script>
网友评论