Sails Body Parser配置

时间:2016-08-11 14:12:46

标签: node.js heroku sails.js sails-skipper

我在Heroku上托管了一个应用程序,它发送一个巨大的数量的JSON。我最初得到一个bodyParser - 请求实体太大的错误(HTTP 400)。谷歌搜索,我遇到了一些stackoverflow / github问题链接

Sails.js bodyParser - request entity too large on version 0.10.5) (https://github.com/balderdashy/skipper/issues/144

我尝试更新我的http.js.现在我的身体解析器看起来像这样:

bodyParser: {
  fn: require('skipper'),
  options:{
    limit: '10mb'
  }
}

这解决了400错误,但现在我收到了Heroku H13错误:

2016-08-11T14:02:08.861774+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response"

此时,我有点难过。我查看了Heroku关于H13错误的文档

https://devcenter.heroku.com/articles/error-codes#h13-connection-closed-without-response),

但我不确定是否

  1. 我需要使用Heroku配置更多(我甚至可以这样做)或
  2. 使用Sails bodyParser配置或
  3. 执行更多操作
  4. 两者的组合
  5. 我目前正在使用Sails版本0.11.2。

    [更新]

    研究更多引导我到这些链接:

    https://github.com/balderdashy/sails/issues/2653

    https://github.com/balderdashy/skipper/issues/22

    我注意到一个人在中间件块之外有他们的bodyParser配置。我也试过把我的外面移动到外面,它似乎修复了我收到的400和503 H13 Heroku问题(我慢慢地试图远离问题)。我的新问题是,为什么以下工作,特别是因为bodyParser注释块是里面中间件块?

    module.exports.http = {
       /****************************************************************************
       *                                                                           *
       * Express middleware to use for every Sails request. To add custom          *
       * middleware to the mix, add a function to the middleware config object and *
       * add its key to the "order" array. The $custom key is reserved for         *
       * backwards-compatibility with Sails v0.9.x apps that use the               *
       * `customMiddleware` config option.                                         *
       *                                                                           *
       ****************************************************************************/
    
      middleware: {
        passportInit: require('passport').initialize(),
        passportSession: require('passport').session(),
        /***************************************************************************
         *                                                                          *
         * 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',
          'passportInit',
          'passportSession',
          'myRequestLogger',
          'bodyParser',
          'handleBodyParserError',
          'compress',
          'methodOverride',
          'poweredBy',
          '$custom',
          'router',
          'www',
          'favicon',
          '404',
          '500'
        ],
        /****************************************************************************
         *                                                                           *
         * Example custom middleware; logs each request to the console.              *
         *                                                                           *
         ****************************************************************************/
    
        // myRequestLogger: function (req, res, next) {
        //     console.log("Requested :: ", req.method, req.url);
        //     return next();
        // }
    
    
        /***************************************************************************
         *                                                                          *
         * The body parser that will handle incoming multipart HTTP requests. By    *
         * default as of v0.10, Sails uses                                          *
         * [skipper](http://github.com/balderdashy/skipper). See                    *
         * http://www.senchalabs.org/connect/multipart.html for other options.      *
         *                                                                          *
         ***************************************************************************/
    
        // bodyParser: require('skipper')
    
      },
      /***************************************************************************
       *                                                                          *
       * The number of seconds to cache flat files on disk being served by        *
       * Express static middleware (by default, these files are in `.tmp/public`) *
       *                                                                          *
       * The HTTP static cache is only active in a 'production' environment,      *
       * since that's the only time Express will cache flat-files.                *
       *                                                                          *
       ***************************************************************************/
    
      // cache: 31557600000
    
      bodyParser: function () {
        var opts = {limit:'10mb'};
        var fn;
    
        // Default to built-in bodyParser:
        fn = require('skipper');
        return fn(opts);
    
      }
    };
    

1 个答案:

答案 0 :(得分:0)

您可以将中间件放在中间件对象的内部和外部。正如医生所说的那样,你把中间件放在那些没有跟随app.use(中间件)'惯例。

由于您的bodysarser中间件返回require('skipper')({limit:'10mb'})而不是require('skipper'),这是非常不同的,因此它不会遵循app.use(中间件)'惯例,应该像你一样放在http模块的根目录外面。