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

查看:64
本文介绍了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"通过:哈希密码"...} 

我的用户可以属于

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

他们也可以具有约束或可以从其组中继承约束

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

问题:

我正在创建一个管理页面,显示所有用户,他们的组,他们的约束.

我应该:

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

类似的东西:

  $ 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. 创建路径/api/users/withConstraintsAndGroups

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


我发现解决方案 1 非常好,可维护,简单且通用,但我对性能很差感到敬佩

我发现解决方案 2 丑陋,难以维护,难以编写代码,不是通用的,但具有良好的性能

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

解决方案

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

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

要记住的一件事是客户端和服务器之间的预期网络延迟(ping时间).在带宽还不错的高延迟情况下,许多小请求将比一个大请求执行得更差.

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

话虽如此,我还是建议 still 建议您选项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天全站免登陆