title: Accessing Express.js local variables in client side JavaScript
body: <p>Curious if I'm doing this right and if not how you guys would approach this.</p>
  
  <p>I have a Jade template that needs to render some data retrieved from a MongoDB database and I also need to have access to that data inside a client side JavaScript file.</p>
  
  <p>I'm using Express.js and sending the data to the Jade template as follows :</p>
  
  <pre><code>var myMongoDbObject = {name : 'stephen'};
  res.render('home', { locals: { data : myMongoDbObject } });
  </code></pre>
  
  <p>Then inside of <strong>home.jade</strong> I can do things like :</p>
  
  <pre><code>p Hello #{data.name}!
  </code></pre>
  
  <p>Which writes out : </p>
  
  <pre><code>Hello stephen!
  </code></pre>
  
  <p>Now what I want is to also have access to this data object inside a client side JS file so I can manipulate the Object on say a button click before POSTing it back to the server to update the database.</p>
  
  <p>I've been able to accomplish this by saving the "data" object inside a hidden input field in the Jade template and then fetching the value of that field inside my client-side JS file.</p>
  
  <p><strong>Inside home.jade</strong></p>
  
  <pre><code>- local_data = JSON.stringify(data) // data coming in from Express.js
  input(type='hidden', value=local_data)#myLocalDataObj
  </code></pre>
  
  <p>Then in my client side JS file I can access local_data like so :</p>
  
  <p><strong>Inside myLocalFile.js</strong></p>
  
  <pre><code>var localObj = JSON.parse($("#myLocalDataObj").val());
  console.log(localObj.name);
  </code></pre>
  
  <p>However this stringify / parsing business feels messy. I know I can bind the values of my data object to DOM objects in my Jade template and then fetch those values using jQuery, but I'd like to have access to the actual Object that is coming back from Express in my client side JS. </p>
  
  <p>Is my solution optimal, how would you guys accomplish this?</p>