'Why is my cloudinary image upload slower in nodejs and reactjs?

It is my first time of trying to use cloudinary and multer to upload and store images for my blog application. Initially, I was storing the images locally using multer and saved the image url in mongodb. Now, I store the images in cloudinary and save the url in database.

I noticed that the cloudinary option is a bit slower compared to saving locally. Is there a workaround this?

Here is my nodecode:

app.post("/api/v1/upload", upload.single("file"), async (req, res) =>{

try {
    const fileStr = req.file.path
 
    if(!fileStr){
          return res.status(500).json( 'No image found');
    }
    const uploadResponse = await cloudinary.uploader.upload(fileStr, {
        upload_preset: 'nodeblog',
    });
   fs.unlinkSync(fileStr)
    const result = {
        url: uploadResponse.secure_url,
        publicId: uploadResponse.public_id
    }
   return res.status(200).json(result)
   
    
} catch (err) {
    console.error(err);
    return res.status(500).json({ err: 'Something went wrong' });
   
}
 

}); 

After image has uploaded successfully, I simply deleted it locally.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source