title: typescript node.js express routes separated files best practices
body: <p>using Express in a Node project along with Typescript what would be the "best practices" for express.Router.</p>
  
  <p>example directory structure</p>
  
  <pre><code>|directory_name
    ---server.js
    |--node_modules
    |--routes
       ---index.ts
       |--admin
          ---admin.ts
       |--products
          ---products.ts
       |--authentication
          ---authentication.ts
  </code></pre>
  
  <p>so inside index.ts it would encapsulate and manage all the sub-routers</p>
  
  <pre><code><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    //admin.ts (nested inside of index.ts)
      import * as express from "express";
  
      export = (() =&gt; {
          
          let router = express.Router();
                
          router.get('/admin', (req, res) =&gt; {
              res.json({success: true});
          });
          
          return router;
      })();</code></pre>
  </div>
  </div>
  
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    //index.ts (master file for express.Router)
  
      import * as express from "express";
  
      //import sub-routers
      import * as adminRouter from "./admin/admin";
      import * as productRouter from "./products/products";
  
      export = (() =&gt; {
  
        let router = express.Router();
  
        // mount express paths, any addition middleware can be added as well.
        // ex. router.use('/pathway', middleware_function, sub-router);
  
        router.use('/products', productRouter);
        router.use('/admin', adminRouter);
  
        //return for revealing module pattern
        return router;
      })(); //&lt;--- this is where I don't understand something....</code></pre>
  </div>
  </div>
  </p>
  
  <p>lastly we would set-up our server.js</p>
  
  <pre><code><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    //the usual node setup
      //import * as *** http, body-parser, morgan, mongoose, express &lt;-- psudocode
  
      import * as masterRouter from './routes/index'
  
      var app = express();
      //set-up all app.use()
  
      app.use('/api', masterRouter);
  
      http.createServer(app).listen(8080, () =&gt; {
            console.log('listening on port 8080')
          };</code></pre>
  </div>
  </div>
  
  </code></pre>
  
  <p>my main question really is, is index.ts (masterRouter file) and it's nested routes that are IIFe's </p>
  
  <p>export = (function(){})(); </p>
  
  <p>should that be the correct/best way to write typescript modules for routers? </p>
  
  <p>or would it be better to use another pattern, perhaps one the utilizes the pattern </p>
  
  <blockquote>
    <p>export function fnName() {} -- export class cName {} -- export default.</p>
  </blockquote>
  
  <p>the reason for the IIFe is that when i import them into another file i won't need to initialize the module and there will only ever be 1 instance of each type of router.</p>