title: express.Router() and requires in Express.js
body: <p>I am trying to organize my project according to Express 4.x new <a href="http://expressjs.com/en/4x/api.html#router" rel="nofollow noreferrer"><code>express.Router()</code></a> method.</p>
  
  <p>As <a href="http://expressjs.com/en/4x/api.html#router" rel="nofollow noreferrer">Express' documentation</a> describes it,</p>
  
  <blockquote>
    <p>A router object is an isolated instance of middleware and routes. You
    can think of it as a “mini-application,” capable only of performing
    middleware and routing functions.</p>
  </blockquote>
  
  <p>For the sake of better understanding, let's consider this project structure:</p>
  
  <pre><code>project/
    index.js
    routes/
      myRouter.js
      ...
    ...
  </code></pre>
  
  <p>And the files themselves:</p>
  
  <p><em>index.js</em></p>
  
  <pre><code>const express = require('express');
  const app = express(); 
  const path = require('path');
  
  const myModule = require('myModule');   
  
  app.use('/myRouter', require('routes/myRouter'));
  
  // some more code using myModule set of functions
  </code></pre>
  
  <p><em>routes/myRouter.js</em></p>
  
  <pre><code>const express = require('express');
  const path = require('path');
  const router = express.Router();
  
  const myModule = require('myModule'); 
  
  router.get('/', function(req, res){
          // some more code using myModule set of functions 
        });
  
  module.exports = router;
  </code></pre>
  
  <p>As you can see, both files need to use <code>myModule</code>'s functions, so AFAIK both files need to require <code>myModule</code>.</p>
  
  <p>How does Express handle this situation?</p>
  
  <p>As I see it, Express directly imports <code>myRouter</code>'s code into <code>index.js</code> via <code>module.exports</code>. If so, is the engine somehow pre-compiling it? And then aren't <code>myRouters' requires</code> redundant?</p>
  
  <p>If not, how does it affect performance? Should I avoid routers for my kind of task?</p>