In Node.js how can I tell when I have finished parsing a web page?
Problem
The documentation has the following code
var options = {
host: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
Whilst the chunks print successfully, I'm not sure how to tell when I have reached the end of the page.
Problem courtesy of: Hoa
Solution
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
res.on('end', function () {
console.log('The end');
});
});
http://nodejs.org/api/http.html#http_event_end
Event: 'end'. Emitted exactly once for each request. After that, no more 'data' events will be emitted on the request.
Solution courtesy of: Vadim Baryshev
Discussion
View additional discussion.