title: Node.js / Express.js - How to override/intercept res.render function?
body: <p>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.</p>
  
  <pre><code>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' });
  });
  </code></pre>
  
  <p>It looks like a contrived example, but it does fit in an overall framework I'm building.</p>
  
  <p>My knowledge of OOP and Prototypal inheritance on JavaScript is a bit weak. How would I do something like this?</p>
  
  <hr>
  
  <p><strong>Update:</strong> After some experimentation I came up with the following:</p>
  
  <pre><code>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' });
  });
  </code></pre>
  
  <p>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?</p>