Error using custom Skipper body parser configuration

微笑、不失礼 提交于 2019-12-02 15:55:59

问题


I'm using sails version 0.10.5. I have the bodyParser set like so in http.js:

bodyParser: require('skipper')({
  limit: 52428800
})

And I keep getting this error when starting the server:

error: TypeError: Cannot read property 'toLowerCase' of undefined
    at _parseHTTPBody (/var/www/testapp-backend/node_modules/skipper/index.js:49:19)
    at /var/www/testapp-backend/node_modules/sails/lib/hooks/http/middleware/defaults.js:104:28
    at module.exports (/var/www/testapp-backend/node_modules/sails/lib/hooks/http/middleware/defaults.js:144:7)
    at loadExpress (/var/www/testapp-backend/node_modules/sails/lib/hooks/http/initialize.js:103:65)
    at /var/www/testapp-backend/node_modules/sails/lib/hooks/http/index.js:190:18
    at /var/www/testapp-backend/node_modules/sails/lib/app/private/after.js:91:14
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:232:13
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:119:25
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:24:16
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:229:17
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:516:34
    at handlerFn (/var/www/testapp-backend/node_modules/sails/lib/app/private/after.js:78:13)
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:511:21
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:227:13
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:111:13
    at Array.forEach (native)
    at _each (/var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:32:24)
    at async.each (/var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:110:9)
    at _asyncMap (/var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:226:9)
    at Object.map (/var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:204:23)
    at _parallel (/var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:509:20)
    at Object.async.parallel (/var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:539:9)
    at Sails.emitter.after (/var/www/testapp-backend/node_modules/sails/lib/app/private/after.js:89:11)
    at Hook.initialize (/var/www/testapp-backend/node_modules/sails/lib/hooks/http/index.js:189:15)
    at Hook.bound [as initialize] (/var/www/testapp-backend/node_modules/lodash/dist/lodash.js:729:21)
    at /var/www/testapp-backend/node_modules/sails/lib/hooks/index.js:132:16
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:425:17
    at /var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:419:17
    at Array.forEach (native)
    at _each (/var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:32:24)
    at Immediate.taskComplete (/var/www/testapp-backend/node_modules/sails/node_modules/async/lib/async.js:418:13)
    at processImmediate [as _immediateCallback] (timers.js:367:17) [TypeError: Cannot read property 'toLowerCase' of undefined]

error: Encountered an error when trying to use configured bodyParser.
error: Usually, this means that it was installed incorrectly.
error: A custom bodyParser can be configured without calling the wrapper function- e.g.:
```
bodyParser: require("connect-busboy")
```
error: Alternatively, if you need to provide options:
```
bodyParser: {
fn: require("connect-busboy"),
options: {/* ... */}
}
```

There are no errors there after. What am I doing wrong? I tried following the instructions of the error but with skipper I'm not able to upload files to S3 greater than just a few mb's. I tried this and it doesn't work:

bodyParser: {
fn: require("skipper"),
options: {limit: 52428800}
}

that is as per the message above.


回答1:


Due to a bug in the default bodyParser setting in Sails < 0.12, the normal method of configuring the middleware won't work. The easiest solution is to simply replace bodyParser in the middleware order with another, custom middleware name and configure that instead, e.g.:

module.exports.http = {

  middleware: {

    // The order in which middleware should be run for HTTP request.
    // (the Sails router is invoked by the "router" middleware below.)
    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      // 'bodyParser', // <-- don't add the "bodyParser" middleware to the stack
      'skipper',   // <-- instead use this "custom" middleware
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      '$custom',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ],

    // Configure Skipper
    skipper: require('skipper')({
      limit: 52428800
    })

  }
}


来源:https://stackoverflow.com/questions/40555199/error-using-custom-skipper-body-parser-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!