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

145
Vistas
Overriding Expressjs response in middleware is not working

I want to modify response body before returning it, i've used solution from this answer

But it didn't work, here's my code:

app.js

function modify(req, res, next) {
  var json = res.json;
  console.log("step 1");
  res.json = function(data) {
    console.log("step 2");
    json.call(this, "modified");
  };
  next();
}
app.use(modify);

in router:

router.get('/',(req,res,next)=>{
  res.json('Welcome')
  next()
})

Middleware has been called but the override function (step 2) was never invoked, please tell me what's wrong with my code, thanks!

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

0

Use next() to only for passing to next function

router.get('/',(req,res,next)=>{
    if(wantToStop=='true'){
       res.json('Welcome')
    }else{
      next()
   }

 })
about 4 years ago · Santiago Trujillo 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