I am new to node.js and backend development. I am facing a issue while returning buffer response. Can anyone help me with below scenario.
I am having backend API function in that, I am fetching multiple images from cloud storage, that returns multiple buffer files as response.
Now that buffer responses I am again passing to a third party API function...
that third party API function will process all buffer files and will generate a single image buffer.
this final single image buffer should be my response of API to front end.
but some how I am not able send that buffer as it is, its getting changed.
and on front end when I am trying to do response.blob(); its generating error
Uncaught (in promise) TypeError: Failed to execute 'blob' on 'Response': body stream already read Can anyone is having any idea how to send buffer response correctly.
//below is some code for backend
downloadSingle = aysnc(req, res){
try{
//I am fetching files from cloud storage
let filelist = [] //I will have a multiple file name in file list;
let OutputBuffers = [];
for (let i =0; i<filelist.length; i++){
const sFiles = await // code for fetching files from cloud storage.. its working fine
const sFileBuffer = await streamToBuffer(sFiles.Body)
OutputBuffers.push({'bufferName': 'fileList[i]' , 'buffer': sFileBuffer})
}
if(OutputBuffers.length>0){
const formData = new FormData();
OutputBuffers.forEach(f=>{
formData.append('files', Buffer.form(f.buffer), {fileName: f.bufferName}))
}
try{
//here I am hitting another API to get a single file from Multiple buffer files
// this API returns buffer response in body and header info
//I need to pass this response buffer and header as it is to front end as response
const result = await fetch(`ThirdParty_API_URL`, {method: "POST", body: formData});
return res.send(result); //I think here I am making mistake
}
catch(e){
return res.status(500).json({'error': e})
}
}
}
catch(e){
return res.status(500).json({'error': e})
}
}
//code for front end
const handleDownloadEvent = async(){
const response = await fetch(`API_URL_for_downloadSingle`, {method: "GET"});
if(response.ok){
const fileName = response.headers.get("Content-Disposition");
const blob = await response.blob(); // this is throwing error
//Uncaught (in promise) TypeError: Failed to execute 'blob' on 'Response': body stream already read
const url = URL.createObjectURL(blob)
}
}