Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain multiple Node http request

I am new to node and need to call a 3rd party api from my code. I found how to do this by using http.request from this link https://docs.nodejitsu.com/articles/HTTP/clients/how-to-create-a-HTTP-request. What I need to do is call two different api urls and use the response data from the first call in the second call which will just be an id as a param on resource2.

I do not know how I would chain two of these calls together without it being a duplicated mess. Any help would be appreciated.

var url1 = {
    host: 'www.domain.com',
    path: '/api/resourse1'
};

var url2 = {
    host: 'www.domain.com',
    path: '/api/resourse2/{id}'
};

var callback = function (response) {
    var str = '';

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

    response.on('end', function () {
        console.log(str);
    });
}

http.request(url1, callback).end();
like image 244
Sealer_05 Avatar asked Jan 17 '16 06:01

Sealer_05


People also ask

Can NodeJS handle multiple requests?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.

How many requests can node handle?

They handle 40K requests per second having Node. js (mostly) for the backend.

CAN NodeJS handle million users?

js as a platform has a few limitations on its own that we have to accept. However, with proper logging, monitoring, in-depth understanding of platforms and tooling you can scale & serve millions of customers in real-time.


1 Answers

Firstly, you'll want to take a look at request, which is most popular choice for HTTP requests, due to its simplicity.

Secondly, we can combine the simplicity of request with the concept of Promises, to make multiple requests in succession, while keeping the code flat.
Using request-promise

var rp = require('request-promise')
var url1 = {}
var url2 = {}
var url3 = {}


rp(url1)
  .then(response => {
    // add stuff from url1 response to url2
    return rp(url2)
  })
  .then(response => {
    // add stuff from url2 response to url3
    return rp(url3)
  })
  .then(response => {
    // do stuff after all requests

    // If something went wrong
    // throw new Error('messed up')
  })
  .catch(err => console.log) // Don't forget to catch errors

As you can see, we can add as many requests as we want, and the code will stay flat and simple. As a bonus, we were able to add error-handling as well. Using traditional callbacks, you'd have to add error-handling to every callback, whereas here we only have to do it once at the end of the Promise chain.

UPDATE (09/16): While Promises take us halfway there, further experience has convinced me that Promises alone get messy when there is a lot of mixing between sync, async code, and especially control flow (e.g. if-else). The canonical way to solve this would be with async/await, however that is still in development and would require transpilation. As such, generators are the next best solution.

Using co

var co = require('co')
var rp = require('request-promise')
var url1 = {}
var url2 = {}
var url3 = {}

co(function* () {
  var response
  response = yield rp(url1)
  // add stuff from url1 response to url2
  response = yield rp(url2)
  // add stuff from url2 response to url3
  response = yield rp(url3)

  // do stuff after all requests

  // If something went wrong
  // throw new Error('messed up')
})
.catch(err => console.log) // Don't forget to catch errors

UPDATE (12/16): Now that the latest version of node at time of writing (7.2.1) supports async/await behind the --harmony flag, you could do this:

const rp = require('request-promise')
const url1 = {}
const url2 = {}
const url3 = {}

async function doRequests() {
  let response
  response = await rp(url1)
  // add stuff from url1 response to url2
  response = await rp(url2)
  // add stuff from url2 response to url3
  response = await rp(url3)

  // do stuff after all requests

  // If something went wrong
  // throw new Error('messed up')
}

doRequests()
.catch(err => console.log) // Don't forget to catch errors
like image 173
Prashanth Chandra Avatar answered Sep 17 '22 16:09

Prashanth Chandra