I have this simple HTMLParser in Node.js using the http module:
var http = require('http');
var options = {
hostname: 'www.google.com',
port: 80,
path: '/',
method: 'GET'
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
var title1 = chunk.indexOf("<title>");
var title2 = chunk.indexOf("</title>");
var titl = chunk.substring(title1 + 7);
var result = titl.substring(0, titl.indexOf("</title>"));
console.log("Title is : " + result);
});
req.end();
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
and when executed, iterates more than once, so I get this output in the command line and it varies but always iterates more than once.
Title is: Google
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Title is:
Any help? Thanks in advance!