我有一个托管在Heroku上的应用程序,它发送大量的 JSON。我最初是得到一个bodyParser -请求实体太大的错误(HTTP 400)。在谷歌周围,我遇到了一些stackoverflow/github问题链接
(Sails.js bodyParser -请求实体过大在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),
但我不确定是否
- 我需要配置更多的Heroku(我甚至会怎么做)或
- 使用Sails bodyParser配置或
- 两者的组合
我现在使用的是0.11.2版本的Sails。
(更新)研究更多,我找到了这些链接:
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);
}
};
您可以将中间件放在中间件对象的内部或外部。正如文档所说,您可以在中间件之外放置不遵循"app.use(middleware)"约定的中间件。
由于您的bodyparser中间件返回require('skipper')({limit:'10mb'})
而不是require('skipper')
,这是非常不同的,它不遵循'app.use(中间件)'约定,应该放在外面,在http模块的根,正如您所做的。