For logging/debugging I'd like to output the first hundred characters or so of the response right before its sent to browser. Is there something simple I can do with middleware and the response object to do this?
Ideally its something like:
app.use(function(req, res, next) {
console.log('Response snippet: '+((res.body || '').substr(0,100)));
next();
});
Except the response doesn't have a body and I cannot quite figure out where the current body to be sent back is passed.
UPDATE:
Peter's answer worked, I figure I'd put my middleware code here to save future viewers a click:
App.use(function(req, res, next) {
var end = res.end;
res.end = function(chunk, encoding){
res.end = end;
if (chunk) {
console.log(chunk);
}
res.end(chunk, encoding);
};
next();
});