Thursday 21 July 2016

javascript - NodeJS http.request not returning data even after specifying return on the 'end' event

Basically I am trying to scrap some data from website and perform the DOM extraction, deletion and updation on a callback function binded to the 'end' event of http.request.



I have returned the data from the 'end' event callback too but it is not receiving in my route callback function. I get undefined there.



Below is the code block:




var scraper = {
extractEmail: function (directoryName) {
var result = getDirectory(directoryName);
if (result !== 404) {
var protocol = result.https ? https : http;
protocol.request({
host: 'somevalue.net',
method: "GET"
}, function (res) {
var data = '';

res.on('data', function (chunk) {
data += chunk;
});

res.on('end', function () {
return data;
});
})
.on('error', function (err) {
return err;

})
.end();
//return data;
}
else {
//return "Failed";
}
}
};



And here is the Routes.js function:



app.get('/:directory', function (req, res) {
var n = scraper.extractEmail(req.params.directory);
console.log(n);
res.send(n);
});



In here also I don't get the value of n.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...