I have a function which reads the data from a web page and post it in the console,but I want to return the data which I've read and do somethink else with it.So instead of console.log(temperature) I want to return the temperature value and do console.log(download(url)) but It post 'undefined'.
//require https module
var https=require("https");
//require cheerio to use jquery-like and return a DOM tree
var cheerio=require('cheerio');
var global="";
//a function with url and callback par which connects to URL API and read the data
module.exports.download=function(url){
//read the data\
https.get(url,function(res){
var data="";
//add it to the data string
res.on('data',function(chunk){
data+=chunk;
});
//parse it with a callback function
res.on('end',function(){
var $=cheerio.load(data);
var temperature=$("span.temp.swip").text();
console.log(temperature);
});
}).on('error',function(err){
console.log(err.message)
});
}
//chose the url to connect
var Ploiesti='44.9417,26.0237';
var Brasov='45.597,25.5525';
var url='https://darksky.net/forecast/' + Ploiesti + '/si24/en';
//download(url);