Multer upload files with PM2

强颜欢笑 提交于 2019-12-01 10:31:21

问题


i have a express app and am using multer to uploads files, the problem is when i running the app in the production server with PM2 the multer upload file gave me an error:

"Error: ENOENT: no such file or directory, open 'uploads/img.png' at Error (native)"

but when i run it with "Debug=projectname:* npm start" its just work fine.

note that i already create uploads directory with all the permissions "read and write for anyone".

(sorry for my bad english)


回答1:


I will try to guess the problem.

At your pm2 will you don't use cwd param, so your application starts with wrong root directory.




回答2:


I have solved the same problem by change relative path to absolute path

var storage = multer.diskStorage({
destination: function(req, file, cb) {
  cb(null, __dirname + '/public/uploads')
},
filename: function(req, file, cb) {
  var fileFormat = (file.originalname).split(".");
  cb(null, file.fieldname + '-' + Date.now() + "." + 
  fileFormat[fileFormat.length - 1]);
}
})

The third line before I change just like below

cb(null, './public/uploads')


来源:https://stackoverflow.com/questions/44690479/multer-upload-files-with-pm2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!