Skip to content

Instantly share code, notes, and snippets.

@zubairalam
Forked from mrlannigan/res.render.js
Created November 24, 2016 08:07
Show Gist options
  • Save zubairalam/be8adf8399f9f11596dfba7416e54e58 to your computer and use it in GitHub Desktop.
Save zubairalam/be8adf8399f9f11596dfba7416e54e58 to your computer and use it in GitHub Desktop.
res.render Override Middleware
/**
* 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