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!
Use next() to only for passing to next function
router.get('/',(req,res,next)=>{
if(wantToStop=='true'){
res.json('Welcome')
}else{
next()
}
})