使用自定义 Skipper 正文解析器配置时出错 [英] Error using custom Skipper body parser configuration

查看:48
本文介绍了使用自定义 Skipper 正文解析器配置时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是风帆版本 0.10.5.我在 http.js 中设置了 bodyParser:

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

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

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

根据上面的消息.

推荐答案

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

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

  }
}

这篇关于使用自定义 Skipper 正文解析器配置时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆