多个HTTP异步请求

时间:2017-10-28 17:13:31

标签: javascript node.js http asynchronous callback

我尝试从不同的HTTP源获取数据,但即使使用async我也无法处理异步模式......

var express = require('express');
var app = express();
var https = require("https");
var timer = require("./my_modules/timer/timer.js");
var http = require('http');
var bodyParser = require('body-parser');
var async = require('async');
var request = require('request');

//These are my source from API.
//Output is a Json file
var sources = {
  cnn: 'https://newsapi.org/v1/articles?source=cnn&sortBy?&apiKey=c6b3fe2e86d54cae8dcb10dc77d5c5fc',
  bbc: 'https://newsapi.org/v1/articles?source=cnn&sortBy?&apiKey=c6b3fe2e86d54cae8dcb10dc77d5c5fc',
  guardian: 'https://newsapi.org/v1/articles?source=cnn&sortBy?&apiKey=c6b3fe2e86d54cae8dcb10dc77d5c5fc',
  othersource: "otherurls..."
};

//I want to push the JSON object in this array
var resultArray = [];

//I setup a https GET request
var getJson = function(url) {
  https.get(url, (res) => {
    var body = '';
    res.on('data', function(chunk) {
      body += chunk;
    });

    res.on('end', function() {
      result = JSON.parse(body);

      //push isn't working...
      resultArray.push(result);
    });

  }).on('error', function(e) {
    console.log('Got an error', e);
  });
}

app.set('port', (process.env.PORT || 5000));
app.listen(
  app.get('port'), () => {
    console.log('We are live on port: ', app.get('port'));
    getJson(sources.cnn);
  });


app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());
app.use(function(req, res, next) {
  res.setHeader('Content-Type', 'text/plain');
  res.status(404).send('Page not found !');
  res.status(503).send('Page not found, error 503');
});

console.log("resultArray:" + resultArray);
//resultArray  = empty...

如何将结果推送到我的阵列? 我无法找到一种方法来设置一个工作回调函数来将结果推送到数组中。

2 个答案:

答案 0 :(得分:1)

由于您已经在使用request软件包,您是否尝试过以下简单的方法:

request({
    url: sources.cnn,
    json: true
}, function(error, response, body) {
    var articles = body.articles;

    // or by case, depending on what you want
    // resultArray = resultArray.concat(articles);
    resultArray.push({
        cnn: articles
    });

    console.log(resultArray);
});

而不是编写自己的getJson函数?

答案 1 :(得分:0)

感谢Roby,您的要求更加清晰!

我仔细阅读了这篇非常明确且有帮助的文章:https://github.com/maxogden/art-of-node#callbacks

我认为我有逻辑:

//main function
function getJson(url, callback){
  request({
      url: url,
      json: true,
      callback:callback  //added this
      }, function(error, response, body) {

          var articles = body.articles;
          callback(articles);
      });
}

//this callback function will be passed to the main function as the 2nd parameter
//it's possible to access "resultArray" ONLY from this function
var result = function(e){
  resultArray.push(e);
  console.log(resultArray);
  };

//url and callback are passed as parameter
getJson(sources.cnn, result);

感谢您的帮助