multer: Multer diskStorage destination errors on production environment
Hello,
On development environment code below works properly but on production nothing works.When I opened logs there are this error: Error OccuredError: ENOENT: no such file or directory, open ‘public/images/8c3f666974_kiwi.png’.2 days I am trying to resolve it but… no result I try many things read many articles but nothing … What is the problem?I read all multer documentation and I think I am implementing it properly?
This is code:
const express = require("express");
const router = express.Router();
const multer = require('multer');
const db = require("../connection/databaseConn");
const Product = require("../models/Product");
const encryption = require("../encryption/encryption");
const path = require('path')
let storage;
let upload;
router.get("/upload",(req,res)=>{
res.render("upload");
});
let imagesNames = [];
new Promise((resolve, reject)=>{
resolve(
storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null,"public/images/")
},
filename: function (req, file, cb) {
let imageId = encryption.generateId();
let imageNameWithId = imageId.substr(0,10);
imagesNames.push({name:imageNameWithId,id:imageId});
let fileName = "" + imageNameWithId + "_" + file.originalname;
cb(null, fileName)
}
}))
}).then((storage)=>{
upload = multer({storage:storage}).any();
})
router.post('/upload', function(req, res) {
upload(req, res, function(err) {
if(err) {
console.log('Error Occured' + err);
return;
}
let files = req.files;
Product.findById(req.session.productId).then((p)=>{
for(let i = 0; i < files.length;i++){
p.images.push({imageId:imagesNames[i].id,imageName :"/static/images/" + imagesNames[i].name + "_" + files[i].originalname});
}
p.save();
}).then(()=>{
req.session.addMessage = "Product created!";
res.redirect("/categoriesLoad");
});
})
});
module.exports = router;
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 1
- Comments: 16 (4 by maintainers)
Try using
destination: require.main?.path + "/" +" public/images/ "
(tested using “multer”: “^1.4.2” , in typescript project)
This is error on /upload path, from heroku logs:
2018-10-03T14:29:07.566481+00:00 heroku[router]: at=info method=POST path=“/upload” host=enigmatic-falls-17964.herokuapp.com request_id=74417b65-3e3d-4b70-941e-c3199325bf6c fwd=“46.55.152.189” dyno=web.1 connect=0ms service=9884ms status=500 bytes=404 protocol=https 2018-10-03T14:29:07.565769+00:00 app[web.1]: Error: ENOENT: no such file or directory, open ‘/app/public/images/5a901a8328_IMG_20181003_142936.jpg’
Thanks for suggestion, but again on development it works and on production return “Internal server error”. As you can see on image below, in green color ,on left side, is image which I add on development environment in directory images: http://s1356.photobucket.com/user/acho999/media/dev-env_zpshjjtaivd.jpg.html. And as I wrote above on prod environment doesn’t work… this is take me yet 5 days at least…