title: ExpressJS - router module with parameters
body: <p>I've been toying with NodeJS and Express for a few days, and it is pretty cool. But I've run into an issue I cannot get my head around, so I'm hoping someone else out there can help me clear this up.</p>
  
  <p>I know that I can declare a module, and then pass parameters into this module via the <code>require</code> method.</p>
  
  <pre><code>--- App.js ----
  var foo = require('./modules/Foo.js');
      foo.config();
  var bar= {}
      require('./modules/Bar.js)(bar, foo)
  
  --- Bar.js ---
  module.exports = function(Bar, Foo) {
       Bar.act = function(){
            Foo.do();
       }
  }
  </code></pre>
  
  <p>I think this is a much cleaner design than having to require <code>Foo</code> in every module that would require using it. I also don't have to do initialization in each module that requires it.</p>
  
  <p>But then comes the <code>Router</code>, and I can't really understand how it works. Most examples out there requires the router in each module, something like this:</p>
  
  <pre><code>--- App.js ----
  var index = require('./views/index.js');
  app.use('/', index);
  
  --- Index.js ---
  var express = require('express');
  var router = express.Router();
  var foo = require('../modules/Foo.js);
  
  foo.config();
  
  router.get('/', function (req, res) {
    foo.do();
    res.render('index');
  })
  
  module.exports = router
  </code></pre>
  
  <p>But then, I need to require all modules used in <code>index.js</code> manually. What I would really want to do is something like this:</p>
  
  <pre><code>--- App.js ----
  var foo = require('../modules/Foo.js);
      foo.config();
  var index = require('./views/index.js')(foo);
  app.use('/', index);
  
  --- Index.js ---
  var express = require('express');
  var router = express.Router();
  
  module.exports = function(foo){
      router.get('/', function (req, res) {
          foo.do();
          res.render('index');
      })
  }
  </code></pre>
  
  <p>But writing it this way, the compiler tells me "Router.use() requires middleware, got a undefined". I can sorta see where this error comes from, (the <code>module.exports</code> does not return an object) but so how do I structure my code so that I can pass parameters to a module that in the end will export a Router middleware? I want to require and initialize <code>Foo</code> in one place, then pass it to all router modules that uses it. Is it possible somehow?</p>