title: node.js export variables across files
body: <p>I'm relatively new to web development, brand new to node.js. </p>
  
  <p>I'm trying to use express to make a site that updates an api with the <a href="https://github.com/sgentle/chef-node" rel="nofollow">chef-node api</a> client, using <a href="http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/" rel="nofollow">this tutorial</a></p>
  
  <p>If I create a stand alone node.js app, it works as expected, and the value 'bacon' is set to 'good'</p>
  
  <p>app1.js</p>
  
  <pre><code>var fs = require('fs'),
      chef = require('chef'),
      key = fs.readFileSync('/Users/foo.pem'),
      chef_client = chef.createClient('foo', key, 'https://chef.example.com/organizations/dev');
  
  var mybreakfast = { "id":"food","bacon":"good"}
  chef_client.put('/data/breakfast/food', mybreakfast,  function(err,res,body) {
    if (err) { return console.log(err); }
    console.log(body)
  });
  </code></pre>
  
  <p><a href="https://gist.github.com/spuder/1601ab806ec6ef42d34c" rel="nofollow">Full code is here</a>.
  The app basically looks like this:<br>
  app.js</p>
  
  <pre><code>var fs = require('fs'),
      chef = require('chef'),
      key = fs.readFileSync('/Users/foo.pem'),
      chef_client = chef.createClient('foo', key, 'https://chef.example.com/organizations/dev');
   ...
   //tutorial says this isn't ideal, but it is quickest way to get working
   app.use(function(req,res,next){
       req.client = chef_client;
       next();
   });
  </code></pre>
  
  <p>routes/index.js</p>
  
  <pre><code>var express = require('express');
  
  var router = express.Router();
  
  /* GET home page. */
  router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
  });
  
  router.get('/databags', function(req, res) {
    req.client.get('/data/breakfast/food',  function(err,res,body) {
      if (err) { return console.log(err); }
      console.log("Found breakfast")
      console.log(body)
      bag_data = body; //TODO: Global variable is bad and you should feel bad, how do I use correct variable?
    });
    res.render('databags.jade', { title: 'Databags', somedata: bag_data });
  });
  
  router.post('/databags', function(req, res) {
    var mybreakfast = { "id":"food","bacon":"good"}
    req.client.put('/data/breakfast/food', mybreakfast,  function(err,res,body) {
      if (err) { return console.log(err); }
      console.log(body)
    });
  
  });
  </code></pre>
  
  <p>vies/databags.jade</p>
  
  <pre><code>html
    body
      form(action='/databags', id='derp', method='POST')
        each value, key in somedata
          label #{key}  
          input(type='text',name="#{key}", value="#{value}")
          br
          br
        input(type='submit', value='Submit', form='derp')
  </code></pre>
  
  <p>When I press the submit button in /databags, I get a 301 and no data is uploaded. </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;title&gt;301 Moved Permanently&lt;/title&gt;&lt;/head&gt;
  &lt;body bgcolor="white"&gt;
  &lt;center&gt;&lt;h1&gt;301 Moved Permanently&lt;/h1&gt;&lt;/center&gt;
  &lt;hr&gt;&lt;center&gt;openresty/1.7.10.1&lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I suspect the problem has to do with the fact that I'm adding client to every request. </p>
  
  <pre><code> //tutorial says this isn't ideal, but it is quickest way to get working
   app.use(function(req,res,next){
       req.client = chef_client;
       next();
   });
  </code></pre>
  
  <p>What is the proper way to make the chef_client variable in app.js available in routes/index.js?</p>
  
  <p>If that is the correct way to do this, then what could be making the app work by itself, but not when used with the express framework? </p>
  
  <p>Since this is my first node.js / express site, any other suggestions to get this app working would be very appreciated.  </p>
  
  <p><strong>Update</strong></p>
  
  <p>Solution has been found, working code snippet available here: <a href="https://gist.github.com/spuder/1e39868b6a9a0c3cdb13" rel="nofollow">https://gist.github.com/spuder/1e39868b6a9a0c3cdb13</a></p>