7

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' }));
LHM
  • 721
  • 12
  • 31
Agustín Clemente
  • 1,178
  • 3
  • 13
  • 23

1 Answers1

4

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());
...
Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48