我正在使用 express 和 body-parser 将大量数据从一台服务器发送到另一台服务器,但一段时间后我收到了这个异常:
{
"message": "request aborted",
"code": "ECONNABORTED",
"expected": 99010,
"length": 99010,
"received": 96872,
"type": "request.aborted"
}
什么可能导致这种情况?如果您需要更多信息,请告诉我。
更新
这是我配置的限制:
application.use(bodyParser.json({ limit: '50mb' }));
application.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
最佳答案
这是 body-parser 使用的 raw body 抛出的异常
来自 express 文档:
request aborted This error will occur when the request is aborted by the client before reading the body has finished. The received property will be set to the number of bytes received before the request was aborted and the expected property is set to the number of expected bytes. The status property is set to 400 and type property is set to 'request.aborted'.
例如,如果您想处理从正文解析器抛出的所有请求
'encoding.unsupported',
'entity.parse.failed',
'entity.verify.failed',
'request.aborted',
'request.size.invalid',
'stream.encoding.set',
'parameters.too.many',
'charset.unsupported',
'encoding.unsupported',
'entity.too.large'
使用这个中间件$ npm i express-body-parser-error-handler
并简单地把它放在你的 body 解析器初始化之后const bodyParserErrorHandler = require('express-body-parser-error-handler')
...
...
application.use(bodyParser.json({ limit: '50mb' }));
application.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
application.use(bodyParserErrorHandler());
...
关于javascript - Node.js 请求中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55419866/