I am using multer to store files in local storage and then picking them according and sending them back currently for some reason files are sending as buffer (i am using postman to view them) . And i also need to send json data alongside the file.
In below example i commented the resFile code which is not working as expected. And i am just sending json data with res.send.
My file structure is like this for reference
~root
-controllers folder (route callback logic here)
-storage (with files i need to fetch)
-route.js file
-app.js file (entry point),
-etc.
try{
const template = await Template.findOne({
where : {
user_id : req.user.dataValues.id,
template_id : req.params.id
}
}).then((data)=>{
let dataJSON = {
id : data.dataValues.id,
user_id : data.dataValues.user_id,
template_id : data.dataValues.template_id,
title : data.dataValues.title,
customizations : JSON.parse(data.dataValues.customizations),
attachments : JSON.parse(data.dataValues.attachments)
}
let name = path.parse(dataJSON.attachments[0].path).name;
let ext = path.parse(dataJSON.attachments[0].path).ext;
console.log(name);
if(data){
// res.sendFile(name+ext,{root :path.join('storage','')},(err)=>{
// if (err) throw err;
// });
res.send({
success : true,
data : dataJSON
})
}else{
if(err) throw err;
res.status(403).send({
success : false,
message : 'Template Doesnt exist'
});
}
});
}