title: "message": "ENOENT: no such file or directory, open 'E:\\astrology\\utils\\uploads\\1600798534862qf.png'"
body: <p>As the title suggests, I am getting <code>error: {&quot;message&quot;: &quot;ENOENT: no such file or directory, open 'E:\\astrology\\utils\\uploads\\1600798534862qf.png'&quot;}</code> in my project even after passing every required configs.</p>
  <p>Note: I've divided 'app' into two parts: main 'app.js' and 'appRoute.js' for more dynamic and code clarity.</p>
  <p><strong>app.js</strong></p>
  <pre><code>const express = require(&quot;express&quot;);
  const path = require(&quot;path&quot;);
  
  const app = express();
  const directory = path.join(__dirname, &quot;utils/uploads&quot;);
  app.use(&quot;/uploads&quot;, express.static(directory));
  
  require(&quot;./config/database/db&quot;)();
  require(&quot;./config/approutes/appRoutes&quot;)(app);
  
  module.exports = app;
  </code></pre>
  <p><strong>appRoute.js</strong></p>
  <pre><code>require(&quot;dotenv&quot;).config();
  
  const morgan = require(&quot;morgan&quot;);
  const bodyParser = require(&quot;body-parser&quot;);
  const cors = require(&quot;cors&quot;);
  
  const productRoutes = require(&quot;../../api/routes/products&quot;);
  
  module.exports = function (app) {
    app.use(morgan(&quot;dev&quot;));
  
    app.use(bodyParser.urlencoded({ extended: true, limit: &quot;100mb&quot; }));
    app.use(bodyParser.json());
  
    app.use(cors({ credentials: true, origin: true }));
  
    app.use(&quot;/products&quot;, productRoutes);
    
    app.use((req, res, next) =&gt; {
      const error = new Error(&quot;Not found&quot;);
      error.status = 404;
      next(error);
    });
  
    app.use((error, req, res, next) =&gt; {
      console.log('SHOW ERROR', error);
      
      res.status(error.status || 500);
      res.json({
        error: {
          message: error.message,
        },
      });
    });
  };
  </code></pre>
  <p><strong>fileUpload.js</strong></p>
  <pre><code>const multer = require(&quot;multer&quot;);
  
  const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, __dirname + &quot;/uploads&quot;);
    },
    filename: function (req, file, cb) {
      cb(null, Date.now() + file.originalname.replace(/\s+/g, &quot;-&quot;));
    },
  });
  
  const fileFilter = (req, file, cb) =&gt; {
    // reject a file
    if (file.mimetype === &quot;image/jpeg&quot; || file.mimetype === &quot;image/png&quot;) {
      cb(null, true);
    } else {
      cb(null, false);
    }
  };
  
  const upload = multer({
    storage,
    limits: {
      fileSize: 1024 * 1024 * 5,
    },
    fileFilter: fileFilter,
  });
  
  module.exports = upload;
  </code></pre>
  <p><strong>Product.js (controller)</strong></p>
  <pre><code>exports.create_product = async (req, res, next) =&gt; {
    const { title, min_content, content } = req.body;
  
    console.log(&quot;req files&quot;, req.files);
  
    try {
      const product = new Product({
        title,
        min_content,
        content,
      });
  
      const new_product = await product.save();
      console.log(&quot;error caught&quot;, new_product);
  
      if (new_product) {
        res.status(201).json({ msg: &quot;New product added&quot;, new_product });
      } else {
        res.status(400).json({ msg: &quot;Unable to create new product&quot; });
      }
    } catch (error) {
      res.status(500).json({ msg: &quot;Internal server error&quot;, error });
    }
  };
  </code></pre>
  <p><strong>Product.js (route)</strong></p>
  <pre><code>const express = require(&quot;express&quot;);
  const router = express.Router();
  
  const ProductController = require(&quot;../controllers/products&quot;);
  const uploadMW = require(&quot;../middleware/fileUpload&quot;);
  
  router.get(&quot;/all&quot;, ProductController.get_products);
  router.post(&quot;/new&quot;, uploadMW.fields([{ name: &quot;thumbnail&quot; }, { name: &quot;image&quot; }]), ProductController.create_product
  );
  
  module.exports = router;
  </code></pre>
  <p><strong>Directory structure</strong></p>
  <p><a href="https://i.stack.imgur.com/TtpXX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TtpXX.jpg" alt="directory structure" /></a></p>
  <p>My OS is windows, so I have included the config to replace the (:) from file names, but nothing seems to work for me. Any help to resolve the same is appreciated.</p>