File download in Node JS
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
add a comment |
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
What isdecrypt
?
– 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 usingres
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
add a comment |
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
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
javascript node.js download
edited Nov 13 '18 at 15:53
Alex Turdean
asked Nov 13 '18 at 11:46
Alex TurdeanAlex Turdean
163
163
What isdecrypt
?
– 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 usingres
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
add a comment |
What isdecrypt
?
– 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 usingres
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
add a comment |
2 Answers
2
active
oldest
votes
Just use res.download(<filepath>)
.
https://expressjs.com/en/api.html#res.download
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
add a comment |
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();
)
)
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Just use res.download(<filepath>)
.
https://expressjs.com/en/api.html#res.download
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
add a comment |
Just use res.download(<filepath>)
.
https://expressjs.com/en/api.html#res.download
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
add a comment |
Just use res.download(<filepath>)
.
https://expressjs.com/en/api.html#res.download
Just use res.download(<filepath>)
.
https://expressjs.com/en/api.html#res.download
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
add a comment |
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
add a comment |
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();
)
)
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
add a comment |
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();
)
)
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
add a comment |
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();
)
)
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();
)
)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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