I am currently extracting data from a website and attempting to display it in the browser as JSON. However, although I can see the data when using console.log()
on the recipes array, nothing is being sent to the browser. Why is the JSON array not showing up in the browser?
router.get('/scrape', function(req, res, next) {
res.json(scrapeUrl("http://url"));
})
function scrapeUrl(url) {
request(url, function(error, response, html){
// First we'll check to make sure no errors occurred when making the request
if(!error){
var $ = cheerio.load(html);
var recipes = [];
$('div.article-block a.picture').each(function(i, elem) {
var deepUrl = $(this).attr('href');
if(!$(this).attr('href').indexOf("tema") > -1) {
request(deepUrl, function(error, response, html){
// First we'll check to make sure no errors occurred when making the request
if(!error){
var $ = cheerio.load(html);
var image = $('div.article div.article-main-pic img').attr('src');
var title = $('div.recipe h2.fn').text();
var object = {url: deepUrl, title : title, image : image};
recipes.push(object);
}
});
}
});
return recipes;
}
});
}