var express = require('express');
var router = express.Router();
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({
storage: storage, fileFilter: function (req, file, cb) {
if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg') {
return cb(null, false);
}
return cb(null, true);
}
}).any();
/* get home page. */
router.get('/', function (req, res) {
res.render('index', { title: 'express' });
});
router.post('/', function (req, res) {
upload(req, res, function (err) {
if (err) {
//I want to jump to another page
} else {
res.send(req.files);
}
});
});
module.exports = router;
In the event of an error (if (err)), I am trying to redirect to a specific page in my views folder named "wrong". However, simple redirection methods like res.redirect('wrong') or res.redirect('views/wrong') are not working for me. I have tried numerous approaches without success. If I upload a file that is not an image, it redirects to a page displaying only an empty array '[]'. How can I achieve successful redirection in this scenario?