Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

when i send req upload image in node give Error: ENOENT: no such file or directory,ues multer

const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const multer = require('multer');

const storage = multer.diskStorage({
  destination: function(req, file, cb) {
    cb(null, './uploads/');
  },
  filename: function(req, file, cb) {
    cb(null, new Date().toISOString() + file.originalname);
  }
});

const fileFilter = (req, file, cb) => {
  // reject a file
  if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
    cb(null, true);
  } else {
    cb(null, false);
  }
};

const upload = multer({
  storage: storage,
  limits: {
    fileSize: 1024 * 1024 * 5
  },
  fileFilter: fileFilter
});

router.post("/", checkAuth, upload.single('productImage'), (req, res, next) => {
  const product = new Product({
    _id: new mongoose.Types.ObjectId(),
    name: req.body.name,
    price: req.body.price,
    productImage: req.file.path 
  });
  product
    .save()
    .then(result => {
      console.log(result);
      res.status(201).json({
        message: "Created product successfully",
        createdProduct: {
            name: result.name,
            price: result.price,
            _id: result._id,
            request: {
                type: 'GET',
                url: "http://localhost:3000/products/" + result._id
            }
        }
      });
    })
    .catch(err => {
      console.log(err);
      res.status(500).json({
        error: err
      });
    });
});

module.exports = router;
Comment

PREVIOUS NEXT
Code Example
Javascript :: Using Math Functions in an IF statement 
Javascript :: cproblem upgrading node on windws 
Javascript :: must line ending of word regex match 
Javascript :: javascript show popup on page refresh unsaved changes 
Javascript :: browserify call bundeled function 
Javascript :: how to check if an image exists in js from cross origin 
Javascript :: how to log message with replacing placeholders in the string in js 
Javascript :: The attribute name of [ *ngFor ] must be in lowercase.(attr-lowercase) in VSCode 
Javascript :: javascript for sub set 
Javascript :: Get Multipal Tab Value to One App Script 
Javascript :: serverless unsupported function event 
Javascript :: Unexpected eval or arguments in strict mode 
Javascript :: javascript fix errora 
Javascript :: make express app object accessible from all project modules 
Javascript :: Calling JSON REST Services with FoxPro and wwJsonServiceClient 
Javascript :: vue send event plus variable 
Javascript :: https://web.roblox.com/users/20732870/profile 
Javascript :: npm view parent package 
Javascript :: scenery 
Javascript :: react js error stackoverflaw 
Javascript :: how to disable background when popup open in javascript 
Javascript :: highlight each occurrence of text 
Javascript :: jquery.MultiFile 
Javascript :: salesforce lightning call javascript every x seconds 
Javascript :: add to dictionary node js 
Javascript :: update a particular holder view in recycler 
Javascript :: jquery select convert into input text 
Javascript :: fonction fleche js 
Javascript :: delete all elements with class javascript 
Javascript :: limiting the length of dynamic text inside html element 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =