REST:返回复杂的嵌套数据与多次调用 [英] REST : Return complex nested data vs. multiple calls

查看:22
本文介绍了REST:返回复杂的嵌套数据与多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 NodeJS 提供给 AngularJS 前端的 REST api.

我与用户合作:

GET/api/users #返回所有用户POST/api/users #创建新用户GET/api/users/:id #返回一个用户PUT/api/users/:id #编辑用户DELTE/api/users/:id #删除一个用户

这是一个用户:

<代码>{登录:管理员"电子邮件:admin@admin.com"通过:哈希密码"...}

我的用户可以属于groups

GET/api/users/:id/groups #返回用户所属的组

他们也可以有constraints或者可以从他们的组中继承constraints

GET/api/users/:id/constraints #返回用户的约束条件GET/api/groups/:id/constraints #返回一个组的约束

问题:

我正在制作一个管理页面,显示所有用户、他们的组、他们的限制.

我应该:

  1. 在 javascript (Angular) 前端的 for 循环 中发出许多请求?

类似:

$http.get(/api/users).then(function(result) {result.data.forEach(函数(用户){$http.get('/api/users/'+user.data.id+'/groups).then(function(groups) {组.forEach(功能(组){$http.get('/api/groups/'+group.data.id+'/constraints)})})})})

  1. 创建路径/api/users/withConstraintsAndGroups

这将返回一个包含所有用户及其组和约束的大列表.

<小时>

我发现解决方案1非常好、可维护、简单且通用,但我担心性能很差

我发现解决方案2丑陋、难以维护、难以编码、不通用但性能良好

我应该选择哪种解决方案?

解决方案

你的问题基本上可以归结为:

<块引用>

哪个更好,一个大的 HTTP 请求还是许多小的请求?

要记住的一件事是客户端和服务器之间的预期网络延迟(ping 时间).在具有良好带宽的高延迟情况下,许多小请求的执行情况将明显比一个大请求的表现要差很多.

此外,有一个大请求可以提高压缩响应的效率,并避免了 额外的 HTTP 请求和响应标头的开销.>

说了这么多,我仍然建议您选项 1 开始,仅仅因为您说编码对您来说非常容易.然后看看它是否符合您的要求.如果没有,那么尝试选项 2.

最后一点,我通常更喜欢一个大请求而不是许多小请求,因为这样可以轻松地将每个请求定义为一个完全应用或回滚的事务.这样我的客户就不会遇到这样一种状态,即他们的许多小请求中的一些成功而另一些请求失败,并且他们现在拥有我的 API 提供的数据的不一致本地副本.

I have a REST api served by NodeJS to an AngularJS fronts.

I work with users :

GET  /api/users  #Returns all users
POST /api/users  #Create  new user

GET   /api/users/:id   #Return a user
PUT   /api/users/:id   #Edit a user
DELTE /api/users/:id   #Delete a user

This is a user :

{
  login : "admin"
  email : "admin@admin.com"
  pass  : "hashedpassword"
  ...
}

My user can belong to groups

GET   /api/users/:id/groups   #Return the groups of a user

They can also have constraints or can inherits constraints form their groups

GET   /api/users/:id/constraints   #Return the constraints of a user
GET   /api/groups/:id/constraints   #Return the constraints of a group

The problem :

I'm making an admin page, displaying All the users, their groups, their constraints.

Should I :

  1. Make many requests in a for loop in the javascript (Angular) front ?

Something like :

$http.get(/api/users).then(function(result) {
  result.data.forEach(function(user) {
    $http.get('/api/users/'+user.data.id+'/groups).then(function(groups) {
      groups.forEach(function(group) {
        $http.get('/api/groups/'+group.data.id+'/constraints)
      })
    })
  })
})

  1. Create a path /api/users/withConstraintsAndGroups

That would return a big list of all the users with their groups and their constraints.


I find solution 1 to be very nice, maintenable, easy, and generic but I'm affraid of very bad performance

I find solution 2 to be ugly, hard to maintain, hard to code, not generic but with good performance

Which solution should I choose?

解决方案

Your question basically boils down to:

Which is better, one big HTTP request, or many small ones?

One thing to keep in mind is the expected network latency (ping time) between your clients and your server. In a high-latency situation with otherwise good bandwidth, many small requests will perform significantly worse than one large one.

Also, having one big request gives you better efficiency for compressing the response, and avoids the overhead of the extra HTTP requests and response headers.

Having said all that, I would still advise you to start with option 1, solely because you stated it is very easy for you to code. And then see if it meets your requirements. If not, then try option 2.

And as a final note, I generally prefer one big request to many small requests because that makes it easy to define each request as a transaction which is either completely applied or rolled back. That way my clients do not run into a state where some of their many small requests succeeded while others failed and they now have an inconsistent local copy of the data my API supplied.

这篇关于REST:返回复杂的嵌套数据与多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆