Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
How to handle "PayloadTooLargeError" with a response or a custom error?

im trying to put a payload limit of 1kb using :

app.use(bodyParser.json({
 limit: "1kb" ,
 strict:true
       }));

its working perfectly as it suppose to work , but the only problem is i get an error of

PayloadTooLargeError: request entity too large
at readStream (E:\Work\FIGHTME\V1.0\api\node_modules\raw-body\index.js:155:17)
at getRawBody (E:\Work\FIGHTME\V1.0\api\node_modules\raw-body\index.js:108:12)
at read (E:\Work\FIGHTME\V1.0\api\node_modules\body-parser\lib\read.js:77:3)
at jsonParser (E:\Work\FIGHTME\V1.0\api\node_modules\body-parser\lib\types\json.js:135:5)
at Layer.handle [as handle_request] (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:317:13)
at E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:284:7
at Function.process_params (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:335:12)
at next (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:275:10)
at expressInit (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\middleware\init.js:40:5)

is there any way to handle this error through callback or anything so i show a custom error or send a custom response ?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Have you tried like this

app.use(express.json({limit: '1kb'}));

Parsing request body as middleware is available in Express v4.16.0 onwards.

EDIT

If you want to customise your error message, then you need to define your error-handler middle-ware to do that. Something like this:

// Not tested code.
// Usually added as last middleware
app.use(function (err, req, res, next) {
    if(err instanceof PayloadTooLargeError){
        //res.status(<your status code>).send(<your response>);
        res.status(400).send("Customized Response");
    }
    else{
        next(err);
    }
})
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda