It’s wierd because, when i do this in my local machine it works, and im using render for testing purposes so i dont have access to directory.
There is no whatsoever problem with any directory but in render it seems that it doesn’t work.
Like on my device, The Directory is (dir path) == C:\Users\XYZ\Desktop\Projects\Ask-International\uploads\client_1702111725831_banner.png The Directoy is (img path) == C:\Users\XYZ\Desktop\Projects\Ask-International\uploads\client_1702111728120_image.png
And like, there is no error or what so ever. But i tested it in render, (the images are UGC, and its deleted after its uploaded to cloudinary) after a while.
I am using free version of render so i cant access anything, so i logged this
The Directoy is (dir path) == /opt/render/project/src/uploads/client_1702111648689_banner.png
But render throws this error
Error uploading project: [Error: ENOENT: no such file or directory, open ‘/opt/render/project/src/uploads/client_1702111648689_banner.png’] .
I even used __dirname and even process.cwd();
It works on my machine, and i thought it would be pretty similar but i don’t know.
Im an beginner as well, here is the small piece of code:
const jsonFilePath = path.join(process.cwd(), ‘JSON-datas’, ‘projects.json’);
router.post(‘/upload-project’, async (req, res) => {
try {
const { comments, projectType, projectName } = req.body;
const bannerImage = req.files.banner;
const images = Array.isArray(req.files.images) ? req.files.images : [req.files.images];
// Process the banner image
const bannerImagePath = path.join(process.cwd(), “uploads”, client_${Date.now()}_banner.png
);
console.log("The Directoy is (dir path) == " +bannerImagePath);
await bannerImage.mv(bannerImagePath);
const bannerUrl = await uploadImageToCloudinary(bannerImagePath);
fs.unlinkSync(bannerImagePath);
// Process each image file
const imageUrls = ;
for (const image of images) {
const imagePath = path.join(process.cwd(), “uploads”, client_${Date.now()}_image.png
);
console.log("The Directoy is (img path) == " + imagePath);
await image.mv(imagePath);
const imageUrl = await uploadImageToCloudinary(imagePath);
fs.unlinkSync(imagePath);
imageUrls.push(imageUrl.secure_url);
}
const projects = readProjects();
projects.services[projectType].push({
banner: bannerUrl.secure_url,
images: imageUrls,
comments: comments,
projectName: projectName,
});
writeProjects(projects);
res.json({ message: ‘Project uploaded successfully.’ });
} catch (error) {
console.error(‘Error uploading project:’, error);
res.status(500).json({ error: ‘Internal Server Error’ });
}
});