Reputation: 512
I am getting a issue which is given below when i hit the upload file api from ui. I am hitting a node js api from react js and then hit public api from node js.
node version:- 10.15.3
npm version:- 6.4.1
router.route("/uploadLocaleFile").post(function(req, res, next) {
req.on("data", data => {
uploadFile(req, data, res);
});
});
function uploadFile(req, data, res) {
request
.post({
uri: `${notebookPath}/localfile/${req.query.experimentId}`,
body: data,
headers: {
"Content-Type": req.headers["content-type"],
Authorization: req.headers["authorization"]
}
})
.pipe(res);
}
Upvotes: 0
Views: 4734
Reputation: 2772
You may want to try @Yaroslav Gaponov solution.
Your problem was that .pipe(res)
is triggering on each data
event. By the time your second data
event occurs, the res
stream is already closed.
What @Yaroslav Gaponov did, was that, on each data
event, he stores the incoming buffer into an array. When the stream is done the end
event will be emited, and only then you are allowed to do 1 write to res
, that is, the .pipe(res)
from uploadFile.
You can also try request
one line pipe like so:
req.pipe(request(your_options)).pipe(res)
Upvotes: 1
Reputation: 2099
Try this code
router.route("/uploadLocaleFile").post(function(req, res, next) {
const chunks = [];
req
.on('data', data => chunks.push(data))
.on("end", _ => uploadFile(req, Buffer.concat(chunks), res)
;
});
function uploadFile(req, data, res) {
request
.post({
uri: `${notebookPath}/localfile/${req.query.experimentId}`,
body: data,
headers: {
"Content-Type": req.headers["content-type"],
Authorization: req.headers["authorization"]
}
})
.pipe(res);
}
Upvotes: 2