I am reading a growing file in nodejs. Using below code in my app.js.
app.post('/readfile',function (req,res) {
var fullfilename = req.body.filepath+"\\"+req.body.filename;
var bite_size = 256;
var readbytes = 0;
var file;
fs.open(fullfilename, 'r', function(err, fd) {
file = fd;
if(err){console.log(err); throw err; return;};
var mybuff;
var func = (function readsome() {
var stats = fs.fstatSync(fd); // yes sometimes async does not make sense!
if(stats.size<readbytes+1) {
setTimeout(readsome, 1000);
}
else {
fs.read(fd, new Buffer(bite_size), 0, bite_size, readbytes, function (err, bytecount, buff) {
//console.log(buff.toString('utf-8', 0, bytecount));
res.json(buff.toString('utf-8', 0, bytecount));
readbytes+=bytecount;
process.nextTick(readsome);
});
};
})();
});
});
And calling this in html like below ,
var myApp= angular.module("myApp", [])
myApp.controller('Ctrl1', function ($scope, $http,$q) {
var FileName = "test.txt"
var obj = {"filepath" : 'D:\\Temp', "filename" : FileName};
$http({
url: '/readTfile',
method: "POST",
data: JSON.stringify(obj)
//timeout: canceller.promise,
//headers: {'Content-Type': 'application/json','charset' : 'utf-8'}
}).success(function(result) {
$scope.myfiledata = result;
}).error(function(data, status) {
console.log(data);
});
});
This is working fine when i put this app.js code in a separate file (readfile.js) but when i put in app.js it gives error.
_http_outgoing.js:357
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
at ServerResponse.header (D:\myapp\node_modules\express\lib\response.js:725:10)
at ServerResponse.send (D:\myapp\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (D:\myapp\node_modules\express\lib\response.js:256:15)
at D:\myapp\app.js:130:10
at FSReqWrap.wrapper [as oncomplete] (fs.js:682:17)
[nodemon] app crashed - waiting for file changes before starting...
Please help to find where is the mistake and what to be changed. Secondly i want to stop this call on button click, how can i do that ?