Image upload on cloudinary not working in heroku

Viewed 957

I am using multer and cloudinary for image upload in node.js app. When I am in development mode, it works perfectly fine but after deploying to heroku, I am getting error in browser console as the server responded with a status of 503 (Service Unavailable). When I check heroku logs it showed as at=error code=H12 desc="Request timeout" method=POST path="/api/users/upload/avatar" I have already added my environment variables in heroku. What could be a problem?

My config file

const {config,uploader } = require('cloudinary').v2

const cloudinaryConfig = (req,res,next) => {
  config({
  cloud_name: process.env.CLOUD_NAME,
  api_key: process.env.API_KEY,
  api_secret: process.env.API_SECRET,
  });
  next()
}
  module.exports ={cloudinaryConfig,uploader}

Multer file

const DatauriParser = require('datauri/parser');

const parser = new DatauriParser();

const storage = multer.memoryStorage();
const multerUploads = multer({ storage,fileFilter:(req,file,cb)=>{
  let ext = path.extname(file.originalname);
  if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") {
  cb(new Error("File type is not supported"), false);
  return;
  } cb(null,true)
} }).single('image');

const dataUri = req => parser.format(path.extname(req.file.originalname).toString(),req.file.buffer)


module.exports = {multerUploads,dataUri}

My upload controller

const uploadImage = async(req,res)=>{
    const folder = req.path.split('/',3)[2]
    if(!req.file){
        throw new Error('Choose a picture to upload')
    }
     if(req.file){
         const file = dataUri(req).content
         const result = await uploader.upload(file,{
             folder:`MobiHub/${folder}`,
             width: 300,
             height:300,
             crop:'fill',
             gravity: "faces"
         })
         const image = result.secure_url
         res.json({image}) 
     }  
   
}
1 Answers

Setting the Cloudinary environment variables (e.g., process.env.CLOUD_NAME) on the Heroku config vars may help. Or simply try to add the CLOUDINARY_URL environment variable (can be found on the Cloudinary dashboard), then you won't need to explicitly declare them on cloudinary.config().

Related