node.js - Node appendFile not appending data chunk to file on filesystem -
i have program trying values request using curl , store them in file , serve stored content back. decision store or append contents in file based on query parameter appendflag when run program getting in console "true" , "appending" suggests indeed reads flag goes if part somehow appendfile function not working.
var http = require('http'); var url = require('url'); var querystring = require('querystring'); var fs = require('fs'); http.createserver(function(request,response){ var str = request.url.split('?')[1]; var query = querystring.parse(str); var writestream = fs.createwritestream(query['filename']); console.log("query - "); console.log(query["appendflag"]); request.on('data',function(chunk){ if(query["appendflag"]=="true"){ console.log("appending"); fs.appendfile(query['filename'],chunk.tostring(),function(err){ if(err) throw err; }); }else{ var buffergood = writestream.write(chunk); if(!buffergood) request.pause(); } }); request.on('end',function(){ response.writehead(200); response.write("\n content url - \n"); var readstream = fs.createreadstream(query['filename'],{buffersize:64*1024}); readstream.on('data',function(chunk){ response.write(chunk.tostring()); }); readstream.on('end',function(){ response.write("\n"); response.end(); }); }); writestream.on('drain',function(){ request.resume(); }); }).listen(8080);
then after reading answer so( how create appending writestream in node.js ) tried -
// program extract url request , writing in particular file var http = require('http'); var url = require('url'); var querystring = require('querystring'); var fs = require('fs'); http.createserver(function(request,response){ var str = request.url.split('?')[1]; var query = querystring.parse(str); var writestream = fs.createwritestream(query['filename']); options = { 'flags': 'a' , 'encoding' : null, 'mode' : 0666 } var appendstream = fs.createwritestream(query['filename'],[options]); console.log("query - "); console.log(query["appendflag"]); request.on('data',function(chunk){ if(query["appendflag"]=="true"){ console.log("appending"); var buffergood = appendstream.write(chunk.tostring()); if(!buffergood) request.pause(); }else{ var buffergood = writestream.write(chunk.tostring()); if(!buffergood) request.pause(); } }); request.on('end',function(){ response.writehead(200); response.write("\n content url - \n"); var readstream = fs.createreadstream(query['filename'],{buffersize:64*1024}); readstream.on('data',function(chunk){ response.write(chunk.tostring()); }); readstream.on('end',function(){ response.write("\n"); response.end(); }); }); writestream.on('drain',function(){ request.resume(); }); }).listen(8080);
that changed flag 'a' , did not append data?
your can use first variant. before appendfile()
you've opened writestream
same query["filename"]
. stream opened.
var writestream = fs.createwritestream(query['filename']); options = { 'flags': 'a' , 'encoding' : null, 'mode' : 0666 } var appendstream = fs.createwritestream(query['filename'],[options]);
may it's better like:
var options = { flags: query.appendfile ? 'w' : 'a' ...
next: why [options]
? should remove brackets.
next: there no guarantee you'll have filename
param in querystring
. please handle situation.
Comments
Post a Comment