我应该做API请求服务器端还是客户端?

时间:2022-02-19 16:37:41

I am trying to make a web app using ExpressJS and Coffeescript that pulls data from Amazon, LastFM, and Bing's web API's.
Users can request data such as the prices for a specific album from a specific band, upcoming concert times and locations for a band, etc... stuff like that.

我正在尝试使用ExpressJS和Coffeescript制作一个web应用程序,从Amazon、LastFM和Bing的web API中提取数据。用户可以请求数据,例如特定乐队的特定专辑价格,即将到来的音乐会时间和乐队的地点等。诸如此类。

My question is: should I make these API calls client-side using jQuery and getJSON or should they be server-side? I've done client-side requests; how would I even make an API call from the server side?
I just want to know what the best practice is, and also if someone could point me in the right direction for making server-side API requests, that would be very helpful.

我的问题是:我应该使用jQuery和getJSON使这些API调用客户端还是服务器端?我做客户端请求;我如何从服务器端发出API调用呢?我只是想知道最佳实践是什么,而且如果有人能够指出我在做服务器端API请求时的正确方向,那将非常有帮助。

Thanks!

谢谢!

3 个解决方案

#1


3  

It's often good to abstract away your storage and dependent services to isolate changes and offer a consolidated and consistent web api for your application. But sometimes, if you have a good hypermedia web api (RESTful responses link to other resources), you could reference a resource link from another service in the response from your service (ex: SO request could reference gravatar image/resource of user). There's no one size fits all - it depends on whether you want to encapsulate the dependency or integrate with it.

抽象出存储和依赖服务来隔离更改并为应用程序提供统一的、一致的web api通常是件好事。但有时,如果您有一个好的超媒体web api (RESTful响应链接到其他资源),您可以在服务的响应中引用来自另一个服务的资源链接(例如:SO request可以引用用户的gravatar映像/资源)。没有一种大小适合所有类型——这取决于您是否想封装依赖项或与之集成。

It might be beneficial to make the web-api requests from your service exposed via expressjs as your own web-apis.

将来自您的服务的web api请求通过expressjs公开为您自己的web api可能是有益的。

Making http web-api requests is easy from node. Here's another SO post covering that:

从节点发出http web-api请求很容易。这是另一篇关于这个的文章:

HTTP GET Request in Node.js Express

HTTP GET请求在节点中。js表达

#2


6  

There's are two key considerations for this question:

对于这个问题,有两个关键的考虑:

  1. Do calls incur any data access? Are the results just going to be written to the screen?
  2. 调用是否引发任何数据访问?结果会写在屏幕上吗?
  3. How & where do you plan to handle errors? How do you handle throttling?
  4. 您打算如何和在哪里处理错误?如何处理节流?

Item #2 is really important here because web services go down all of the time for a whole host of reasons. Your calls to Bing, Amazon & Last FM will fail probably 1% or 0.1% of the time (based on my experiences here).

项目#2在这里非常重要,因为web服务由于很多原因一直在下降。你给Bing, Amazon & Last FM的电话可能有1%或0.1%的时间会失败(基于我在这里的经验)。

To make requests users server-side JS you probably want to take a look at the Request package on NPM.

要让请求用户使用服务器端JS,您可能需要查看NPM上的请求包。

#3


1  

well, the way you describe it I think you may want to fetch data from amazon, lastfm and so on, process it with node, save it in your database and provide your own api.

你描述它的方式我想你可能想从amazon, lastfm等获取数据,用node处理它,保存在你的数据库中,并提供你自己的api。

you can use node's http.request() to fetch the data and build your own rest api with express.js

可以使用node的http.request()获取数据,并使用express.js构建自己的rest api

#1


3  

It's often good to abstract away your storage and dependent services to isolate changes and offer a consolidated and consistent web api for your application. But sometimes, if you have a good hypermedia web api (RESTful responses link to other resources), you could reference a resource link from another service in the response from your service (ex: SO request could reference gravatar image/resource of user). There's no one size fits all - it depends on whether you want to encapsulate the dependency or integrate with it.

抽象出存储和依赖服务来隔离更改并为应用程序提供统一的、一致的web api通常是件好事。但有时,如果您有一个好的超媒体web api (RESTful响应链接到其他资源),您可以在服务的响应中引用来自另一个服务的资源链接(例如:SO request可以引用用户的gravatar映像/资源)。没有一种大小适合所有类型——这取决于您是否想封装依赖项或与之集成。

It might be beneficial to make the web-api requests from your service exposed via expressjs as your own web-apis.

将来自您的服务的web api请求通过expressjs公开为您自己的web api可能是有益的。

Making http web-api requests is easy from node. Here's another SO post covering that:

从节点发出http web-api请求很容易。这是另一篇关于这个的文章:

HTTP GET Request in Node.js Express

HTTP GET请求在节点中。js表达

#2


6  

There's are two key considerations for this question:

对于这个问题,有两个关键的考虑:

  1. Do calls incur any data access? Are the results just going to be written to the screen?
  2. 调用是否引发任何数据访问?结果会写在屏幕上吗?
  3. How & where do you plan to handle errors? How do you handle throttling?
  4. 您打算如何和在哪里处理错误?如何处理节流?

Item #2 is really important here because web services go down all of the time for a whole host of reasons. Your calls to Bing, Amazon & Last FM will fail probably 1% or 0.1% of the time (based on my experiences here).

项目#2在这里非常重要,因为web服务由于很多原因一直在下降。你给Bing, Amazon & Last FM的电话可能有1%或0.1%的时间会失败(基于我在这里的经验)。

To make requests users server-side JS you probably want to take a look at the Request package on NPM.

要让请求用户使用服务器端JS,您可能需要查看NPM上的请求包。

#3


1  

well, the way you describe it I think you may want to fetch data from amazon, lastfm and so on, process it with node, save it in your database and provide your own api.

你描述它的方式我想你可能想从amazon, lastfm等获取数据,用node处理它,保存在你的数据库中,并提供你自己的api。

you can use node's http.request() to fetch the data and build your own rest api with express.js

可以使用node的http.request()获取数据,并使用express.js构建自己的rest api