I have implemented a post method for file uploadation and now I want implement get() to check that either I'm getting right response of image using postman and without using UI. Here is my code of POST method.
// route controller for Profile Uploadation
exports.uploadFiles = async (req, res) => {
try{
console.log('Call from upload files',req.file);
if(req.file == undefined ){
return res.status(500).send({message:'You must have to select a file'});
}
//console.log('req of id',req.params.id);
const profileUsername = req.session.Auth;
Profile.create({
type:req.body.mimetype,
name:req.file.originalname,
post_username:profileUsername,
data:fs.readFileSync(__dirname + "/../uploads/" + req.file.filename),
}).then(image=>{
fs.writeFileSync(__dirname + "/../uploads/" + image.name, image.data);
req.session.profile_img_id = image.id;
console.log('req of image id',image.id);
return res.status(201).send({message:'Image file has been uploaded successfully'});
});
} catch(error){
console.log(error);
return res.send(`Error when trying upload images: ${error}`);
}
}
And Controller for file
const multer=require('multer');
const imageFilter=(req,file,cb)=>{
if(file.mimetype.startsWith("image")){
cb(null,true);
} else {
cb('Please upload only images.', false)
}
};
var storage=multer.diskStorage({
destination:(req,file,cb)=>{
cb(null,__dirname + "/../uploads/" );
},
filename:(req,file,cb)=>{
cb(null, `${Date.now()}-usama-${file.originalname}`);
}
})
var uploadFile=multer({ storage : storage, fileFilter : imageFilter });
module.exports=uploadFile;
And the route file is
router.post('/upload_profile', posts.sessionForApi, uploads.single('file'), posts.uploadFiles);
The issue I'm facing is not know how to implelment get() in to fetch image data and to test that response on postman.