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
javascript node.js download
add a comment |
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
javascript node.js download
What isdecrypt
?
– omerowitz
Nov 13 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 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
add a comment |
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
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 at 15:53
asked Nov 13 at 11:46
Alex Turdean
163
163
What isdecrypt
?
– omerowitz
Nov 13 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 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
add a comment |
What isdecrypt
?
– omerowitz
Nov 13 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 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
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
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 at 15:37
add a comment |
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();
})
})
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
add a comment |
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
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
add a comment |
up vote
0
down vote
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 at 15:37
add a comment |
up vote
0
down vote
up vote
0
down vote
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 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
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 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
add a comment |
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();
})
})
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
add a comment |
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();
})
})
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
add a comment |
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();
})
})
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 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
add a comment |
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
add a comment |
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 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