title: Monkey Patch restify response before sending the response
body: <p>I'm trying to apply JSON mask (not related to Friday 13th game or movie) on my <strong>restify</strong> json <strong>response</strong>. </p>
  
  <blockquote>
    <p>The normal way to do it is to call next() in all server routes, then
    implement the mask in the 'pre' handler of restify</p>
  </blockquote>
  
  <p>I can't do it right now, I'm going for a quick fix so I'm trying monkey patch my restify response as follows : </p>
  
  <p>Create a module 'CustomResponse' :</p>
  
  <pre><code>function CustomResponse (originalResponse) {
    this.restifyResponse = originalResponse
    this.type = 'customResponse'
  }
  
  CustomResponse.prototype.send = function (code, payload) {
    if (!payload) { payload = code }
  
    console.log(payload)  
    this.restifyResponse.send(code, payload)
  }
  
  module.exports = CustomResponse
  </code></pre>
  
  <p>I'm trying to use this module in a <strong>middleware</strong> but it doesn't work : </p>
  
  <pre><code>  var CustomResponse = require('./customResponse') // the file above
  
    server.use(function (req, response, next) {
      response = new customResponse(response)
      next()
    })
  </code></pre>
  
  <p>This works only when I use it in a <strong>route</strong> : </p>
  
  <pre><code> var CustomResponse = require('./customResponse') // the file above
  
   server.get({
     path: '/foo/bar'
   }, function(request, response, next){
       response = new CustomResponse(response)
       response.send('baz') // this will print on the console the response and send it to the browser
   })
  </code></pre>
  
  <p>Thanks for any hint !</p>