I’m using express-file upload and Cloudinary to upload a file, but not to work, here is my server file and upload file. postman is sending a 500 empty error message, postman reads all the files, but I can’t upload them on Cloudinary. I’ve tried everything I know
import express from 'express';
import cloudinary from 'cloudinary';
const router = express.Router();
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET
});
router.post('/upload', (req, res) => {
try {
console.log(req.files)
if (!req.files || Object.keys(req.files).length === 0) return res.status(422).json('no files uploaded');
const file = req.files.file;
console.log(file);
if (file.size > 1024 * 1024) {
rmvTmp(file.tempFilePath);
return res.status(400).json({ msg: 'size too large' });
}
if (file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png') {
rmvTmp(file.tempFilePath);
return res.status(400).json({ msg: 'this file format is not supported' });
}
cloudinary.v2.uploader.upload(
file.tempFilePath,
{ folder: "vireau" },
async (err, result) => {
if (err) throw err;
rmvTmp(file.tempFilePath);
res.json({ public_id: result.public_id, url: result.secure_url });
}
);
} catch (error) {
return res.status(500).json({ msg: error.message });
}
});
const rmvTmp = (path) => {
fs.unlink(path, (err) => {
if (err) throw err;
});
};
export default router;