使用自定义Skipper正文解析器配置时出错

时间:2016-11-11 19:50:51

标签: sails.js sails-skipper

我使用的是风帆版0.10.5。我在http.js

中设置了bodyParser
bodyParser: require('skipper')({
  limit: 52428800
})

启动服务器时我一直收到此错误:

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: {/* ... */}
}
```

之后没有错误。我究竟做错了什么?我尝试按照错误的说明进行操作,但是对于船长我无法将文件上传到S3而不仅仅是几个mb。我试过这个并不起作用:

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

根据上面的信息。

1 个答案:

答案 0 :(得分:2)

由于Sails<中的默认bodyParser设置存在错误0.12,配置中间件的常规方法不起作用。最简单的解决方案是简单地将中间件订单中的bodyParser替换为另一个自定义中间件名称,并配置 ,例如:

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
    })

  }
}