title: 404 error when Routing using Express and Nodejs
body: <p>I have created a basic Node/Express App and am trying to implement routes based on separation of logic in different files.</p>
  
  <p><strong>In Server.js</strong></p>
  
  <pre><code>var app = express();
  
  var router = express.Router();
  require('./app/routes/users')(router);
  require('./app/routes/events')(router);
  require('./app/routes/subscribe')(router);
  require('./app/routes/login')(router);
  app.use('/api',router);
  </code></pre>
  
  <p><strong>In ./app/routes/users.js</strong></p>
  
  <pre><code>module.exports = function(router){
  
      router.route('/users/')
          .all(function(req, res, next) {
            // runs for all HTTP verbs first
            // think of it as route specific middleware!
          })
          .get(function(req, res, next) {
            res.json(req.user);
          })
          .put(function(req, res, next) {
            // just an example of maybe updating the user
            req.user.name = req.params.name;
            // save user ... etc
            res.json(req.user);
          })
          .post(function(req, res, next) {
            next(new Error('not implemented'));
          })
          .delete(function(req, res, next) {
            next(new Error('not implemented'));
          })
  
      router.route('/users/:user_id')
          .all(function(req, res, next) {
            // runs for all HTTP verbs first
            // think of it as route specific middleware!
          })
          .get(function(req, res, next) {
            res.json(req.user);
          })
          .put(function(req, res, next) {
            // just an example of maybe updating the user
            req.user.name = req.params.name;
            // save user ... etc
            res.json(req.user);
          })
          .post(function(req, res, next) {
            next(new Error('not implemented'));
          })
          .delete(function(req, res, next) {
            next(new Error('not implemented'));
          })
   }
  </code></pre>
  
  <p>All of the routes are returning 404-Not Found.</p>
  
  <p>Does anyone have suggestions on the best way to implement modular routing in Express Apps ?</p>
  
  <p>Is it possible to load multiple routes in a single Instance of express.Router() ?</p>
  
  <p><strong>------------Edit---------------</strong></p>
  
  <p><strong>On Further Testing</strong></p>
  
  <p>I've been able to debug the express.Router() local instance, and the routing layer stack in the local "router" variable is being updated with the routes from the individual modules. </p>
  
  <p>The last line:</p>
  
  <pre><code>app.use('/api', router);
  </code></pre>
  
  <p>is also successfully updating the global app instance internal app.router object with the correct routing layers from the local router instance passed to it.</p>
  
  <p>I think the issue is that the Routes for the '/api' are at number 13-14 in the routing layer stack so there is some issue further up the stack with some other middleware routing not letting the routes through to the end... I just need to track this down I guess. </p>