i have a growing txt file and below function to read the file as it grows. This function i have put in a route js and invoking by $http call. Things are working but i don't know how to stop reading and terminate/cancel the function execution call. please help
var fs = require('fs'),
bite_size = 256,
readbytes = 0,
file;
fs.open('test.txt', 'r', function(err, fd) {
file = fd; //readsome();
var func = (function readsome() {
var stats = fs.fstatSync(file); // yes sometimes async does not make sense!
if(stats.size<readbytes+1) {
console.log('Hehe I am much faster than your writer..! I will sleep for a while, I deserve it!');
setTimeout(readsome, 1000);
}
else {
fs.read(file, new Buffer(bite_size), 0, bite_size, readbytes, function (err, bytecount, buff) {
console.log(buff.toString('utf-8', 0, bytecount));
readbytes+=bytecount;
process.nextTick(readsome);
});
}
})();
});