Wednesday, 3 May 2017

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,wrap(data,status))
}
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!



via meobeo173

No comments:

Post a Comment