1

I am using node, express, html and i am trying to post a to my server side using a html form. The problem is I get {} as my req.body.

My html form is the following:

    <form method = 'post' action='get_name' enctype="multipart/form-data">
      <input type="text" name="form_name"><br>
      <input type="submit" value="Upload name">
    </form>

I use the following in the begining of the my node.js file:

app.use(bodyParser.urlencoded({limit:'5mb', extended:false}));
app.use(busboy());

My app.post is the following:

app.post('/get_name',function(req, res, next){
        console.log("the name of the form is : ", req.body);
        res.redirect('/admin');
});

When i am trying to get req.body.form_name I get undefined. I cant find out what is wrong with my code. Any suggestions are welcome. :)

3 Answers 3

5

If you're going to use busboy, you should follow the documentation:

https://github.com/mscdex/busboy

Otherwise, bodyParser() does not support multi-part form data. I personally recommend this library for it's simplicity:

https://www.npmjs.com/package/multer

This will populate req.body the way you are intending to use it.

Sign up to request clarification or add additional context in comments.

1 Comment

since bodyParser() does not support multi-part form data, if you want to populate req.body, try using this library:npmjs.com/package/multer .. I personally use this over other options because of it's simplicity
0

Use connect-multiparty module for multipart/form-data and add that in middleware of API route.

let multipart = require('connect-multiparty');
let multipartMiddleware = multipart();

 router.route('/customer').post(validate(validations.customerValidation.registerCustomer),multipartMiddleware,CONTROLLER.CustomerBaseController.registerCustomer);

It is working on my side.

Comments

-3

Try this:

app.post('/get_name',function(req, res, next){
        console.log("to name of the form is : ", req.param('form_name'));
        res.redirect('/admin');
});

1 Comment

Might be worth console.log()ing your req.params above that other console.log, see if you can spot the correct info in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.