Sails.js change bodyParser for specific paths

418 Views Asked by At

In Sails.js 0.10.5, I want to replace bodyParser for specific paths. For example, use a different body parser for path '/app/upload' and for the rest use the default. How do I do this?

1

There are 1 best solutions below

0
elssar On

You can do this by overriding config/http.js. Add your custom parser to the middleware, and replace bodyParser in the order with your custom parser.

Something like this should work

module.exports.http = {
    middleware: {
        superBodyParser: function (req, res, next) {
            if (req.path === '/app/upload') {
                // your custom parser
            }
            else {
                require('skipper')(req, res, next);
            }
        },
        order: [
          'startRequestTimer',
          'cookieParser',
          'session',
          'myRequestLogger',
          // 'bodyParser',  <-- not required anymore
          'superBodyParser' 
          'handleBodyParserError',
          'compress',
          'methodOverride',
          'poweredBy',
          '$custom',
          'router',
          'www',
          'favicon',
          '404',
          '500'
        ]
    }
};