我尝试了此处列出的许多解决方案(增加内存限制、添加参数限制、添加类型为“application/json”)来修复此“请求实体太大”错误(它还会返回http代码413)。但他们似乎都没有让这个错误消失。
json 的当前大小范围可以从 200k 到 400k 实体。
这是当前配置:
app.use( bodyParser.json({limit: "15360mb", type:'application/json'}) );
app.use(bodyParser.urlencoded({
limit: "15360mb",
extended: true,
parameterLimit:5000000,
type:'application/json'
}));
app.use(bodyParser())
关于如何提高限制有什么想法吗?
更多信息
这是完整的错误消息(如果有帮助的话):
{ Error: Request Entity Too Large
at respond (/home/nodejs/server/node_modules/elasticsearch/src/lib/transport.js:307:15)
at checkRespForFailure (/home/nodejs/server/node_modules/elasticsearch/src/lib/transport.js:266:7)
at HttpConnector.<anonymous> (/home/nodejs/server/node_modules/elasticsearch/src/lib/connectors/http.js:159:7)
at IncomingMessage.bound (/home/nodejs/server/node_modules/lodash/dist/lodash.js:729:21)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
status: 413,
displayName: 'RequestEntityTooLarge',
message: 'Request Entity Too Large',
path: '/_bulk',
query: {},
body: '<large body here>'
}
解决方案
该错误确实是由于elasticsearch上的配置错误而不是nodejs造成的。通过关注 https://github.com/elastic/elasticsearch-js/issues/241 并在
http.max_content_length: 500mb
中设置 elasticsearch.yml
来解决此问题。
@ryanlutgen 还在此处提供了有关此错误的更多信息的链接。 https://github.com/elastic/elasticsearch/issues/2902
此问题已修复。感谢您的所有投入!
如果你没有解决你的问题,bodyParser 也有一个限制。 你必须使用
app.use(bodyParser({limit: '4MB'}))
我没有使用
bodyParser
,所以我通过以下方法解决了我的问题:
app.use(express.json({
limit: '2MB',
}));