我有一个托管在Heroku上的应用程序,它向发送大量的JSON 。我最初收到了一个bodyParser请求实体太大的错误(HTTP400)。在谷歌搜索时,我发现了一些堆栈溢出/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),
但我不确定
我目前使用的是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);
}
};发布于 2016-08-12 15:17:46
您可以将您的中间件放在中间件对象内外。正如医生所说,您把不遵循‘app.use(中间件)’约定的中间件放在了外部。
由于您的解析器中间件返回的是require('skipper')({limit:'10mb'})而不是非常不同的require('skipper'),所以它不遵循‘app.use(中间件)’的约定,应该像您一样放置在http模块的根目录之外。
https://stackoverflow.com/questions/38898815
复制相似问题