How can I use body-parser with LoopBack? How can I use body-parser with LoopBack? express express

How can I use body-parser with LoopBack?


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."})    });}


Just to be more clear about what it takes to get this working (because I still struggled for a while after finding this answer!), here are the steps I took:

As described above, in $APP_HOME/server/middleware.json, add the body-parser to the "parse" section:

{  "initial:before": {    "loopback#favicon": {}  },  "initial": {    "compression": {},    "cors": {      "params": {        "origin": true,        "credentials": true,        "maxAge": 86400      }    }  },  "session": {  },  "auth": {  },  "parse": {    "body-parser#json": {},    "body-parser#urlencoded": {"params": { "extended": true }}  },  "routes": {  },  "files": {  },  "final": {    "loopback#urlNotFound": {}  },  "final:after": {    "errorhandler": {}  }}

Next, I added the parser setup to $APP_HOME/server/server.js:

var loopback = require('loopback');var bodyParser = require('body-parser');var multer = require('multer');var boot = require('loopback-boot');var app = module.exports = loopback();app.use(bodyParser.json()); // for parsing application/jsonapp.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencodedapp.use(multer()); // for parsing multipart/form-dataapp.start = function() {......cont'd

Then, since I didn't want to mess with custom routes, I added the following to $APP_HOME/common/models/model.js:

module.exports = function(Model) {  Model.incoming = function(req, cb) {    cb(null, 'Hey there, ' + req.body.sender);  }  Model.remoteMethod(    'incoming',    { accepts: [      { arg: 'req', type: 'object', http: function(ctx) {        return ctx.req;      }     }],    returns: {arg: 'summary', type: 'string'}    }  );};

I can now run my app with $> slc run .

When I post to the endpoint, it now gets parsed properly, and all is well with the world. I hope this helps someone else!


I'm using loopback 2.14.0:

To make use of the body-parser in your custom bootscript routes you should only need to:

1) install body-parsernpm install body-parser --save

2) Register the the module in middleware.json

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

There is no need to require the parser setup in server.js, loopback does this for you when you register the middleware.

Please note body parser is now installed in your source "node_modules" directory as well as in the loopback modules directory.

If at all possible try register custom remote methods as described in the loopback documentation.

Registering routes this way gives you access to loopback's body-parser out of the box and is the 'cleanest' implementation.