📅  最后修改于: 2023-12-03 14:44:46.509000             🧑  作者: Mango
如果您正在构建一个Web应用程序,那么使用cookie是一个必不可少的组件。而strong-cookie则是一个在Node.js环境中操作cookie的优秀模块。本文中,我们将介绍如何使用npm在Shell-Bash中安装strong-cookie模块。
npm install strong-cookie
+ strong-cookie@0.x.x
added x packages from x contributors and audited x packages in x.xx s
found x vulnerabilities (x low, x moderate, x high)
run `npm audit fix` to fix them, or `npm audit` for details
const Cookie = require('strong-cookie');
const Cookie = require('strong-cookie');
//设置Cookie
let cookie = new Cookie('key', 'value');
cookie.httpOnly = true;
cookie.path = '/';
cookie.domain = 'example.com';
cookie.expires = new Date(Date.now() + 3600000); //过期时间一小时
cookie.secure = true;
//获取Cookie
let header_value = Cookie.serialize('session_id', '5Kk6UW8rUvC6Kjx6', {
httpOnly: true,
maxAge: 60*60*24,
path: '/',
domain: 'example.com',
secure: true
});
//删除Cookie
Cookie.parse(header_value).forEach(cookie => {
cookie.expires = new Date();
cookie.value = '';
cookie.httpOnly = true;
document.cookie = cookie.toString();
});
强大的strong-cookie模块提供了各种操作Web应用程序中cookie的工具。在Shell-Bash中安装它以简化您的开发工作,并开始创建更好的Web应用程序吧!