Multer diskStorage destination errors on production environment
See original GitHub issueHello,
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;
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:14 (4 by maintainers)
Top Results From Across the Web
node.js - Multer Image Not loaded on Production Build React?
I am using MERN to create simple eCommerce I am facing error when i am in production mode. The file uploads ...
Read more >How to use the multer function in multer - Snyk
To help you get started, we've selected a few multer examples, ... JWT_SECRET if (!JWT_SECRET) { throw new Error("JWT_SECRET environment variable undefined!
Read more >Uploading Files Using Multer in a Node.js Application
The first is error which we are going to pass null to. The second is the destination folder which is public. The second...
Read more >Build an Email Application using Node JS Express JS with ...
... Handling Files over a Node JS server. Configuring Multer; Setting up multer diskStorage; Explanation. multer.diskStorage; 1. destination; 2. filename.
Read more >Handling File Uploads in Node.js with Express and Multer
In this article, we'll be using Multer, Formidable and Multiparty to ... diskStorage({ destination: function(req, file, cb) { cb(null, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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’
Try using
destination: require.main?.path + "/" +" public/images/ "
(tested using “multer”: “^1.4.2” , in typescript project)