<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>manifest &amp;mdash; Simple Engineering</title>
    <link>https://getsimple.works/tag:manifest</link>
    <description></description>
    <pubDate>Tue, 28 Apr 2026 15:04:37 +0000</pubDate>
    <item>
      <title>Modularizing nodejs applications ~ Manifest routes</title>
      <link>https://getsimple.works/modularizing-nodejs-applications-manifest-routes?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[We assume most of the system components to be accessible for testability. However, that is challenging when routes are a little bit complex. To reduce the complexity that comes with working on large-scale expressjs routes, we will apply a technique known as manifest routes to make route declarations change proof, making them more stable as the rest of the application evolves.&#xA;&#xA;In this article we will talk about:&#xA;&#xA;The need to have manifest routes technique &#xA;How to apply the manifest routes as a modularization technique&#xA;&#xA;  Even though this blog post was designed to offer complementary materials to those who bought my Testing nodejs Applications book, the content can help any software developer to tuneup working environment. You use this link to buy the book.  Testing nodejs Applications Book Cover&#xA;&#xA;Show me the code&#xA;&#xA;var express = require(&#39;express&#39;)&#xA;var app = express();&#xA;&#xA;app.get(&#39;/&#39;, function(req, res, next) {  &#xA;  res.render(&#39;index&#39;, { title: &#39;Express&#39; });&#xA;});&#xA;&#xA;/* code that initialize everything, then comes this route/&#xA;app.get(&#39;/users/:id&#39;, function(req, res, next){&#xA;  User.findById(req.params.id, function(error, user){&#xA;    if(error) return next(error);&#xA;    return res.status(200).json(user);&#xA;  });&#xA;});&#xA;&#xA;app.listen(port, function () {&#xA;  console.log(&#39;Example app listening on port 3000!&#39;)&#xA;});&#xA;&#xA;What can possibly go wrong?&#xA;&#xA;When trying to figure out how to approach modularization of expressjs routes with a manifest route pattern, the following points may be a challenge:&#xA;&#xA;Where to start with modularization without breaking the rest of the application&#xA;How to introduce the layered architecture, without incurring additional test burden, but making it easier to isolate tests&#xA;&#xA;The following sections will explore more on making points stated above work. &#xA;&#xA;The need to have manifest routes technique&#xA;&#xA;There is a subtle nuance that is missing when following traditional approaches to modularization. &#xA;&#xA;When adding an index file, as a part of the modularization process, exporting the content of directories, for that matter -- sub-directories, does not result in exporting routes that can be plugged into existing expressjs applications. &#xA;&#xA;The remedy is to create, isolate, export, and manifest them to the outer world.   &#xA;&#xA;How to apply the manifest routes handlers for reusability&#xA;&#xA;The handlers are a beast in their own way. &#xA;&#xA;A collection of related route handlers can be used as a baseline to create the controller layer. The modularization of this newly created/revealed layer can be achieved in two steps as was the case for other use cases. The first step consists of naming, ejecting, and exporting single functions as modules. The second step consists of adding an index to every directory and exporting the content of the directory.&#xA;&#xA;Manifest routes&#xA;&#xA;In essence, requiring a top-level directory, will seek for index.js at top of the directory and make all the route content accessible to the caller. &#xA;&#xA;var routes = require(&#39;./routes&#39;); &#xA;Example: /routes has index.js at top level directory ~ source&#xA;&#xA;A typical default entry point of the application: &#xA;&#xA;var express = require(&#39;express&#39;);  &#xA;var router = express.Router();&#xA;&#xA;router.get(&#39;/&#39;, function(req, res, next) {  &#xA;  return res.render(&#39;index&#39;, { title: &#39;Express&#39; });&#xA;});&#xA;module.exports = router;  &#xA;Example: default /index entry point&#xA;&#xA;Anatomy of a route handler &#xA;&#xA;module.exports = function (req, res) {  };&#xA;Example: routes/users/get-user|new-user|delete-user.js&#xA;&#xA;  &#34;The most elegant configuration that I&#39;ve found is to turn the larger routes with lots of sub-routes into a directory instead of a single route file&#34; - Chev  source&#xA;&#xA;When individual routes/users sub-directories are put together, the resulting index would look as in the following code sample &#xA;&#xA;var router = require(&#39;express&#39;).Router();  &#xA;router.get(&#39;/get/:id&#39;, require(&#39;./get-user.js&#39;));  &#xA;router.post(&#39;/new&#39;, require(&#39;./new-user.js&#39;));  &#xA;router.post(&#39;/delete/:id&#39;, require(&#39;./delete-user.js&#39;));  &#xA;module.exports = router;    &#xA;Example: routes/users/index.js&#xA;&#xA;Update when routes/users/favorites/ adds more sub-directories&#xA;&#xA;router.use(&#39;/favorites&#39;, require(&#39;./favorites&#39;)); &#xA;...&#xA;module.exports = router;&#xA;Example: routes/users/index.js ~ after adding a new favorites requirement&#xA;&#xA;We can go extra mile and group route handlers in controllers. Using route and controllers&#39; route handler as a controller would look as in the following example:&#xA;&#xA;var router = require(&#39;express&#39;).Router();&#xA;var catalogues = require(&#39;./controllers/catalogues&#39;);&#xA;&#xA;router.route(&#39;/catalogues&#39;)&#xA;  .get(catalogues.getItem)&#xA;  .post(catalogues.createItem);&#xA;module.exports = router;&#xA;&#xA;Conclusion &#xA;&#xA;Modularization makes expressjs routes reusable, composable, and stable as the rest of the system evolves. Modularization brings elegance to route composition, improved testability, and reduces instances of redundancy. &#xA;&#xA;In this article, we revisited a technique that improves expressjs routes elegance, their testability, and re-usability known under the manifest route moniker. We also re-state that the manifest route technique is an extra mile to modularizing expressjs routes. There are additional complimentary materials in the &#34;Testing nodejs applications&#34; book. &#xA;&#xA;References &#xA;&#xA;Testing nodejs Applications book&#xA;An Intuitive Way To Organize Your expressjs Routes ~ CodeTunnel&#xA;&#xA;#snippets #modularization #manifest-routes #nodejs #expressjs]]&gt;</description>
      <content:encoded><![CDATA[<p>We assume most of the system components to be accessible for testability. However, that is challenging when routes are a little bit complex. To reduce the complexity that comes with working on large-scale <code>expressjs</code> routes, we will apply a technique known as <em>manifest routes</em> to make route declarations change proof, making them more stable as the rest of the application evolves.</p>

<p><strong><em>In this article we will talk about:</em></strong></p>
<ul><li>The need to have manifest routes technique</li>
<li>How to apply the manifest routes as a modularization technique</li></ul>

<blockquote><p>Even though this blog post was designed to offer complementary materials to those who bought my <strong><em><a href="https://bit.ly/2ZFJytb">Testing <code>nodejs</code> Applications book</a></em></strong>, the content can help any software developer to tuneup working environment. <strong><em><a href="https://bit.ly/2ZFJytb">You use this link to buy the book</a></em></strong>.  <a href="https://bit.ly/2ZFJytb"><img src="https://snap.as/a/42OS2vs.png" alt="Testing nodejs Applications Book Cover"/></a></p></blockquote>

<h2 id="show-me-the-code" id="show-me-the-code">Show me the code</h2>

<pre><code class="language-JavaScript">var express = require(&#39;express&#39;)
var app = express();

app.get(&#39;/&#39;, function(req, res, next) {  
  res.render(&#39;index&#39;, { title: &#39;Express&#39; });
});

/** code that initialize everything, then comes this route*/
app.get(&#39;/users/:id&#39;, function(req, res, next){
  User.findById(req.params.id, function(error, user){
    if(error) return next(error);
    return res.status(200).json(user);
  });
});

app.listen(port, function () {
  console.log(&#39;Example app listening on port 3000!&#39;)
});

</code></pre>

<h2 id="what-can-possibly-go-wrong" id="what-can-possibly-go-wrong">What can possibly go wrong?</h2>

<p>When trying to figure out how to approach modularization of <code>expressjs</code> routes with a manifest route pattern, the following points may be a challenge:</p>
<ul><li>Where to start with modularization without breaking the rest of the application</li>
<li>How to introduce the layered architecture, without incurring additional test burden, but making it easier to isolate tests</li></ul>

<p>The following sections will explore more on making points stated above work.</p>

<h2 id="the-need-to-have-manifest-routes-technique" id="the-need-to-have-manifest-routes-technique">The need to have manifest routes technique</h2>

<p>There is a subtle nuance that is missing when following traditional approaches to modularization.</p>

<p>When adding an <code>index</code> file, as a part of the modularization process, exporting the content of directories, for that matter — sub-directories, does not result in exporting routes that can be plugged into existing <code>expressjs</code> applications.</p>

<p>The remedy is to create, isolate, export, and manifest them to the outer world.</p>

<h2 id="how-to-apply-the-manifest-routes-handlers-for-reusability" id="how-to-apply-the-manifest-routes-handlers-for-reusability">How to apply the manifest routes handlers for reusability</h2>

<p>The handlers are a beast in their own way.</p>

<p>A collection of related route handlers can be used as a baseline to create the controller layer. The modularization of this newly created/revealed layer can be achieved in two steps as was the case for other use cases. The first step consists of naming, ejecting, and exporting single functions as modules. The second step consists of adding an <code>index</code> to every directory and exporting the content of the directory.</p>

<h2 id="manifest-routes" id="manifest-routes">Manifest routes</h2>

<p>In essence, requiring a top-level directory, will seek for <code>index.js</code> at top of the directory and make all the route content accessible to the caller.</p>

<pre><code class="language-JavaScript">var routes = require(&#39;./routes&#39;); 
</code></pre>

<p><em><em>Example</em>: <code>/routes</code> has <code>index.js</code> at top level directory ~ <a href="http://stackoverflow.com/a/5365577/132610">source</a></em></p>

<p>A typical default entry point of the application:</p>

<pre><code class="language-JavaScript">var express = require(&#39;express&#39;);  
var router = express.Router();

router.get(&#39;/&#39;, function(req, res, next) {  
  return res.render(&#39;index&#39;, { title: &#39;Express&#39; });
});
module.exports = router;  
</code></pre>

<p><em><em>Example</em>: default <code>/index</code> entry point</em></p>

<p>Anatomy of a route handler</p>

<pre><code class="language-JavaScript">module.exports = function (req, res) {  };
</code></pre>

<p><em><em>Example</em>: <code>routes/users/get-user|new-user|delete-user.js</code></em></p>

<blockquote><p><em>“The most elegant configuration that I&#39;ve found is to turn the larger routes with lots of sub-routes into a directory instead of a single route file”</em> – Chev  <a href="https://codetunnel.io/an-intuitive-way-to-organize-your-expressjs-routes/">source</a></p></blockquote>

<p>When individual routes/users sub-directories are put together, the resulting index would look as in the following code sample</p>

<pre><code class="language-JavaScript">var router = require(&#39;express&#39;).Router();  
router.get(&#39;/get/:id&#39;, require(&#39;./get-user.js&#39;));  
router.post(&#39;/new&#39;, require(&#39;./new-user.js&#39;));  
router.post(&#39;/delete/:id&#39;, require(&#39;./delete-user.js&#39;));  
module.exports = router;    
</code></pre>

<p><em><em>Example</em>: <code>routes/users/index.js</code></em></p>

<p>Update when <code>routes/users/favorites/</code> adds more sub-directories</p>

<pre><code class="language-JavaScript">router.use(&#39;/favorites&#39;, require(&#39;./favorites&#39;)); 
...
module.exports = router;
</code></pre>

<p><em><em>Example</em>: <code>routes/users/index.js</code> ~ after adding a new favorites requirement</em></p>

<p>We can go extra mile and group route handlers in controllers. Using route and controllers&#39; route handler as a controller would look as in the following example:</p>

<pre><code class="language-JavaScript">var router = require(&#39;express&#39;).Router();
var catalogues = require(&#39;./controllers/catalogues&#39;);

router.route(&#39;/catalogues&#39;)
  .get(catalogues.getItem)
  .post(catalogues.createItem);
module.exports = router;
</code></pre>

<h2 id="conclusion" id="conclusion">Conclusion</h2>

<p>Modularization makes <code>expressjs</code> routes reusable, composable, and stable as the rest of the system evolves. Modularization brings elegance to route composition, improved testability, and reduces instances of redundancy.</p>

<p>In this article, we revisited a technique that improves <code>expressjs</code> routes elegance, their testability, and re-usability known under the manifest route moniker. We also re-state that the manifest route technique is an extra mile to modularizing <code>expressjs</code> routes. There are additional complimentary materials in the <strong>“Testing <code>nodejs</code> applications”</strong> book.</p>

<h2 id="references" id="references">References</h2>
<ul><li><a href="https://bit.ly/2ZFJytb">Testing <code>nodejs</code> Applications book</a></li>
<li>An Intuitive Way To Organize Your <code>expressjs</code> Routes ~ <a href="https://codetunnel.io/an-intuitive-way-to-organize-your-expressjs-routes/">CodeTunnel</a></li></ul>

<p><a href="https://getsimple.works/tag:snippets" class="hashtag"><span>#</span><span class="p-category">snippets</span></a> <a href="https://getsimple.works/tag:modularization" class="hashtag"><span>#</span><span class="p-category">modularization</span></a> <a href="https://getsimple.works/tag:manifest" class="hashtag"><span>#</span><span class="p-category">manifest</span></a>-routes <a href="https://getsimple.works/tag:nodejs" class="hashtag"><span>#</span><span class="p-category">nodejs</span></a> <a href="https://getsimple.works/tag:expressjs" class="hashtag"><span>#</span><span class="p-category">expressjs</span></a></p>
]]></content:encoded>
      <guid>https://getsimple.works/modularizing-nodejs-applications-manifest-routes</guid>
      <pubDate>Thu, 17 Jun 2021 05:46:06 +0000</pubDate>
    </item>
  </channel>
</rss>