- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将数据输入 cookie 时遇到问题。
数据是一个数组,其尺寸很大,例如product_id、name等等。
但我看到一些电子商务网站的添加到购物车数据功能没有直接存储到cookie中,而只是设置它,因为当我删除cookie时,数据会丢失,但当添加新数据时,内存cookie并没有增加。
所以我的问题是数据存储在哪里?因为我检查过它即使在本地存储中也是空的
我所做的是这样的:我使用数据数组将数据输入到 cookie 中,并将其更改为 JSON.stringify
。在本例中,我只放了 2 个数据,结果大小为 2539,这意味着它太大了。
我的 Controller 添加到购物车
import db from '../../config/conn';
export const addToCart = (req,res) =>{
var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
let reqBody = JSON.stringify(req.body);
let cart = [];
if(typeof req.cookies.cart == "undefined"){
cart.push(reqBody);
}else{
cart = JSON.parse(req.cookies.cart);
cart.push(reqBody);
}
cart = JSON.stringify(cart);
res.cookie('cart', cart, { maxAge: 24 * 60 * 60 * 1000, expired: tomorrow})
return res.status(200).json(cart);
}
服务器
import express from 'express';
import bodyParser from 'body-parser';
// import passport from 'passport';
import connection from './config/conn';
import { CategoryRoutes,ProductRoutes,CartRoutes} from './modules';
import session from 'express-session';
import csrf from 'csurf'
import cors from 'cors'
import cookieParser from 'cookie-parser'
const app = express();
app.use(cookieParser());
app.use(session({
secret: 'iCtkGVe0-4FKIGgBopL2QUM9K-jIK9miZhQE',
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000
}
}));
app.use(csrf({ cookie: true }));
app.use((req,res,next)=>{
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Frame-Options','deny');
res.header('X-Content-Type-Options','nosniff');
res.removeHeader("X-Powered-By");
res.cookie('c_token', req.csrfToken());
next();
})
app.use(cors({ origin:'http://localhost:3000/',credentials:true}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/api/', [CategoryRoutes, ProductRoutes, CartRoutes]);
const port = process.env.PORT || 5000;
app.listen(port, (err) => {
if(err){
console.log(err);
}else{
console.log(`Server running on port ! ${port}`);
}
});
What should I do?how to create Cart system in the right way ?
最佳答案
how to create Cart system in the right way ?
尽管听起来很俗气,但在编程中并没有“正确的方法”来做很多事情。就我个人而言,我倾向于将购物车信息存储在本地存储中,这对于我的情况来说效果很好。将其存储在 cookie 中是可能的,但会产生负面影响,即该 cookie 将随每个请求一起发送,即使对于图像或 api 调用也是如此(假设您的 api 在同一域上运行)。
so my question is where is the data stored?
许多网站不会将其存储在本地存储中,或本地计算机上的任何位置。他们将其存储在服务器上,当您打开页面时,他们会检索它。 Cookie 主要只存储用于从 api 获取购物车商品的身份验证 token 。想象一下,有一个像 http://mywebshop.com/api/cart_items
这样的端点,它返回项目列表。
这种方法在本地存储中存储所没有的优点是,当您移动到另一台计算机(但使用相同的登录名)时,您的购物车仍然存在。另外,在高级场景中,我可以想象当从网站上删除该商品时,想要从所有包含该商品的购物车中删除该商品。
关于javascript - 将数据购物车存储到 cookie 中需要很大的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54134071/
有没有一个简单的答案:为什么 GHC 这么大? OCaml:2MB Python:15MB SBCL:9MB OpenJRE - 26MB GHC:113MB 对“如果 Haskell 是正确的工具,
我发现我的 access_log 占用了我的大部分硬盘。它的大小超过 200 GB。我怎样才能重置它? 我在装有 Plesk 的 CentOS 服务器上使用 Apache 2.2.3。 谢谢你们 !
我正在使用 Java 中的 BeanShell 解释器来解决字符串计算问题。问题是我自己做了一些解释器无法完成的解析并将部分结果存储在 BigInteger 中。然后我将所有内容拼凑起来并交给解释器来
我在我的 android 项目中使用 OpenCV native 库。它是一个带有一个 CameraScreen 的 hello world 项目,它已经有 40mb。我怎样才能减少 apk 的大小,
我使用基于 Laravel 和 Vue.js 的 Laravue Dashboard,在在线服务器上运行,而不是本地主机,它是全新安装,没有进行任何编辑。 我的问题是: 运行 npm run watc
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我广泛使用了 Kendo DataSourceResult ToDataSourceResult(this IQueryable enumerable, DataSourceRequest reque
我使用 phonegap 构建 html\css 应用程序陪审团手机给我 div: 并且模拟器中的最小高度太大,我在底部看到黑线像这样: 如果我在 css 文件中更改 min-height: 736
以下工作正常但速度太慢。只需要知道B表中有匹配的记录,有什么技巧吗? (奇怪的是相反的搜索:找到不加入的记录(IS NULL)非常快) SELECT TableA.id FROM TableA
我正在使用 Vue.js,我的项目中只有 4 个组件。 我只导入了bootstrap、jquery 和lodash: import { map } from 'lodash'; import 'boo
我有 2 个字谜检测功能;一个使用排序和比较,另一个跟踪每个字母字符出现的次数。 这里假设传递给函数的两个字符串是相同的,第一个随机生成(未排序),第二个 = 给第一个,这样两个函数都“一路”执行并返
我正在尝试编写一个脚本,该脚本将通过 HTTP 同时下载最多 N 个文件。 我以前用过 AnyEvent::Worker::Pool管理阻塞任务池。我也用过 AnyEvent::HTTP结合AnyEv
我是一名优秀的程序员,十分优秀!