Preview:
import express from "express";
const router = express.Router();
import multer from "multer";
import productModel from "../model/productModel.js";

const Storage = multer.diskStorage({
  destination: "upload",
  filename: (req, file, cb) => {
    cb(null, file.originalname);
  },
});

const upload = multer({
  storage: Storage,
}).single("image");

router.post("/product", (req, res) => {
  upload(req, res, (err) => {
    if (err) {
      console.log(err);
    } else {
      const product = new productModel({
        name: req.body.name,
        image: {
          data: req.file.filename,
          contentType: "image/png",
        },
      });
      product
        .save()
        .then(() => res.send("successfully created"))
        .catch((err) => res.send("error in creating product"));
    }
  });
});

export default router;
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter