Created
February 27, 2013 21:02
-
-
Save mrlannigan/5051687 to your computer and use it in GitHub Desktop.
res.render Override Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Override res.render to do any pre/post processing | |
*/ | |
app.use(function(req, res, next) { | |
var render = res.render; | |
res.render = function(view, options, fn) { | |
var self = this, | |
options = options || {}, | |
req = this.req, | |
app = req.app, | |
defaultFn; | |
if ('function' == typeof options) { | |
fn = options, options = {}; | |
} | |
defaultFn = function(err, str){ | |
if (err) return req.next(err); | |
self.send(str); | |
}; | |
if ('function' != typeof fn) { | |
fn = defaultFn; | |
} | |
render.call(self, view, options, function(err, str) { | |
// do whatever post processing you want | |
fn(err, str); | |
}); | |
}; | |
next(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment