title: Override res.write in NodeJS
body: <p>I've been playing with NodeJS for a while now, and I've really been enjoying it, but I've run myself into a wall though...</p>
  
  <p>I'm trying to create a connect module to intercept http.ServerResponse methods.  My end goal is to allow the user to apply some kind of filter to outgoing data.  For example, they would have the option to apply compression or something before the data goes out.</p>
  
  <p>I am having this weird bug though... this method is getting called twice as often as it should be.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>var http = require('http'), orig;
  
  orig = http.ServerResponse.prototype.write;
  
  function newWrite (chunk) {
      console.log("Called");
  
      orig.call(this, chunk);
  }
  
  http.ServerResponse.prototype.write = newWrite;
  
  http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.write("Hello");
      res.write(" World");
      res.end();
      console.log("Done");
  }).listen(12345);
  </code></pre>
  
  <p>This works and I get 'Hello World' as output when I access this using a browser, but I'm getting 4 'Called' to the console, and 'Done' gets output twice.  This leads me to believe that for some reason, my server code is getting called twice.  I did a console.log in my newWrite method on this.constructor, and the constructor in both instances is ServerResponse, so that doesn't help much.</p>
  
  <p>I'm really confused about what is going on here.  What could possibly be going on??  This doesn't directly affect the output, but I could potentially be serving gigabytes of compressed data to many clients simultaneously, and doing everything twice will put undue strain on my server.</p>
  
  <p>This is going to be part of a larger fileserver, hence the emphasis on not doing everything twice.</p>
  
  <p>Edit:</p>
  
  <p>I've already read this question in case you're wondering:</p>
  
  <p><a href="https://stackoverflow.com/questions/4415346/can-i-use-an-http-serverresponse-as-a-prototype-in-node-js">Can I use an http.ServerResponse as a prototype in node.js?</a></p>