Below is my code to handle uploadImage route
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'uploads/');
},
filename: function(req, file, cb) {
cb(
null,
file.originalname.split('.')[0] +
'__' +
Date.now() +
'.' +
file.originalname.split('.')[1]
);
}
});
var upload = multer({ storage: storage }).single('thumbnail');
router.post('/uploadImage.json', authenticate, function(req, res) {
upload(req, res, function() {
console.log(req.file); //here always "undefined"
let image = new Image({
image_filename: req.file.filename,
image_url: NODE_ENV.domain_name + '/' + req.file.path,
_creator: req.user._id
});
image
.save()
.then(image => {
res.send(image);
})
.catch(err => {
res.status(400).send({ status: 'error', message: err });
});
});
});
I am running my node application on production by pm2 and below is my pm2 ecosystem.config.js file
module.exports = {
apps : [
{
name : 'myapp',
script : './myapp/backend/bin/www',
env: {
NODE_ENV: "production"
}
}
]
}
and I run
pm2 start ecosystem.config.js
and try to upload a image , the req.file from multer will be undefind.
But if I change the NODE_ENV to development, everything will be ok.
How can I fixed it?