Should I do API requests server side or client side?
Solution 1
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
Solution 2
There's are two key considerations for this question:
- Do calls incur any data access? Are the results just going to be written to the screen?
- 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.
Solution 3
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.
you can use node's http.request() to fetch the data and build your own rest api with express.js
aeyang
Updated on June 15, 2022Comments
-
aeyang almost 2 years
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
andgetJSON
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!
-
aeyang over 11 yearsThanks for the link. The two answers below yours have both mentioned
http.request
. Im wondering how theRequest
npm package is different? Is it simpler to use? -
aeyang over 11 yearsThank you for the link. I will give this a try. Can I also ask, where did you put this getJSON module in the structure of your app? Lets say I won't store the results of the call to my DB.
-
aeyang over 11 yearsAlso, on your other post, you mentioned you had a more complete sample. Is that offer still good?
-
bryanmac over 11 yearsSure, for playing around and a presentation I wrote a students rest service that talked to Parse.com with http requests for storage: github.com/bryanmacfarlane/nodefun/tree/master/express/…
-
bryanmac over 11 yearsNote that it's 'playing around' quality for demo and secrets.js needs the parse.com keys replaced for it to work.
-
Gates VP over 11 yearsThe
http.request
library is very low-level access. TheRequest
library in NPM has support for things like OAuth, cookies, streaming and form submissions. If you're trying to proxy calls out to third parties, you will want this tool. -
aeyang over 11 yearsThank You. Ill definitely be trying this!