Sails Body 解析器配置
Sails Body Parser Configuration
我有一个托管在 Heroku 上的应用程序,它发送 大量 数量的 JSON。我最初收到 bodyParser - 请求实体太大错误 (HTTP 400)。谷歌搜索,我发现了一些 Whosebug/github 问题链接
(Sails.js bodyParser - request entity too large on version 0.10.5)
(https://github.com/balderdashy/skipper/issues/144)
其中我尝试更新我的 http.js。现在我的 body 解析器看起来像这样:
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 body解析器配置或
做更多事情
- 两者的结合
我目前使用的是 0.11.2 版的 Sails。
[更新]
进一步研究将我带到这些链接:
https://github.com/balderdashy/sails/issues/2653
https://github.com/balderdashy/skipper/issues/22
我注意到有人在中间件块之外设置了 body 解析器配置。我也尝试将我的移动到外面,它似乎解决了我收到的 400 和 503 H13 Heroku 问题(我慢慢地避开了这个问题)。我的新问题是,为什么下面的工作有效,特别是因为 bodyParser 注释块 inside 中间件块?
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 中间件 return require('skipper')({limit:'10mb'})
而不是 require('skipper')
这是非常不同的,它不遵循 'app.use(middleware)' 约定,应该放在外面,在http 模块的根目录,就像您所做的那样。
我有一个托管在 Heroku 上的应用程序,它发送 大量 数量的 JSON。我最初收到 bodyParser - 请求实体太大错误 (HTTP 400)。谷歌搜索,我发现了一些 Whosebug/github 问题链接
(Sails.js bodyParser - request entity too large on version 0.10.5) (https://github.com/balderdashy/skipper/issues/144)
其中我尝试更新我的 http.js。现在我的 body 解析器看起来像这样:
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 body解析器配置或 做更多事情
- 两者的结合
我目前使用的是 0.11.2 版的 Sails。
[更新]
进一步研究将我带到这些链接:
https://github.com/balderdashy/sails/issues/2653
https://github.com/balderdashy/skipper/issues/22
我注意到有人在中间件块之外设置了 body 解析器配置。我也尝试将我的移动到外面,它似乎解决了我收到的 400 和 503 H13 Heroku 问题(我慢慢地避开了这个问题)。我的新问题是,为什么下面的工作有效,特别是因为 bodyParser 注释块 inside 中间件块?
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 中间件 return require('skipper')({limit:'10mb'})
而不是 require('skipper')
这是非常不同的,它不遵循 'app.use(middleware)' 约定,应该放在外面,在http 模块的根目录,就像您所做的那样。