跳到主要内容

语法

2024年04月08日
柏拉文
越努力,越幸运

一、Web


安装js-cookie

yarn add js-cookie -S

封装Cookie常规操作

import Cookies from "js-cookie";

export const getCookie = (key) => {
return Cookies.get(key);
};

export const setCookie = (key, value) => {
return Cookies.set(key, value);
};

export const removeCookie = (key) => {
return Cookies.remove(key);
};

二、Node


Koa 通过原生方法操作 Cookie

// 获取 Cookie 
ctx.cookies.get('token')

// 设置 Cookie
ctx.cookies.set("token", token, {
domain: "localhost",
path: "/",
maxAge: 1000 * 60 * 60 * 1,
expires: new Date(),
httpOnly: false,
overwrite: false,
});