expressmultipartform-datamulterbody-parserformidable

Express JS modules to parse form-data text from Postman


Postman Screen Shot

I have just 3 variables and posting using postman to sever.

I need a solution to parse just form-data text in Express JS

When I searched in net, I see people just suggested that, We cant use body-parser to handle form-data and need to use multer, busboy, multiparty

Also people suggest if not sending files need not use multer and formidable.

But can anyone explain how exactly to use this with node js. When I see modules github, i am not able to understand to use it for my needs.

https://stackoverflow.com/a/36199881/5078763

I know setting x-www-form-urlencoded option in postman works but I need for form-data

app.post('/addUsrForm', addUsrFnc);

function addUsrFnc(req, res)
{
    console.log("Data Received : ");

    var namUserVar =
    {
        nameKey: req.body.nameKey,
        mailKey: req.body.mailKey,
        mobileKey: req.body.mobileKey
    };
    console.log(NquUsrJsnObjVar);
}

Solution

  • This answer provides good detailing of the different use cases for html form encoding. What does enctype='multipart/form-data' mean?

    x-www-form-urlencoded is the default. multipart/form-data is for larger data sends, such as entire files.

    Postman settings aside, if your server needs to handle multipart/form-data, install multer and use it like so...

    if just sending text fields in a multipart/form-data encoding:

    var multer = require('multer')
    var multParse = multer()
    ...
    function handler(req, res) {
       // fields will be parsed in req.body
    }
    ...
    app.post('/', multParse.none(), handler)
    

    Follow the multer api on the multer github page if you actually are sending files and not just text fields.