{"id":265,"date":"2017-10-26T05:07:49","date_gmt":"2017-10-26T12:07:49","guid":{"rendered":"http:\/\/vinodsr.com\/myblog\/?p=265"},"modified":"2020-04-24T22:04:59","modified_gmt":"2020-04-25T05:04:59","slug":"nodejs-modular-development","status":"publish","type":"post","link":"https:\/\/vinodsr.com\/myblog\/2017\/10\/nodejs-modular-development\/","title":{"rendered":"Build your nodeJS Application in a modular way"},"content":{"rendered":"\n<p><span style=\"font-family: 'Scope One';\"><span style=\"font-size: 28px;\"><strong>N<\/strong><\/span>ow a days almost all web services or integrations are done on top of nodeJS run-time. NodeJS is a flexible platform with lot of community support. Even it is possible to create documents like xlsx, docx or pdf directly from nodeJS. All major cloud platforms uses nodeJS as their level 1 language.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One'; font-size: 20px;\"><strong>Modularity<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">NodeJS by design achieves the modularity by using the node_modules structure. All the required modules are stored in node_modules directory and we can invoke the modules any where in our code.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">But are we using this modularity in our application code. Most of the application I saw contains a lib folder, i which we store all js files . This js files are imported in the required areas using relative paths.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">const db = require(\"..\/db\/\") \nconst logging = require (\"..\/..\/logging\")<\/code><\/pre>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">The main problem with this kind of approach is that when we change the path of a service file the path to the db should change. Also the format is not readable. We will be confused with the file&#8217;s authenticity.<\/span><\/p>\n\n\n\n<p><span style=\"font-size: 20px; font-family: 'Scope One';\"><strong>The Solution<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">A much better approach is to design our application as modules, such as db , logging, error etc . Lets say your application name is cms, then it is much easier to represent the module using scope<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">require(\"@cms\/db\")<\/code><\/pre>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">You can develop the modules separately and publish in to any NPM servers ( public \/ private ) and use it as like any other module.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">If your application needs the logging module<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm i --save @cms\/logging<\/code><\/pre>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">If you don&#8217;t want to split your application in to bits and pieces, there is another approach.<\/span><\/p>\n\n\n\n<p><span style=\"font-size: 20px;\"><strong><span style=\"font-family: 'Scope One';\">A more better way<\/span><\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">Keep the modules that you want inside a separate folder. Lets say &#8220;@cms&#8221;. Use separate folder for each module and let the modules have a separate package.json. This way it will become a valid node module.<\/span><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"191\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/1.png\" alt=\"\" class=\"wp-image-272\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/1.png 600w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/1-300x96.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft\"><img loading=\"lazy\" decoding=\"async\" width=\"481\" height=\"147\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/2.png\" alt=\"\" class=\"wp-image-273\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/2.png 481w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/2-300x92.png 300w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"616\" height=\"122\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/3.png\" alt=\"\" class=\"wp-image-274\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/3.png 616w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/3-300x59.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/figure><\/div>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">  <\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">The package json for the modules will look like this<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n  \"name\": \"@cms\/db\",\n  \"version\": \"1.0.1\",\n  \"description\": \"db module for CMS Application\",\n  \"main\": \"index.js\",\n  \"dependencies\":{\n\t\"mysql\" : \"latest\"\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">Once the modules are ready now its time to do some scripting. Add the install.js in the &#8220;scripts&#8221; folder.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">let fs = require('fs')\n\nconsole.log('Creating symlinks ...')\nif (fs.existsSync('node_modules\/@cms')) {\n    console.log('link exists already ')\n} else {\n    let source = '..\/@cms'\n    console.log(`creating link for ${source}`)\n    fs.symlinkSync(source, 'node_modules\/@cms', 'junction')\n    console.log('done')\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">Add this script to your main package.json.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n  \"name\": \"CMSApplication\",\n  \"version\": \"1.0.1\",\n  \"description\": \"Sample CMS Application\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"install\": \"node scripts\/install.js\",\n    \"start\": \"node index.js\"\n  },\n  \"dependencies\":{\n\t\"express\" : \"latest\"\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">The script will be executed every time you do npm install. So once all other node modules defined in the dependencies are installed, it will create a link from the @cms folder outside to the @cms folder inside node_modules. So any changes you make to the outer @cms folder will be reflected to the folder inside node_modules. <\/span><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"106\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/install.png\" alt=\"\" class=\"wp-image-269\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/install.png 544w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/install-300x58.png 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/figure><\/div>\n\n\n\n<p><span style=\"font-family: 'Scope One';\"><\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">You can see that the symlinks are installed for @cms. This is not a shortcut file rather that hard links created using &#8220;ln&#8221; in linux.<\/span><\/p>\n\n\n\n<div class=\"wp-block-image wp-image-270 size-full\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"455\" height=\"226\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/folder-list.png\" alt=\"node_modules folder\" class=\"wp-image-270\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/folder-list.png 455w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/folder-list-300x149.png 300w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><figcaption><span style=\"font-family: 'Scope One';\">@cms folder is along with other npm modules in the node_modules directory<\/span><\/figcaption><\/figure><\/div>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">Inside @cms you can see our modules which is defined in the outer @cms folder<\/span><\/p>\n\n\n\n<div class=\"wp-block-image size-full wp-image-271\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"152\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/F2.png\" alt=\"\" class=\"wp-image-271\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/F2.png 466w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/10\/F2-300x98.png 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><figcaption><span style=\"font-family: 'Scope One';\">The modules inside @cms scope<\/span><\/figcaption><\/figure><\/div>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">This way you can achieve modularity. &#8220;@cms&#8221; folder is part of your source code. You can then import the required modules in the normal way.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: 'Scope One';\">This approach helps me in making the application more modular and extensible. Let me know your thoughts on this.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now a days almost all web services or integrations are done on top of nodeJS run-time. NodeJS is a flexible platform with lot of community&hellip;<\/p>\n","protected":false},"author":1,"featured_media":267,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[48,65,63,64,47,46],"class_list":["post-265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs","tag-linux","tag-node","tag-nodejs","tag-npm","tag-tips-and-tricks","tag-windows","has-post-thumbnail-archive"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/comments?post=265"}],"version-history":[{"count":11,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/265\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/265\/revisions\/324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/media\/267"}],"wp:attachment":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/categories?post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/tags?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}