Home > Net >  How to get body form data in nodejs express?
How to get body form data in nodejs express?

Time:10-18

This is how I sending params from postman

app.post('/hide_feed', middleware.authenticateToken, (req, res, next) => {
    if (req.body.followered_to_id) {
        return res.status(400).json({
            status: 400,
            msg: req.body.followered_to_id
        });
    }
});

Tried adding

app.use(express.urlencoded({
    extended: false // also true
}));

Still not able to get body form data

CodePudding user response:

You can use multer npm package to parse multipart form data for you. This is a simple middleware, so it should be easy to use.

Speaking of urlencoded this middleware handles x-www-form-urlencoded content type.

CodePudding user response:

Tried using multer

app.post('/hide_feed', middleware.authenticateToken, (req, res, next) => {
    let upload = multer({
        storage: storage,
    }).single('');
    upload(req, res, function(err) {
        if (err) {
            return res.status(400).json({
                status: 400,
                msg: "fail"
            });
        } else {
            if (req.body.followered_to_id) {
                return res.status(400).json({
                    status: 400,
                    msg: req.body.followered_to_id
                });
            } else {
                return res.status(400).json({
                    status: 400,
                    msg: "fail"
                });
            }
        }
    });
});
  • Related