Multer is a middleware package recommended by Express JS to upload files. Checking their error handling section they use this example:
const multer = require('multer')
const upload = multer().single('avatar')
app.post('/profile', function (req, res) {
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
} else if (err) {
// An unknown error occurred when uploading.
}
// Everything went fine.
})
})
And as you can see , that error handling example is executed within the regular code of the route. This is a problem for me because I need to handle those errors before executing two middleware functions that are like this
app.post('/profile', [middlewareFunctions.Authenticate,middlewareFunctions.getDataUploadedFile], function (req, res) {
})
So Basically my question is, Where can I use Multer's error handling section before my middleware gets executed?. Thank you
PS: Someone already posted this question on Stackoverflow but didn't specify correctly and with an example on how it was solved, and besides that the question was posted two years ago , so I don't think his answer is compatible with the current version of Multer.