1

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!

Community
  • 1
  • 1
meobeo173
  • 617
  • 1
  • 7
  • 20

1 Answers1

0

Use next() to only for passing to next function

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

 })
Love-Kesh
  • 777
  • 7
  • 17