File download in Node JS











up vote
3
down vote

favorite












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 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 at 12:00










  • @omerowitz i updated my question.
    – Alex Turdean
    Nov 13 at 16:01










  • @DLowther yes and i tried several methods to end my request properly but nothing works
    – Alex Turdean
    Nov 13 at 16:02















up vote
3
down vote

favorite












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 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 at 12:00










  • @omerowitz i updated my question.
    – Alex Turdean
    Nov 13 at 16:01










  • @DLowther yes and i tried several methods to end my request properly but nothing works
    – Alex Turdean
    Nov 13 at 16:02













up vote
3
down vote

favorite









up vote
3
down vote

favorite











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 at 15:53

























asked Nov 13 at 11:46









Alex Turdean

163




163












  • What is decrypt?
    – omerowitz
    Nov 13 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 at 12:00










  • @omerowitz i updated my question.
    – Alex Turdean
    Nov 13 at 16:01










  • @DLowther yes and i tried several methods to end my request properly but nothing works
    – Alex Turdean
    Nov 13 at 16:02


















  • What is decrypt?
    – omerowitz
    Nov 13 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 at 12:00










  • @omerowitz i updated my question.
    – Alex Turdean
    Nov 13 at 16:01










  • @DLowther yes and i tried several methods to end my request properly but nothing works
    – Alex Turdean
    Nov 13 at 16:02
















What is decrypt?
– omerowitz
Nov 13 at 11:50




What is decrypt?
– omerowitz
Nov 13 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 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 at 12:00












@omerowitz i updated my question.
– Alex Turdean
Nov 13 at 16:01




@omerowitz i updated my question.
– Alex Turdean
Nov 13 at 16:01












@DLowther yes and i tried several methods to end my request properly but nothing works
– Alex Turdean
Nov 13 at 16:02




@DLowther yes and i tried several methods to end my request properly but nothing works
– Alex Turdean
Nov 13 at 16:02












2 Answers
2






active

oldest

votes

















up vote
0
down vote













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 at 15:37


















up vote
0
down vote













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 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 at 15:56










  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.
    – Alex Turdean
    Nov 13 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous
    – Alex Turdean
    Nov 13 at 15:59










  • @AlexTurdean , I just updated the answer
    – Bhuvanachandu
    Nov 14 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',
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








up vote
0
down vote













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 at 15:37















up vote
0
down vote













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 at 15:37













up vote
0
down vote










up vote
0
down vote









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 at 11:55









Souvik Dey

15225




15225












  • Ok. But my file is encrypted and i have to decrypt it first. How can i decrypt it before download?
    – Alex Turdean
    Nov 13 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 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 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 at 15:37












up vote
0
down vote













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 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 at 15:56










  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.
    – Alex Turdean
    Nov 13 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous
    – Alex Turdean
    Nov 13 at 15:59










  • @AlexTurdean , I just updated the answer
    – Bhuvanachandu
    Nov 14 at 3:52















up vote
0
down vote













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 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 at 15:56










  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.
    – Alex Turdean
    Nov 13 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous
    – Alex Turdean
    Nov 13 at 15:59










  • @AlexTurdean , I just updated the answer
    – Bhuvanachandu
    Nov 14 at 3:52













up vote
0
down vote










up vote
0
down vote









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 at 3:55

























answered Nov 13 at 12:01









Bhuvanachandu

456




456












  • But if i do that, from my knowledge, i could close it too early. Right?
    – Alex Turdean
    Nov 13 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 at 15:56










  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.
    – Alex Turdean
    Nov 13 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous
    – Alex Turdean
    Nov 13 at 15:59










  • @AlexTurdean , I just updated the answer
    – Bhuvanachandu
    Nov 14 at 3:52


















  • But if i do that, from my knowledge, i could close it too early. Right?
    – Alex Turdean
    Nov 13 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 at 15:56










  • @Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.
    – Alex Turdean
    Nov 13 at 15:57












  • @Katie.Sun yes, but NodeJs works asynchronous
    – Alex Turdean
    Nov 13 at 15:59










  • @AlexTurdean , I just updated the answer
    – Bhuvanachandu
    Nov 14 at 3:52
















But if i do that, from my knowledge, i could close it too early. Right?
– Alex Turdean
Nov 13 at 15:38




But if i do that, from my knowledge, i could close it too early. Right?
– Alex Turdean
Nov 13 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 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 at 15:56












@Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.
– Alex Turdean
Nov 13 at 15:57






@Bhuvi Anyway, i tried that for you and i get errors with memory leak detected.
– Alex Turdean
Nov 13 at 15:57














@Katie.Sun yes, but NodeJs works asynchronous
– Alex Turdean
Nov 13 at 15:59




@Katie.Sun yes, but NodeJs works asynchronous
– Alex Turdean
Nov 13 at 15:59












@AlexTurdean , I just updated the answer
– Bhuvanachandu
Nov 14 at 3:52




@AlexTurdean , I just updated the answer
– Bhuvanachandu
Nov 14 at 3:52


















 

draft saved


draft discarded



















































 


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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents