title: Node.js / Express.js - How does app.router work?
body: <p>Before I ask about <code>app.router</code> I think I should explain at least what I think happens when working with middleware. To use middleware, the function to use is <code>app.use()</code>. When the middleware is being executed, it will either call the next middleware by using <code>next()</code> or make it so no more middleware get called. That means that the order in which I place my middleware calls is important, because some middleware depends on other middleware, and some middleware near the end might not even be called.</p>
  
  <p>Today I was working on my application and had my server running in the background. I wanted to make some changes and refresh my page and see the changes immediately. Specifically, I was making changes to my layout. I couldn't get it to work so I searched Stack&nbsp;Overflow for the answer and found <a href="https://stackoverflow.com/questions/5612777/stylus-and-express-stylesheets-are-not-re-compiled-when-modified">this question</a>. It says to make sure that <code>express.static()</code> is beneath <code>require('stylus')</code>. But when I was looking at that OP's code, I saw that he had his <code>app.router</code> call at the very end of his middleware calls, and I tried to figure out why that was.</p>
  
  <p>When I made my Express.js application (version 3.0.0rc4), I used the command <code>express app --sessions --css stylus</code> and in my app.js file the code came setup with my <code>app.router</code> above both the <code>express.static()</code> and <code>require('stylus')</code> calls. So it seems like, if it comes already setup that way, then it should stay that way.</p>
  
  <p>After re-arranging my code so I could see my Stylus changes, it looks like this:</p>
  
  <pre><code>app.configure(function(){
    //app.set() calls
    //app.use() calls
    //...
    app.use(app.router);
    app.use(require('stylus').middleware(__dirname + '/public'));
    app.use(express.static(__dirname + '/public', {maxAge: 31557600000}));
  });
  
  app.get('/', routes.index);
  
  app.get('/test', function(req, res){
    res.send('Test');
  });
  </code></pre>
  
  <p>So I decided that the first step would be to find out why it is important to even have <code>app.router</code> in my code. So I commented it out, started my app and navigated to <code>/</code>. It displayed my index page just fine. Hmm, maybe it worked because I was exporting the routing from my routes file (routes.index). So next I navigated to <code>/test</code> and it displayed Test on the screen. Haha, OK, I have no idea what <code>app.router</code> does. Whether it is included in my code or not, my routing is fine. So I am definitely missing something.</p>
  
  <p><strong>So Here Is My Question:</strong></p>
  
  <p>Could somebody please explain what <code>app.router</code> does, the importance of it, and where I should place it in my middleware calls? It would also be nice if I got a brief explanation about <code>express.static()</code>. As far as I can tell, <code>express.static()</code> is a cache of my information, and if the application can't find the requested page, it will check the cache to see if it exists.</p>