How can I use body-parser with LoopBack?

前端 未结 8 700
别那么骄傲
别那么骄傲 2020-12-24 13:56

I see that LoopBack has the Express 3.x middleware built-in. Indeed, body-parser is in loopback/node_modules. But I cannot figure out how to use it as middlewar

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 14:36

    After hours of frustration, I just added it to middleware.json like so:

    "parse": {
        "body-parser#json": {},
        "body-parser#urlencoded": {"params": { "extended": true }}
    }
    

    It is installed as a dependency. Now I have form data in req.body in my routes. My server/boot/routes.js looks like this:

    module.exports = function(app) {
        app.post('/mailing_list', function(req, res) {
            console.log(req.body.email);
            res.send({"status": 1, "message": "Successfully added to mailing list."})
        });
    }
    

提交回复
热议问题