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

258
Vistas
How can I handle json-parse errors in node.js

My problem seems to be fairly simple and common, but nevertheless I can't seem to find anything about it after hours of research:

I want to retrieve JSON-data in Node.js via http-POST using Express. To keep it simple, I want to use the app.use(express.json()); function (see code).

When the JSON-data is correct JSON-syntax, everything works fine, but when there is a syntax error in the JSON-data, it handles the error on its own, automatically replying to the client and logging a huge error message on the console. What I'd like to do is to handle the error the way I want, but I'm not sure how I can manage to do that.

The code I'm using is the following:

const express = require('express');

const app = express();

app.use(express.json());

app.post('/', function(req, res) {
    // do something with the retreived JSON-data
});

I would be very thankful if somebody could give me a tip/solution! :)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could handle malformed JSON errors using this, as the error is an instance of SyntaxError

app.use((error, request, response, next) => {
    if (error instanceof SyntaxError)
        response.status(404).send('your error message');
    else 
        next();
});
about 4 years ago · Juan Pablo Isaza 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