简体   繁体   English

在后端或前端列出集合

[英]List a collection in backend or in frontend

I'm pretty new to NodeJS and I need some advices. 我是NodeJS的新手,我需要一些建议。 I have a page which displays a list of user, I use Mongoose to retrieve this list from the collection. 我有一个页面显示一个用户列表,我使用Mongoose从集合中检索此列表。 I know two ways for displaying this list : 我知道两种显示此列表的方法:

1) Make the query in backend before the page is loaded, then send the result to the view : 1)在加载页面之前在后端进行查询,然后将结果发送到视图:

// app.js
var users = Users.find({}, function(err, results){
    res.render('list', { users: results});
});

2) Make the query on frontend after the page been loaded, in an asynchronous way. 2)加载页面后,以异步方式在前端进行查询。 Example, with Angular 示例,使用角度

// users.js
var User = $resource('/getUsers');
User.query(function (results){
    $scope.users = results;
});

// users.html
<ul>
    <li ng-repeat="user in users"{{ user.name }}></li>
</ul>

// app.js
app.get('getUsers', function(req, res){
    Users.find({}, function(req, results){
        res.json(results);
    })
});

I hope everything is clear. 我希望一切都清楚。 What is the best thing to do, in terms of best practice and/or performance ? 就最佳实践和/或性能而言,最佳方法是什么?

Thanks 谢谢

From the user experience perspective, it is nearly always better to render on the server first, and then only when the content updates, render on the client (or, actually, querying the server for updated HTML content -- is also often better than rendering on the client). 从用户体验的角度来看,首先总是在服务器上进行渲染,然后仅在内容更新,在客户端上进行渲染(或者实际上是在服务器上查询更新的HTML内容)时,总是比渲染更好。在客户端上)。 Full client-side rendering usually results into pages that load longer, use significant CPU resources on the client, especially on mobile, and often have some flickering due to asynchronous requests and many partial updates on the page. 完整的客户端渲染通常会导致页面加载时间更长,在客户端(尤其是在移动设备上)上使用大量的CPU资源,并且由于异步请求和页面上的许多部分更新而经常闪烁。

Having that said, there are situations when client-side rendering makes sense, eg when some content is too long to load at once and it is better to show the page and a progress indicator, eg "we are loading your data...". 话虽这么说,在某些情况下,客户端呈现是有意义的,例如,某些内容太长而无法一次加载,并且最好显示页面和进度指示器,例如“我们正在加载您的数据...” 。 Nevertheless, for most content-oriented websites and portals, nearly complete server-side rendering is a better way to go. 但是,对于大多数面向内容的网站和门户,几乎完整的服务器端渲染是一种更好的方法。

To sort of play devils advocate here, I'm going to have to disagree with @Andrew Skylyarevsky. 为了在这里扮演恶魔的拥护者,我将不得不不同意@Andrew Skylyarevsky。

I consider content driven sites to be on average, more a front end heavy app than a server side app. 我认为以内容为导向的网站平均来说,比服务器端应用程序更像是前端繁重的应用程序。 If the user is being served a page that has content heavily customized to suit their needs, and performance is your goal, you will see the best user experience from serving the customer the bare minimum of content needed to load the page and interact. 如果向用户提供的页面内容经过大量自定义以满足他们的需求,而性能是您的目标,那么从为客户提供加载页面和进行交互所需的最少内容的服务中,您将获得最佳的用户体验。 All further data should be returned via asyc calls such as you would see in a front end heavy app. 所有进一步的数据都应通过asyc调用返回,例如您在前端重型应用程序中看到的那样。 If it were a server side rendered heavy app you as a user would be expecting subsequent requests for content to result in a server-side page reload. 如果它是服务器端渲染的繁重应用程序,那么作为用户,您将期望后续对内容的请求导致服务器端页面重新加载。

I agree with @Andrew that one of the drawbacks of the front end approach is the initial load times where the user has to wait for data to come back and render. 我同意@Andrew的观点,前端方法的缺点之一是初始加载时间,用户必须等待数据返回并呈现。 However, this can and should be mitigated through various techniques. 但是,可以并且应该通过各种技术来减轻这种情况。 Server vs Front end implementations take about the same amount of effort and complexity on your part as the developer. 服务器与前端的实现与开发人员所做的工作量和复杂性差不多。 But if you are looking for a near-seemless experience for the user (performant) you need a well implemented and thought through front end heavy application. 但是,如果您正在寻找对用户(性能高手)近乎仿佛的体验,则需要一个良好的实施方案并仔细考虑前端繁重的应用程序。 Some things should still be done on the back end, but most techniques take place in the front. 某些事情仍然应该在后端完成,但是大多数技术都发生在前端。 Serving the user as little as they need to get going and firing off subsequent requests in the background is one such method. 一种这样的方法就是为用户提供尽可能少的服务,并在后台触发后续请求。 It is not easy to tune this experience. 调整这种体验并不容易。

I should note that I'm coming from a background of having worked on several enterprise level applications that sat in the front end of a browser. 我应该注意,我来自从事多个位于浏览器前端的企业级应用程序的工作背景。 I have also migrated many server-side applications to front end applications and have seen the benefits both in terms of performance and user experience. 我还将许多服务器端应用程序迁移到了前端应用程序,并且看到了性能和用户体验方面的好处。 So I am biased. 所以我有偏见。 Just some food for thought. 只是需要深思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM