Wednesday, 25 July 2018

How to change the default configuration of Sails JS middleware

I am trying to alter the settings of a Sails JS app and am having a bit of bother passing a parameter to the body-Parser in order to change the default settings.

I previously had the issue described here: Posting larger files

I believe this problem has been answered correctly by changing the default 'limit' option, as a 100kb default size minus the 33% overhead from the formData object is pretty consistent with what size of file I can/can't send. So the solution proposed was this:

var bodyParser = require('body-parser');
...
app.use(bodyParser.urlencoded( { limit: 1048576 } )); 

but am unable to implement the solution in my Sails app. I have read through the Sails Documentation on changing the middleware settings, and tried the following in my config/http.js file...

First attempt - wrapper

/**
 * HTTP Server Settings
 * (sails.config.http)
 */

module.exports.http = {

middleware: {

  order: [
   'startRequestTimer',
   'cookieParser',
   'session',
   'refreshSessionCookie',
   'bodyParserInit',
   'bodyParser',
   'handleBodyParserError',
   'compress',
   'methodOverride',
   'poweredBy',
   '$custom',
   'requestLogger',
   'router',
   'www',
   'favicon',
   '404',
   '500'
  ],

   bodyParserInit   : (function (){
     let bodyParser = require('body-parser');

     return bodyParser( { extended: true, limit: 1073741824 } )
   })(),

  )
},

// cache: 31557600000

};

Second attempt - overwrite

/**
 * HTTP Server Settings
 * (sails.config.http)
 */

module.exports.http = {

middleware: {

  order: [
   'startRequestTimer',
   'cookieParser',
   'session',
   'refreshSessionCookie',
   //'bodyParserInit',
   'bodyParser',
   'handleBodyParserError',
   'compress',
   'methodOverride',
   'poweredBy',
   '$custom',
   'requestLogger',
   'router',
   'www',
   'favicon',
   '404',
   '500'
  ],

   bodyParser: (function _configureBodyParser(){
      let skipper = require('skipper');
      let middlewareFn = skipper({
          limit: 1073741824,
      });
      return middlewareFn;
   })(),

  )
},

// cache: 31557600000

};

However, neither attempt solved my issue as the limit still appears to be set 100kb regardless of anything I've done. How do I implement this correctly, so that the body parser accepts files of up to 50kb? I presume either I am not configuring this correctly or something else is overwriting what I've done.

EDIT: I'm using Sails version > 12.0.X



from How to change the default configuration of Sails JS middleware

No comments:

Post a Comment