I am trying to implement a simple Express handler which appends the request body to a file, and returns the offset in a file where the write has been performed. Based on some research, it seems that there is no ftell
-like function in NodeJS, and the similar questions (like fs.createReadStream() at specific position of file) refer to using start
parameter or manually seeking - which I do not see how to use in combination with appending to a file (when the offset at the end of a file is unknown). I am currently stuck with a code like
app.put('/:blob', (req, res) => {
var blob = req.params.blob;
var blobpath = path.join(__dirname, 'data/' + blob);
var stream = fs.createWriteStream(blobpath, {flags:'a'});
var pos = fs.tell(stream); // <-- I do not know how to do this
function handle(data) {
stream.write(data);
req.once('data', handle);
}
req.once('data', handle);
req.on('end', function() {
stream.end();
res.json({offset: pos});
});
});
Could you please help me how I should achieve my goal?