I want to intercept every call to render, do my own stuff, and then proceed with the original render method. I know this can easily be done through a middleware like this:
function (req, res, next) {
var _render = render;
res.render = function () {
// custom stuff
_render.apply(this, arguments);
}
next();
}
However it seems more efficient to just change the prototype of the response object instead of replacing res.render on every request. I tried such a solution with no success. When logging http.ServerResponse.prototype
there's no trace of any render method.
Finally i've tried to just intercept app.render instead, like this:
var _render = app.render;
app.render = function () {
// this is refering to app instead of res...
_render.apply(this, arguments);
}
That does fulfill my criteria of only being done once, but it is called on the app object and not the res object which means I can't access the res or req objects.
Basically what I think I would like to do is something like:
var _render = something.response.render;
something.response.render = function (view, data, callback) {
// Access res.*, as this.*
_render.call(this, view, data, callback);
};
Any ideas how to achieve that?