I want to access req.body from multiple middlewares in my routes. But I can't seem to find a way to get parsed body data from my custom middle wares.
For instance:
let mymiddleware = (req, res, next) => {
console.log(req.body); //<- undefined
next();
}
router.post('/', mymiddleware,
multer({fileFilter: filefilter,storage:storage}).any(),//<-this file uploader middleware retrieves req.body without problem.
function (req, res) {
console.log(req.body); //<- parsed body showing here.
});
Am I missing something? Why does req.body not parsing when multiple middleware is used? (Yes I believe I have set my body-parser properly since it's working in handlers)
(EDIT: more details on this problem)
Here is my body-parser setting in app.js before any app.use routers:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
else res.redirect('/');
}
app.use('/console/create_album', ensureAuthenticated, create_album);
and my form on the front end:
<form enctype="multipart/form-data" action='/console/create_album' method='post'>
<!---Just some text inputs, and textarea, file inputs are here -->
</form>
The files and body-parsed data are successfully retrieved without problems when I don't use 'mymiddleware' middleware on router.post.