title: NodeJS Express defining custom routes
body: <p>This is probably a really basic concept that I'm not understanding but in my NodeJS application I am trying to define a custom route.</p>
  
  <p>my directory structure is as follows</p>
  
  <pre><code>/application
      /app.js
      /package.json
      /node_modules
      /public
      /routes
          /control
              /users.js
      /views
          /control
              /users.ejs
  </code></pre>
  
  <p>Which I am happy with because I want to keep the routes and views in a 1 to 1 relationship because I will eventually end up with something like</p>
  
  <pre><code>/application
      /app.js
      /package.json
      /node_modules
      /public
      /routes
          /control
              /users.js
              /system.js
          /tools
              /stock.js
              /report.js
      /views
          /control
              /users.ejs
              /system.ejs
          /tools
              /stock.ejs
              /report.ejs
  </code></pre>
  
  <p>So I don't want to end up with a /routes/index.js file with a hideous amount of routing code inside.</p>
  
  <p>It seems to work while my app.js file is as follows</p>
  
  <pre><code>//==============================================================================
  // setup
  //==============================================================================
  
  var express = require("express");
  var path = require("path");
  
  var app = express();
  var port = 3000;
  var message = null;
  
  app.set("view engine", "ejs");
  
  app.use(express.static(path.join(__dirname, "public")));
  
  //==============================================================================
  // routes
  //==============================================================================
  
  var users = require("./routes/control/users");
  app.get("/", users.users);
  
  //==============================================================================
  // start server
  //==============================================================================
  
  app.listen(port, function() {
  
     message = "Server Started : Port " + port;
     console.log(message);
  
  });
  </code></pre>
  
  <p>Although I can see this is going to end up looking like</p>
  
  <pre><code>//==============================================================================
  // setup
  //==============================================================================
  
  var express = require("express");
  var path = require("path");
  
  var app = express();
  var port = 3000;
  var message = null;
  
  app.set("view engine", "ejs");
  
  app.use(express.static(path.join(__dirname, "public")));
  
  //==============================================================================
  // routes
  //==============================================================================
  
  // control
  
  var users = require("./routes/control/users");
  app.get("/", users.users);
  
  var system = require("./routes/control/system");
  app.get("/", system.system);
  
  // tools
  
  var stock = require("./routes/tools/stock");
  app.get("/", stock.stock);
  
  var report = require("./routes/tools/report");
  app.get("/", report.report);
  
  //==============================================================================
  // start server
  //==============================================================================
  
  app.listen(port, function() {
  
     message = "Server Started : Port " + port;
     console.log(message);
  
  });
  </code></pre>
  
  <p>So I don't really want to have that many requires but doing it like the following doesn't seem to work and I'm not sure why</p>
  
  <pre><code>// control
  
  var control = require("./routes/control");
  
  app.get("/", control.users.users);
  app.get("/", control.system.system);
  
  // tools
  
  var tools = require("./routes/tools");
  
  app.get("/", tools.stock.stock);
  app.get("/", tools.report.report);
  </code></pre>