Node.js 请求已中止

Agu*_*nte 9 javascript node.js express body-parser

我正在使用 Express 和 body-parser 将大量数据从一台服务器发送到另一台服务器,但一段时间后我收到此异常:

{
    "message": "request  aborted",
    "code": "ECONNABORTED",
    "expected": 99010,
    "length": 99010,
    "received": 96872,
    "type": "request.aborted"
}
Run Code Online (Sandbox Code Playgroud)

什么可能导致这种情况?如果您需要更多信息,请告诉我。

更新 这是我配置的限制:

application.use(bodyParser.json({ limit: '50mb' }));
application.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
Run Code Online (Sandbox Code Playgroud)

Nao*_*dgi 5

这是主体解析器使用的原始主体抛出的异常

来自快递文档:

请求中止 当客户端在读取正文完成之前中止请求时,将发生此错误。receive 属性将设置为请求中止之前接收到的字节数,expected 属性将设置为预期字节数。status 属性设置为 400,type 属性设置为“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'
Run Code Online (Sandbox Code Playgroud)

使用这个中间件

$ npm i express-body-parser-error-handler
Run Code Online (Sandbox Code Playgroud)

并简单地把它放在你的 body-parser 初始化之后

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());
...
Run Code Online (Sandbox Code Playgroud)