0

我正在使用sails 0.10.5版。我的 bodyParser 设置如下http.js

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

这是根据上面的消息。

4

1 回答 1

2

由于bodyParserSails < 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
    })

  }
}
于 2016-12-07T19:05:26.947 回答