Monday, May 20, 2024
23
rated 0 times [  28] [ 5]  / answers: 1 / hits: 54581  / 11 Years ago, mon, april 22, 2013, 12:00:00

Is it sensible to use Node.js to write a stand alone app that will connect two REST API's?



One end will be a POS - Point of sale - system



The other will be a hosted eCommerce platform



There will be a minimal interface for configuration of the service. nothing more.


More From » web-services

 Answers
26

Yes, Node.js is perfectly suited to making calls to external APIs. Just like everything in Node, however, the functions for making these calls are based around events, which means doing things like buffering response data as opposed to receiving a single completed response.



For example:



// get walking directions from central park to the empire state building
var http = require(http);
url = http://maps.googleapis.com/maps/api/directions/json?origin=Central Park&destination=Empire State Building&sensor=false&mode=walking;

// get is a simple wrapper for request()
// which sets the http method to GET
var request = http.get(url, function (response) {
// data is streamed in chunks from the server
// so we have to handle the data event
var buffer = ,
data,
route;

response.on(data, function (chunk) {
buffer += chunk;
});

response.on(end, function (err) {
// finished transferring data
// dump the raw data
console.log(buffer);
console.log(n);
data = JSON.parse(buffer);
route = data.routes[0];

// extract the distance and time
console.log(Walking Distance: + route.legs[0].distance.text);
console.log(Time: + route.legs[0].duration.text);
});
});


It may make sense to find a simple wrapper library (or write your own) if you are going to be making a lot of these calls.


[#78719] Sunday, April 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maya

Total Points: 418
Total Questions: 116
Total Answers: 112

Location: Mauritania
Member since Sun, Oct 17, 2021
3 Years ago
;