javascript - Edit the response in ExpressJS -
i'm trying edit response request in express. when request xhr request, want return body inside javascript object (this return different parts of page different pieces in javascript object, i.e. { header: '<header></header>', footer: '<footer></footer>' }
.
i can't seem find part of response object holds actual response. direction think i'm meant go, can't work.
app.use(function(req, res, next) { res.on('send', function() { if(req.xhr) { //if page requested ajax, return json res.body = {body: res.body}; } }); next(); });
you'll want monkey-patch send method
app.use(function(req,res,next){ var send=res.send; res.send = function(){ //use arguments here body out (won't first property if there's response code passed through) console.log("sending",arguments); send.apply(res,arguments); } next(); })
look @ source response.send method find out how parse arguments. https://github.com/visionmedia/express/blob/master/lib/response.js#l81
Comments
Post a Comment