Using Node.js to connect to a REST API

52,110

Solution 1

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.

Solution 2

Sure. The node.js API contains methods to make HTTP requests:

I assume the app you're writing is a web app. You might want to use a framework like Express to remove some of the grunt work (see also this question on node.js web frameworks).

Share:
52,110
AndrewMcLagan
Author by

AndrewMcLagan

I’m a full stack Web Developer with over fifteen years experience in the industry. I concentrate on open source languages and technologies such as PHP 5.6+, Python, Javascript EMCA6, AngularJS, React, Laravel and Symfony. My strengths would be a very strong interest in the open source community and approaching web­app development from a software design perspective rather than simply being a code­monkey. Strong understanding of basic SOLID principals and the patterns that make these principles a reality. Coming from many years experience as a contract developer, keeping up­-to-­date with the open source community, tools and best practices has been paramount to my career. Also, I LOVE to code!

Updated on July 11, 2022

Comments

  • AndrewMcLagan
    AndrewMcLagan almost 2 years

    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.

    • Gabriel Llamas
      Gabriel Llamas about 11 years
      Yes, it's ok. I don't see why you cannot use node.js for that purpose.
  • AndrewMcLagan
    AndrewMcLagan about 11 years
    I really warm to the node evented model. when data is chunked in like this. is it possible to begin manipulating it before the stream has ended? does it arrive in order?
  • Robert Mitchell
    Robert Mitchell about 11 years
    Thanks! Yes, the data is streamed in order. If you are able to use the data before streaming is complete, I don't see why you couldn't work with it before then (although I personally haven't had a use case for it yet).
  • Kasra
    Kasra almost 7 years
    "res" is undefined!
  • Schubert David Rodriguez
    Schubert David Rodriguez almost 7 years
    Yo must put it into a route in app.get('/', auth.protected, function (req, res){ });