title: How to debug express router
body: <p>I'm trying to add a module to a huge project, based on another module from the same project.</p>
  
  <p>The client side, in angular, requests an object via</p>
  
  <pre><code>$http.get('/foo/var');
  </code></pre>
  
  <p>And the server is registering the routing this way:</p>
  
  <pre><code>var express = require('express');
  module.exports = function(dependencies) {
      var controller = require('./controller')(dependencies);
      var router = express.Router();
      router.get('/foo/var', controller.getInbox);
  
      return router;
  };
  </code></pre>
  
  <p>Now, this is not working (requesting <a href="http://ulr:port/foo/var" rel="nofollow noreferrer">http://ulr:port/foo/var</a> returns a 404). I'm not asking why does this not work, but why can't I debug it like:</p>
  
  <pre><code>var logger = dependencies('logger');//Works OK
  var controller = require('./controller')(dependencies);
  
  logger.debug('This string is being printed when registering this module');
  
  var router = express.Router();
  router.get('/foo/var', controller.getInbox);
  
  logger.debug(router);//This makes the server crash
  return router;
  </code></pre>
  
  <p>So, whenever I try to log the router (both in my dysfunctional router, and the working one I'm basing mine on) the server refuses to launch by throwing this exceptions</p>
  
  <pre><code>error: Fatal error: TypeError: Cannot read property 'method' of null
      at Function.proto.handle (/path/to/my/project/node_modules/express/lib/router/index.js:114:33)
      at router (/path/to/my/project/node_modules/express/lib/router/index.js:24:12)
      at cb (/path/to/my/project/node_modules/winston/lib/winston/logger.js:206:7)
      at done (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:132:19)
      at /path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:32:16
      at emit (/path/to/my/project/node_modules/winston/lib/winston/logger.js:196:7)
      at /path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:122:13
      at _each (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:46:13)
      at Object.async.each (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:121:9)
      at Logger.log (/path/to/my/project/node_modules/winston/lib/winston/logger.js:214:9)
  error: TypeError: Cannot read property 'method' of null
      at Function.proto.handle (/path/to/my/project/node_modules/express/lib/router/index.js:114:33)
      at router (/path/to/my/project/node_modules/express/lib/router/index.js:24:12)
      at cb (/path/to/my/project/node_modules/winston/lib/winston/logger.js:206:7)
      at done (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:132:19)
      at /path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:32:16
      at emit (/path/to/my/project/node_modules/winston/lib/winston/logger.js:196:7)
      at /path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:122:13
      at _each (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:46:13)
      at Object.async.each (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:121:9)
      at Logger.log (/path/to/my/project/node_modules/winston/lib/winston/logger.js:214:9)
  </code></pre>
  
  <p>Despite it would be nice to know what am I doing wrong, I'd rather prefer to know what's the proper way to debug this issue</p>
  
  <p>Debugging the module using <a href="https://stackoverflow.com/a/31965124/1430607">@robertklep's indications</a></p>
  
  <p>What I can see in the debug is:</p>
  
  <pre><code>Wed, 12 Aug 2015 16:22:46 GMT express:router:route new /foo/var
  Wed, 12 Aug 2015 16:22:46 GMT express:router:layer new /foo/var
  Wed, 12 Aug 2015 16:22:46 GMT express:router:route get /foo/var
  </code></pre>
  
  <p>So it looks like that's the endpoint I want my thing to listen, but it's still not working, I wonder what's layer and route thing, and why route is repeated, though.</p>
  
  <p>I found this module <a href="https://www.npmjs.com/package/express-list-routes" rel="nofollow noreferrer">https://www.npmjs.com/package/express-list-routes</a> and I'm testing it.</p>
  
  <h2>[EDIT] Controller code</h2>
  
  <pre><code>module.exports = function(dependencies) {
      function getInbox() {
          return [
          { date:'31/08/1986', from:"Moi même", to:"Myself", subject:"about"},
          { date:'01/01/1712', from:"Bill Murray", to:"Hulla Kaboolla", subject:"about"}
          ];
      }
  
      return {
          getInbox: getInbox,
      };
  };
  </code></pre>