File download in Node JS










3















I am trying to implement a download router for a nodejs app. After several downloads my app crashes. Any advice?



app.route( "/download" )
.get( (req, res) =>
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);

)


I get this error:



 Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:243:12)
at Decipher.Writable.write (_stream_writable.js:291:5)
at ReadStream.ondata (_stream_readable.js:666:20)
at ReadStream.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at ReadStream.Readable.push (_stream_readable.js:219:10)
at fs.read (internal/fs/streams.js:196:12)
at FSReqCallback.wrapper [as oncomplete] (fs.js:473:5)
Emitted 'error' event at:
at Decipher.onerror (_stream_readable.js:690:12)
at Decipher.emit (events.js:182:13)
at writeAfterEnd (_stream_writable.js:245:10)
at Decipher.Writable.write (_stream_writable.js:291:5)
[... lines matching original stack trace ...]
at FSReqCallback.wrapper [as oncomplete] (fs.js:473:5)


UPDATE
Someone asked about decrypt, the files are encrypted using crypto module:



var decrypt = crypto.createDecipher(algorithm, password);


Also, after reading res.end() is never sent after streaming a file to client i tried this:



var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res, end: false);
filestream.on("close", function()
res.status(200);
res.end();
);
filestream.on("error", function()
res.status(400);
res.end();
);


It doesn't work :(, i get the same error










share|improve this question
























  • What is decrypt?

    – omerowitz
    Nov 13 '18 at 11:50











  • my off the top of my head guess is that since you are never closing out a request using res your requests are hanging out in system memory.

    – D Lowther
    Nov 13 '18 at 12:00











  • @omerowitz i updated my question.

    – Alex Turdean
    Nov 13 '18 at 16:01











  • @DLowther yes and i tried several methods to end my request properly but nothing works

    – Alex Turdean
    Nov 13 '18 at 16:02















3















I am trying to implement a download router for a nodejs app. After several downloads my app crashes. Any advice?



app.route( "/download" )
.get( (req, res) =>
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);

)


I get this error:



 Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:243:12)
at Decipher.Writable.write (_stream_writable.js:291:5)
at ReadStream.ondata (_stream_readable.js:666:20)
at ReadStream.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at ReadStream.Readable.push (_stream_readable.js:219:10)
at fs.read (internal/fs/streams.js:196:12)
at FSReqCallback.wrapper [as oncomplete] (fs.js:473:5)
Emitted 'error' event at:
at Decipher.onerror (_stream_readable.js:690:12)
at Decipher.emit (events.js:182:13)
at writeAfterEnd (_stream_writable.js:245:10)
at Decipher.Writable.write (_stream_writable.js:291:5)
[... lines matching original stack trace ...]
at FSReqCallback.wrapper [as oncomplete] (fs.js:473:5)


UPDATE
Someone asked about decrypt, the files are encrypted using crypto module:



var decrypt = crypto.createDecipher(algorithm, password);


Also, after reading res.end() is never sent after streaming a file to client i tried this:



var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res, end: false);
filestream.on("close", function()
res.status(200);
res.end();
);
filestream.on("error", function()
res.status(400);
res.end();
);


It doesn't work :(, i get the same error










share|improve this question
























  • What is decrypt?

    – omerowitz
    Nov 13 '18 at 11:50











  • my off the top of my head guess is that since you are never closing out a request using res your requests are hanging out in system memory.

    – D Lowther
    Nov 13 '18 at 12:00











  • @omerowitz i updated my question.

    – Alex Turdean
    Nov 13 '18 at 16:01











  • @DLowther yes and i tried several methods to end my request properly but nothing works

    – Alex Turdean
    Nov 13 '18 at 16:02













3












3








3








I am trying to implement a download router for a nodejs app. After several downloads my app crashes. Any advice?



app.route( "/download" )
.get( (req, res) =>
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);

)


I get this error:



 Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:243:12)
at Decipher.Writable.write (_stream_writable.js:291:5)
at ReadStream.ondata (_stream_readable.js:666:20)
at ReadStream.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at ReadStream.Readable.push (_stream_readable.js:219:10)
at fs.read (internal/fs/streams.js:196:12)
at FSReqCallback.wrapper [as oncomplete] (fs.js:473:5)
Emitted 'error' event at:
at Decipher.onerror (_stream_readable.js:690:12)
at Decipher.emit (events.js:182:13)
at writeAfterEnd (_stream_writable.js:245:10)
at Decipher.Writable.write (_stream_writable.js:291:5)
[... lines matching original stack trace ...]
at FSReqCallback.wrapper [as oncomplete] (fs.js:473:5)


UPDATE
Someone asked about decrypt, the files are encrypted using crypto module:



var decrypt = crypto.createDecipher(algorithm, password);


Also, after reading res.end() is never sent after streaming a file to client i tried this:



var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res, end: false);
filestream.on("close", function()
res.status(200);
res.end();
);
filestream.on("error", function()
res.status(400);
res.end();
);


It doesn't work :(, i get the same error










share|improve this question
















I am trying to implement a download router for a nodejs app. After several downloads my app crashes. Any advice?



app.route( "/download" )
.get( (req, res) =>
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);

)


I get this error:



 Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:243:12)
at Decipher.Writable.write (_stream_writable.js:291:5)
at ReadStream.ondata (_stream_readable.js:666:20)
at ReadStream.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at ReadStream.Readable.push (_stream_readable.js:219:10)
at fs.read (internal/fs/streams.js:196:12)
at FSReqCallback.wrapper [as oncomplete] (fs.js:473:5)
Emitted 'error' event at:
at Decipher.onerror (_stream_readable.js:690:12)
at Decipher.emit (events.js:182:13)
at writeAfterEnd (_stream_writable.js:245:10)
at Decipher.Writable.write (_stream_writable.js:291:5)
[... lines matching original stack trace ...]
at FSReqCallback.wrapper [as oncomplete] (fs.js:473:5)


UPDATE
Someone asked about decrypt, the files are encrypted using crypto module:



var decrypt = crypto.createDecipher(algorithm, password);


Also, after reading res.end() is never sent after streaming a file to client i tried this:



var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res, end: false);
filestream.on("close", function()
res.status(200);
res.end();
);
filestream.on("error", function()
res.status(400);
res.end();
);


It doesn't work :(, i get the same error







javascript node.js download






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 15:53







Alex Turdean

















asked Nov 13 '18 at 11:46









Alex TurdeanAlex Turdean

163




163












  • What is decrypt?

    – omerowitz
    Nov 13 '18 at 11:50











  • my off the top of my head guess is that since you are never closing out a request using res your requests are hanging out in system memory.

    – D Lowther
    Nov 13 '18 at 12:00











  • @omerowitz i updated my question.

    – Alex Turdean
    Nov 13 '18 at 16:01











  • @DLowther yes and i tried several methods to end my request properly but nothing works

    – Alex Turdean
    Nov 13 '18 at 16:02

















  • What is decrypt?

    – omerowitz
    Nov 13 '18 at 11:50











  • my off the top of my head guess is that since you are never closing out a request using res your requests are hanging out in system memory.

    – D Lowther
    Nov 13 '18 at 12:00











  • @omerowitz i updated my question.

    – Alex Turdean
    Nov 13 '18 at 16:01











  • @DLowther yes and i tried several methods to end my request properly but nothing works

    – Alex Turdean
    Nov 13 '18 at 16:02
















What is decrypt?

– omerowitz
Nov 13 '18 at 11:50





What is decrypt?

– omerowitz
Nov 13 '18 at 11:50













my off the top of my head guess is that since you are never closing out a request using res your requests are hanging out in system memory.

– D Lowther
Nov 13 '18 at 12:00





my off the top of my head guess is that since you are never closing out a request using res your requests are hanging out in system memory.

– D Lowther
Nov 13 '18 at 12:00













@omerowitz i updated my question.

– Alex Turdean
Nov 13 '18 at 16:01





@omerowitz i updated my question.

– Alex Turdean
Nov 13 '18 at 16:01













@DLowther yes and i tried several methods to end my request properly but nothing works

– Alex Turdean
Nov 13 '18 at 16:02





@DLowther yes and i tried several methods to end my request properly but nothing works

– Alex Turdean
Nov 13 '18 at 16:02












2 Answers
2






active

oldest

votes


















0














Just use res.download(<filepath>).



https://expressjs.com/en/api.html#res.download






share|improve this answer























  • Ok. But my file is encrypted and i have to decrypt it first. How can i decrypt it before download?

    – Alex Turdean
    Nov 13 '18 at 15:37


















0














This error usually occurs when we are out of memory.
So we have to close every stream after using it.



Streams are EventEmitters so you can listen to certain events.



app.route( "/download" ).get( (req, res) => 
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);
filestream.on("finish", function()
res.send();
)

)





share|improve this answer

























  • But if i do that, from my knowledge, i could close it too early. Right?

    – Alex Turdean
    Nov 13 '18 at 15:38











  • @AlexTurdean Not sure what you mean by 'too early'? You decide when it happens by where you place the statement in your code

    – Katie.Sun
    Nov 13 '18 at 15:56











  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.

    – Alex Turdean
    Nov 13 '18 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous

    – Alex Turdean
    Nov 13 '18 at 15:59











  • @AlexTurdean , I just updated the answer

    – Bhuvanachandu
    Nov 14 '18 at 3:52










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280368%2ffile-download-in-node-js%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Just use res.download(<filepath>).



https://expressjs.com/en/api.html#res.download






share|improve this answer























  • Ok. But my file is encrypted and i have to decrypt it first. How can i decrypt it before download?

    – Alex Turdean
    Nov 13 '18 at 15:37















0














Just use res.download(<filepath>).



https://expressjs.com/en/api.html#res.download






share|improve this answer























  • Ok. But my file is encrypted and i have to decrypt it first. How can i decrypt it before download?

    – Alex Turdean
    Nov 13 '18 at 15:37













0












0








0







Just use res.download(<filepath>).



https://expressjs.com/en/api.html#res.download






share|improve this answer













Just use res.download(<filepath>).



https://expressjs.com/en/api.html#res.download







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 11:55









Souvik DeySouvik Dey

15245




15245












  • Ok. But my file is encrypted and i have to decrypt it first. How can i decrypt it before download?

    – Alex Turdean
    Nov 13 '18 at 15:37

















  • Ok. But my file is encrypted and i have to decrypt it first. How can i decrypt it before download?

    – Alex Turdean
    Nov 13 '18 at 15:37
















Ok. But my file is encrypted and i have to decrypt it first. How can i decrypt it before download?

– Alex Turdean
Nov 13 '18 at 15:37





Ok. But my file is encrypted and i have to decrypt it first. How can i decrypt it before download?

– Alex Turdean
Nov 13 '18 at 15:37













0














This error usually occurs when we are out of memory.
So we have to close every stream after using it.



Streams are EventEmitters so you can listen to certain events.



app.route( "/download" ).get( (req, res) => 
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);
filestream.on("finish", function()
res.send();
)

)





share|improve this answer

























  • But if i do that, from my knowledge, i could close it too early. Right?

    – Alex Turdean
    Nov 13 '18 at 15:38











  • @AlexTurdean Not sure what you mean by 'too early'? You decide when it happens by where you place the statement in your code

    – Katie.Sun
    Nov 13 '18 at 15:56











  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.

    – Alex Turdean
    Nov 13 '18 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous

    – Alex Turdean
    Nov 13 '18 at 15:59











  • @AlexTurdean , I just updated the answer

    – Bhuvanachandu
    Nov 14 '18 at 3:52















0














This error usually occurs when we are out of memory.
So we have to close every stream after using it.



Streams are EventEmitters so you can listen to certain events.



app.route( "/download" ).get( (req, res) => 
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);
filestream.on("finish", function()
res.send();
)

)





share|improve this answer

























  • But if i do that, from my knowledge, i could close it too early. Right?

    – Alex Turdean
    Nov 13 '18 at 15:38











  • @AlexTurdean Not sure what you mean by 'too early'? You decide when it happens by where you place the statement in your code

    – Katie.Sun
    Nov 13 '18 at 15:56











  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.

    – Alex Turdean
    Nov 13 '18 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous

    – Alex Turdean
    Nov 13 '18 at 15:59











  • @AlexTurdean , I just updated the answer

    – Bhuvanachandu
    Nov 14 '18 at 3:52













0












0








0







This error usually occurs when we are out of memory.
So we have to close every stream after using it.



Streams are EventEmitters so you can listen to certain events.



app.route( "/download" ).get( (req, res) => 
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);
filestream.on("finish", function()
res.send();
)

)





share|improve this answer















This error usually occurs when we are out of memory.
So we have to close every stream after using it.



Streams are EventEmitters so you can listen to certain events.



app.route( "/download" ).get( (req, res) => 
var filename = req.query.filename;
var file = __dirname + '/upload/' + filename;
var mimetype = mime.lookup(file);

res.setHeader('Content-disposition', 'attachment; filename=' + req.query.filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(file);
filestream.pipe(decrypt).pipe(res);
filestream.on("finish", function()
res.send();
)

)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 3:55

























answered Nov 13 '18 at 12:01









BhuvanachanduBhuvanachandu

957




957












  • But if i do that, from my knowledge, i could close it too early. Right?

    – Alex Turdean
    Nov 13 '18 at 15:38











  • @AlexTurdean Not sure what you mean by 'too early'? You decide when it happens by where you place the statement in your code

    – Katie.Sun
    Nov 13 '18 at 15:56











  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.

    – Alex Turdean
    Nov 13 '18 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous

    – Alex Turdean
    Nov 13 '18 at 15:59











  • @AlexTurdean , I just updated the answer

    – Bhuvanachandu
    Nov 14 '18 at 3:52

















  • But if i do that, from my knowledge, i could close it too early. Right?

    – Alex Turdean
    Nov 13 '18 at 15:38











  • @AlexTurdean Not sure what you mean by 'too early'? You decide when it happens by where you place the statement in your code

    – Katie.Sun
    Nov 13 '18 at 15:56











  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.

    – Alex Turdean
    Nov 13 '18 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous

    – Alex Turdean
    Nov 13 '18 at 15:59











  • @AlexTurdean , I just updated the answer

    – Bhuvanachandu
    Nov 14 '18 at 3:52
















But if i do that, from my knowledge, i could close it too early. Right?

– Alex Turdean
Nov 13 '18 at 15:38





But if i do that, from my knowledge, i could close it too early. Right?

– Alex Turdean
Nov 13 '18 at 15:38













@AlexTurdean Not sure what you mean by 'too early'? You decide when it happens by where you place the statement in your code

– Katie.Sun
Nov 13 '18 at 15:56





@AlexTurdean Not sure what you mean by 'too early'? You decide when it happens by where you place the statement in your code

– Katie.Sun
Nov 13 '18 at 15:56













@Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.

– Alex Turdean
Nov 13 '18 at 15:57






@Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.

– Alex Turdean
Nov 13 '18 at 15:57














@Katie.Sun yes, but NodeJs works asynchronous

– Alex Turdean
Nov 13 '18 at 15:59





@Katie.Sun yes, but NodeJs works asynchronous

– Alex Turdean
Nov 13 '18 at 15:59













@AlexTurdean , I just updated the answer

– Bhuvanachandu
Nov 14 '18 at 3:52





@AlexTurdean , I just updated the answer

– Bhuvanachandu
Nov 14 '18 at 3:52

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280368%2ffile-download-in-node-js%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo