title: Is it OK to add data to the response object in a middleware module in Express.js?
body: <p>Here's the basic setup. I'm trying to create a simple middleware component that would allow me to easily pass data from my route directly to my javascript in the client side. (Very similiar to the <a href="https://github.com/gazay/gon" rel="noreferrer">Gon</a> gem in ruby). The way I'm doing it is by having a module that looks like this:</p>
  
  <pre><code>    module.exports = function(){
      return function(req,res,next){
          var app = req.app;
          if(typeof(app) == 'undefined'){
              var err = new Error("The JShare module requires express");
              next(err);
              return;
          }
          res.jshare = {};
          app.dynamicHelpers({
              includeJShare: function(req,res){
                  if(typeof(res.jshare) === 'undefined'){
                      return "";
                  }
                  return function(){
                      return '&lt;script type="text/javascript"&gt;window.jshare=' + JSON.stringify(res.jshare) + '&lt;/script&gt;';
                  } 
              }
          });
          next();
      };
  }
  </code></pre>
  
  <p>Then, in my route I can do this:</p>
  
  <pre><code>exports.index = function(req, res){
    res.jshare.person = {firstName : "Alex"};
    res.render('index', { title: 'Express' })
  };
  </code></pre>
  
  <p>Finally in the layout.jade:</p>
  
  <pre><code>!{includeJShare()}
  </code></pre>
  
  <p>What that does is in outputs a line of javascript on the client that creates the exact JSON object that was created server side. </p>
  
  <p>Here's the question; it all works as expected, but being new to Express and Node.js in general, I was just curious if attaching properties onto the response object is OK, or is there something wrong with doing it that I'm simply overlooking? For some reason it doesn't pass my "smell test" but I'm not sure why..... </p>