SlartyBartfast
SlartyBartfast

Reputation: 311

API aggregation in node.js

I am trying to aggregate several location API systems (Google, Foursquare, etc...) to provide diverse location data to users. Delivery is based on user location resulting in a lat, long and radius search.

I have scoured the web for specific API aggregation ideas, with not much luck, I have only found many clever libraries for server side HTTP requests. What I am looking for specifically is opinions on best implimentation of geoJson aggregation.

My options (as I see them) are:


1. Expose multiple location based APIs (Google, Foursquare, etc..) to the user on the front end, i.e. query the APIs each time the user moves there position.

Advantages

Disadvantages


2. Somehow aggregate API calls on the server based on user location (mostly static), update aggregation with 'cron job' as required. Therefore data delivery is effectivley pre-cached and can be paginated more easily depending on user preferences.

Advantages

Disadvantages


I realize I can achieve this with a http.get calls populating a database (lets say mongo due to geospatial queries) on the backend. It just seems like a common use case and I cannot find any reference to it.

Does anyone have any experience with this in Node? I feel like im missing something. Thanks

Upvotes: 0

Views: 2025

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

You don't see much about this because it's such a common task and you are asking the internet the wrong questions.

You are describing aggregating multiple http request results, something which a google search would turn up for you.

ES6 promises make this easy and elegant:

Promise.all([
    fetch("api.com/things"),
    fetch("api2.com/otherthings"),
    fetch("api3.com/yetotherthings")
    ])
    .then(res=> {
      //do something with res[0], res[1], res[2]
    })

Theoretically, you could do it all on the client side just as well, using the same exact pattern. However, more than likely some of the apis you want to call won't allow themselves to be called from the browser (CORS violations). You'll most likely have to stand up a server to act as a proxy that can pass calls onto the apis that don't support browser requests.

Of course there's more to do. Promise.all will fail if any individual promise fails. So you'll have to handle error possibilities in each call. Also, your api calls may be significantly more complex to construct than the simple gets above. You mentioned caching results, etc...

But the above is the basic idea.

Upvotes: 5

Related Questions