I'm building a Node.js app with Connect/Express.js and I want to intercept the res.render(view, option) function to run some code before forwarding it on to the original render function.
app.get('/someUrl', function(req, res) {
res.render = function(view, options, callback) {
view = 'testViews/' + view;
res.prototype.render(view, options, callback);
};
res.render('index', { title: 'Hello world' });
});
It looks like a contrived example, but it does fit in an overall framework I'm building.
My knowledge of OOP and Prototypal inheritance on JavaScript is a bit weak. How would I do something like this?
After some experimentation I came up with the following:
app.get('/someUrl', function(req, res) {
var response = {};
response.prototype = res;
response.render = function(view, opts, fn, parent, sub){
view = 'testViews/' + view;
this.prototype.render(view, opts, fn, parent, sub);
};
response.render('index', { title: 'Hello world' });
});
It seems to work. Not sure if it's the best solution as I'm creating a new response wrapper object for each request, would that be a problem?