I'm trying to get the source of an HTML file with an HTTP request in node.js - my problem is that it returns data twice. Here is my code:
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
if(chunk.length > 1000) {
console.log(chunk.length);
}
});
req.on('error', function(e) {
console.log("error" + e.message);
});
});
req.end();
This then returns:
5637
3703
The hell! When I just console.log(chunk), it returns all the data as if it were one large string, and when I add a something like console.log("data starts here") in the res.on('data', it returns the whole string with the "data starts here" somewhere in the middle, implying it's just being split.
Every test I do returns 2 values and it's really annoying. I can just do "if(chunk.length > 4000)" but given the nature of the page I'm getting, this could change. How can I make it so that all the data returns in one large chunk?