STORE FILES

 HERE, IMAGES ARE  UPLOADED INTO THE CLOUD AND A LINK IS GIVEN IN THE END.

WE INSTALL TWO PACKAGES FROM NPM LIBRARY

npm i cloudinary

npm i multer-storage-cloudinary

A CLOUD CONFIG FILE IS CREATED 

CloudConfig.js

const cloudinary = require("cloudinary").v2;
const { CloudinaryStorage } = require("multer-storage-cloudinary");

cloudinary.config({
  cloud_name: process.env.CLOUD_NAME,
  api_key: process.env.CLOUD_API_KEY,
  api_secret: process.env.CLOUD_API_SECRET,
});

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: {
    folder: "wanderlust_DEV",
    allowedFormat: ["png", "jpeg", "jpg"],
  },
});

module.exports = {
  cloudinary,
  storage,
};


in APP.JS

const multer = require("multer");
const { storage } = require("../CloudConfig.js");
const upload = multer({ storage });


router
  .route("/")
  .get(wrapAsync(listingController.index))
  // .post(
  //   isLoggedIn,
  //   ValidateListing,
  //   wrapAsync(listingController.postNewListing)
  // );
  .post(upload.single("listing[image]"), (req, res) => {
    res.send(req.file);
  });



Comments