Good day -- I've conducted many queries on this subject but can't find a good example that fits my situation.
Goal: To call multiple urls using request and async (these urls are different, some are xml and some are json), with the ability to scale the amount of links, with the end result of passing the data to the view for parsing.
Code:
var express = require('express');
var router = express.Router();
var request = require('request');
var parseString = require('xml2js').parseString;
var async = require('async');
var bis;
var url_bis = {
url: 'https://www.bis.org/list/cbspeeches/index.rss',
headers: {
'User-Agent': 'request'
}
};
function callback_bis(error, response, body) {
if (!error && response.statusCode == 200) {
bis = "";
parseString(body, function (err, result) {
bis = result;
bis = bis["rdf:RDF"]["item"];
});
}
}
var doj;
var url_doj = {
url: 'http://www.justice.gov/feeds/opa/justice-news.xml',
headers: {
'User-Agent': 'request'
}
};
function callback_doj (error, response, body) {
if (!error && response.statusCode == 200) {
doj = "";
parseString(body, function (err, result) {
_doj = result;
doj = _doj.rss.channel[0].item;
});
}
}
function feed_doj() {
request(url_doj, callback_doj);
}
function feed_bis() {
request(url_bis, callback_bis);
}
router.get('/', function(req, res, next) {
async.parallel({
bis: feed_bis,
doj: feed_doj
}, function(err, results) {
console.log(results)
// how to get the bis/doj response here in results?
});
});
module.exports = router;
async, but could you not simple store the response of each request in a global variable?