Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

{
    "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' }));
like image 933
Agustín Clemente Avatar asked Feb 27 '26 19:02

Agustín Clemente


1 Answers

this is an exception thrown by raw body that is used by body-parser

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

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

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());
...
like image 167
Naor Tedgi Avatar answered Mar 02 '26 09:03

Naor Tedgi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!