简体   繁体   中英

How to combine API GET request in nodejs?

I have several API Get request at once in nodejs. Each API have new data every couple minutes.

var express = require('express');
var router = express.Router();
var request = require("request");

let value1, value2, bodyData1, bodyData2;

var options = { method: 'GET',
  url: 'https://api.example.com/data1',
  qs: 
   { 
     valueType: 'MAXIMUM'
   },
  headers: 
   { 
     authorization: 'ABC123456',
     accept: 'application/json; charset=utf-8' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  bodyData1 = JSON.parse(body);
  value1 = bodyData1.value;
});

var options = { method: 'GET',
  url: 'https://api.example.com/data2',
  qs: 
   { 
     valueType: 'MAXIMUM'
   },
  headers: 
   { 
     authorization: 'ABC123456',
     accept: 'application/json; charset=utf-8' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  bodyData2 = JSON.parse(body);
  value2 = bodyData2.value;
});

router.get('/', function(req, res, next) {
  res.render('home', {valueA : value1, valueB: value2});
});

module.exports = router;

I want to know if it is possible to combine them into one function? Any other things I should concern?

You can use Redis cache to store data in memory for fast retrieval and fetch from memory very quickly.

Also, after some interval, you can add them to a database through bulk creation. It will decrease your database call.

// Example in sequilize

await db.table_name.bulkcreate([ {0bj1}, {obj2}..,{obj3 } ]);

It is possible if you have promises which is currently not the case. You have to wrap your request() call in a Promise. You can do it manually with a custom function requestToPromise .

You can then use Promise.all to call multiple promises in parallel.

function requestToPromise(options) {
  return new Promise((resolve, reject) => {
    request(options, (error, response, body) => {
      if (error) return reject(error);
      resolve(body);
    });
  });
}

var optionsRequest1 = {
  method: "GET",
  url: "https://api.example.com/data1",
  qs: {
    valueType: "MAXIMUM"
  },
  headers: {
    authorization: "ABC123456",
    accept: "application/json; charset=utf-8"
  }
};
var optionsRequest2 = {
  method: "GET",
  url: "https://api.example.com/data2",
  qs: {
    valueType: "MAXIMUM"
  },
  headers: {
    authorization: "ABC123456",
    accept: "application/json; charset=utf-8"
  }
};

var requestPromise1 = requestToPromise(optionsRequest1);
var requestPromise2 = requestToPromise(optionsRequest2);
Promise.all([requestPromise1, requestPromise2]).then(results => {
  var [resultPromise1, resultPromise2] = results;
}).catch(error => {
  //handle error
});

Instead of using the custom function requestToPromise you can also use util.promisify

const util = require('util');

const requestAsync = util.promisify(request);
Promise.all([requestAsync(optionsRequest1), requestAsync(optionsRequest2)]).then(results => {
  var [resultPromise1, resultPromise2] = results;
}).catch(error => {
  //handle error
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM