1
votes

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.

3
Please show your body-parser configuration, show where it is defined relative to the above route and show what kind of request is being sent. It's probably the case that your body-parser is not configured for this type of post data and multer is what is responsible for reading the body data and parsing it here. - jfriend00
Hello @jfriend00 I have updated my post with more details. Thanks. - Baksa Gimm

3 Answers

2
votes

You've configured body-parser for two data types, json and url-encoded. The body-parser middleware will examine each incoming request and only do anything with the request if it is one of those two types that you have configured it for.

But, when you do your file upload, it's not one of those two types so therefore, the body-parser does not read and parse it.

The multer() middleware, on the other hand, is expecting the type for a file upload so it reads, parses and processes the body for you.

FYI, the body-parser doc specifically says this:

This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:

busboy and connect-busboy

multiparty and connect-multiparty

formidable

multer

And, your form specifies enctype="multipart/form-data" which is multipart.

0
votes

You need to initialize body parser. Refer https://www.npmjs.com/package/body-parser and check out their example.

EDIT: Multer stores a file in your server at the specified path. You then have to use fs to retrieve the file. For multer you need to send the data in the form of 'form-data' and not urlencoded or json. So bodyparser cannot detect these. If data is sent as urlencoded and json you can configure bodyparser as in the exmaple and access req.body.

0
votes

Change your code to :

router.post('/',
    multer({fileFilter: filefilter,storage:storage}).any(),
    mymiddleware,
    function (req, res) {
        console.log(req.body); //<- parsed body showing here.
    });

Please note node route will execute in a chronology of defined. So to access req.body parser must be defined first.