Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I do API requests server side or client side?

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.

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.

Thanks!

like image 733
aeyang Avatar asked Oct 02 '12 05:10

aeyang


2 Answers

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. How & where do you plan to handle errors? How do you handle throttling?

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).

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

like image 94
Gates VP Avatar answered Oct 14 '22 08:10

Gates VP


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.

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

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

HTTP GET Request in Node.js Express

like image 32
bryanmac Avatar answered Oct 14 '22 09:10

bryanmac