How to change the default configuration of Sails JS middleware
Asked Answered
T

2

6

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

Tentative answered 18/7, 2018 at 8:57 Comment(2)
Did you take a look at Skipper's FAQ? github.com/balderdashy/…Izzard
Yes, it suggests something similar to what I've tried.Tentative
S
0

If you are using Sails > v0.12.x:

    module.exports.http = {

      middleware: {
          // ... options
          bodyParser: require('skipper')({ limit: 1073741824 })
      }

    };

For sails < v0.12.x:

    module.exports.http = {

        middleware: {
            order: [
                // ... options
                // 'bodyParser', <- remove bodyParser
                'skipper', // <-- add skipper
            ],
            skipper: require('skipper')({ limit: 1073741824 })
        }

    };

Here's the link to the documentation.

Seigler answered 24/7, 2018 at 16:42 Comment(1)
thanks for your response. I have read the documentation, and your answer is very similar to my second attempt. I've tried again as you have written it and it hasn't worked, I am still only able to upload files under 100kb.Tentative
P
0

Just wondering if you have looked at this Sails GitHubIssue

Seems very much related and from what it seems few people have made it work.

Practice answered 27/7, 2018 at 15:58 Comment(1)
Hi, thanks for your answer. I have looked at this, and tried various solutions posted on that thread, but none of the above appear to be working for me. I did manage to replicate some of their errors, but nothing worked as expected.Tentative

© 2022 - 2024 — McMap. All rights reserved.