【发布时间】:2016-01-06 04:28:08
【问题描述】:
我刚刚学会了如何在客户端 JS 中访问 express 局部变量:
https://stackoverflow.com/questions/34618426/express-handlebars-local-variables-in-client-side-js
但这打开了一个全新的蠕虫罐...现在我不知道在服务器端(在路由内)做什么数据操作以及在客户端做什么。
例如: 这里我将每个数据库项的 description 属性进行剪切,以便前 100 个字符可以用作精选列表拼贴中的预览。
//routes/index.js
router.get('/', isAuthenticated, function (req, res, next) {
//Fetching data to populate newest listings collage
User.find({role: 'organization'}).sort({datecreated: -1}).limit(6).exec(function (err, docs) {
docs.forEach(function(item){
item.description = item.description.slice(0,100);
});
console.log(docs[0].description);
res.render('index.hbs', {
user: req.user,
orgsfeatured: docs,
message: req.flash('message')
})
});
});
将整个对象数组发送到客户端javascript进行切片是否更典型/高性能/可维护?
我知道这是客户端 CPU 负载与服务器负载之间的平衡,但必须有一些最佳实践。如果我不清楚,请告诉我。 谢谢!!
【问题讨论】:
-
只是另一个想法,不知道它是否是真正的答案,但为什么不将预览也保留在数据库中?
标签: javascript node.js express