{"componentChunkName":"component---src-templates-post-js","path":"/pensieve/docker-for-nodejs-3cjg","result":{"data":{"markdownRemark":{"html":"<h2><img src=\"https://miro.medium.com/max/1200/1*EKW3XazCN98jcVrlEP3H8g.png\" alt=\"Draft fail\"></h2>\n<p>If you are using express framework for developing a web application , you might be familiar with the concept of routing and how it plays an important role in your app. Routing refers to how an application’s endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing.\nYou define routing using methods of the Express app object that correspond to HTTP methods;\nFor example, app.get() to handle GET requests and app.post to handle POST requests. For a full list, see app.METHOD. You can also use app.all() to handle all HTTP methods and app.use() to specify middleware as the callback function (See Using middleware for details).\nI like to fragment my routes in separate files and like to require them in the root js file. This not only improves your code readability but also makes debugging alot easier as it is easy to pinpoint the exact location in the exact file instead of going through bulk of codes in a single file.\nTo use your fragmented routes , you have to require them similar to a nodejs module and put it inside app.use . This is simple if your routes folder has a couple of routes but if you are talking about a webapp of a large scale, there may be a dozen or more routes files which means you have to require each files and the app.js file gets populated with the code for reqiring your routes .\nAlso if there are multiple routes , there may be a possibility that you may forget to use a route.\nSo a quick hack would be to use dynamic routing which automatically requires all your routes in routes folder thus reducing the code and\nrequiring the routing easier.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">/**\n * Dyanmic Routing\n * Adds all routes from routes folder\n */</span>\nfs<span class=\"token punctuation\">.</span><span class=\"token function\">readdir</span><span class=\"token punctuation\">(</span><span class=\"token string\">'./routes'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">err<span class=\"token punctuation\">,</span> files</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  files<span class=\"token punctuation\">.</span><span class=\"token function\">forEach</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">file</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    app<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/'</span><span class=\"token punctuation\">,</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'./routes/'</span> <span class=\"token operator\">+</span> file<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Here we have used the filesystem module to read all files from routes folder and using them in instance of express app.</p>","frontmatter":{"title":"Dynamically requiring all routes in express app","description":null,"date":"2019-09-16","slug":"/pensieve/docker-for-nodejs-3cjg","tags":["Nodejs","Webdev","Express"]}}},"pageContext":{}},"staticQueryHashes":["3115057458"]}