0

I want to upload pictures to my nodejs app which has been hosted on netlify, it work on the local environment but after hosting on netlify it seems it can't locate the uploads folder to put the file there...

Upload code

const multer = require("multer");
const path = require("path");

let storage = multer.diskStorage({
 destination: (req, file, cb) => {
   cb(null, "./uploads");
 },
 filename: (req, file, cb) => {
   let pathname = path.extname(file.originalname);
   cb(null, Date.now() + pathname);
 },
});

let upload = multer({
 storage: storage,
 fileFilter: (req, file, cb) => {
   if (file.mimetype === "image/png" || file.mimetype === "image/jpg") {
     console.log("ok");
     cb(null, true);
   } else {
     console.log("Jpg and png only");
     cb(null, false);
   }
 },
 limits: { fileSize: 1024 * 1024 * 2 },
});


module.exports = upload

route

productRouter
  .route("/")
  .post(upload.single("image"), addProduct);
TRI NI TY
  • 1
  • 1
  • Well of course, netlify doesn't have `upload` folder. So how can you save to something that doesn't exist?!? That folder exists only in your local environment – Shivam Sep 22 '22 at 01:15
  • @Shivam But I have **uploads** folder in my github repo... – TRI NI TY Sep 22 '22 at 01:33
  • @Shivam is there any other alternative? – TRI NI TY Sep 22 '22 at 01:33
  • Well you would have to store your files in third party storage services like - AWS S3 or Google Cloud Storage because If I remember correctly, Netlify doesn't offer cloud storage. – Shivam Sep 22 '22 at 12:51

0 Answers0