简体   繁体   English

Node.js 请求中止

[英]Node.js request aborted

I'm using express and body-parser to send large amounts of data from one server to another, but I'm receiving this exception after some time:我正在使用 express 和 body-parser 将大量数据从一台服务器发送到另一台服务器,但一段时间后我收到了这个异常:

{
    "message": "request  aborted",
    "code": "ECONNABORTED",
    "expected": 99010,
    "length": 99010,
    "received": 96872,
    "type": "request.aborted"
}

What could cause this?什么可能导致这种情况? If you need more information, please let me know.如果您需要更多信息,请告诉我。

UPDATE This is my configured limit:更新这是我配置的限制:

application.use(bodyParser.json({ limit: '50mb' }));
application.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));

this is an exception thrown by raw body that is used by body-parser这是由 body-parser 使用的 raw body 抛出的异常

from express-docs:来自快递文档:

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'. status 属性设置为 400,type 属性设置为“request.aborted”。

if you want to handle all request thrown from body parser for example例如,如果您想处理从正文解析器抛出的所有请求

  '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'

use this middleware使用这个中间件

$ npm i express-body-parser-error-handler

and simply put it right after your body-parser initialization并在您的 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());
...

Express as a 2 minute timeout by default. 默认情况下,表示为2分钟超时。

check this post https://stackoverflow.com/a/49746488/11277076 检查此帖子https://stackoverflow.com/a/49746488/11277076

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM