简体   繁体   English

优化传统REST API数据源和Falcor客户端之间的node.js falcor路由器

[英]Optimising a node.js falcor router between a traditional REST API data source and a Falcor client

I have a traditional REST API, that returns data like so: 我有一个传统的REST API,它返回如下数据:

User list - GET /users.json 用户列表 - GET /users.json

users: [
    {id: 0, name: "John Smith"},
    ...
]

Users by Id - GET /users/0.json 用户ID: GET /users/0.json

user: {
    id: 0,
    name: "John Smith"
}

As you can see, if you first request the data from the list ( /users.json ), and then click on a user, even though the information was present in the first list, it is re-requested from the user by id request. 如您所见,如果您首先从列表中请求数据( /users.json ),然后单击某个用户,即使该信息存在于第一个列表中,也会通过id请求从用户重新请求该信息。 。

In Falcor, this would be solved by having a list of references in the first call. 在Falcor中,这将通过在第一次调用中具有引用列表来解决。

My question is, if I am writing a Falcor router to act as a middleman, how do I optimise such a scenario? 我的问题是,如果我正在编写Falcor路由器作为中间人,我该如何优化这种情况? Currently, the router has to request the full user list, and then throw out the information and return a list of references based on the ids to the client. 目前,路由器必须请求完整的用户列表,然后丢弃信息并将基于ID的引用列表返回给客户端。 This still saves bandwidth on the client side but is suboptimal between the Falcor router and its datasource (REST API). 这仍然节省了客户端的带宽,但在Falcor路由器及其数据源(REST API)之间并不是最理想的。

It is possible to workaround this situation, but first, I would like to explain why you are seeing this mismatch. 可以解决这种情况,但首先,我想解释为什么你看到这种不匹配。 It is because Falcor respect REST principles, but your API does not. 这是因为Falcor尊重REST原则,但您的API却没有。 REST states that data coming from the API should be cacheable. REST声明来自API的数据应该是可缓存的。 It cannot be cacheable if it resides in two places at once. 如果它一次驻留在两个地方,则无法缓存。 For example, if I were to PUT or PATCH /users/0.json , how can the client know that this operation has an impact on /users.json (a different resource) and invalidate its cache? 例如,如果我是PUTPATCH /users/0.json ,客户端如何知道此操作对/users.json (一个不同的资源)有影响并使其缓存无效? It cannot. 这不可以。 In fully-compliant HTTP REST APIs and Falcor APIs alike, data resides in one place only, and then it can be linked to via refs. 在完全兼容的HTTP REST API和Falcor API中,数据只驻留在一个地方,然后它可以链接到via refs。 For HTTP, refs are URLs, and so a GET call to /users.json should respond with a list of URLs like ["/users/0.json", "/users/1.json"] . 对于HTTP,refs是URL,因此对/users.jsonGET调用应该响应一个URL列表,如["/users/0.json", "/users/1.json"]

That said, it does not mean you are out of luck. 也就是说,这并不意味着你运气不好。

What you probably want on the Falcor side is to have a route like this: users[{integers:indices}][{keys:props}] . 在Falcor方面你可能想要的是拥有这样的路线: users[{integers:indices}][{keys:props}] In the handler for this route, you can query pathSet.indices and see how much indices were actually requested. 在此路由的处理程序中,您可以查询pathSet.indices并查看实际请求了多少索引。 If there is only one (or few), forward the request to /users/${indices[i]}.json , otherwise forward it to /users.json . 如果只有一个(或几个),请将请求转发给/users/${indices[i]}.json ,否则将其转发给/users.json

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

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