{"componentChunkName":"component---src-pages-pensieve-index-js","path":"/pensieve/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"frontmatter":{"title":"Vim quickies/ cheatsheet","description":null,"slug":"/pensieve/vim-cheatsheet","date":"2019-11-24","tags":["Vim","Editor","Linux"],"draft":false},"html":"<p><img src=\"https://www.ubuntupit.com/wp-content/uploads/2019/03/Vim-Commands.jpg\" alt=\"Draft fail\"></p>\n<h2>Generally helpful stuff</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Open a file for editing             :e path/to/file.txt\nReturn to Normal mode               ESC   or &lt;CTRL>+C</code></pre></div>\n<h2>Navigating around text</h2>\n<p>You have to be in Normal mode. Use ESC to get out of Visual, Replace, or Insert mode.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">(left)                       h\n(down)                       j\n(up)                         k\n(right)                      l\nnext word                    e\nJump to the first line       gg\nJump to the last line        G</code></pre></div>\n<h2>Entering Text</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Insert text before cursor               i\nInsert text after cursor                a</code></pre></div>\n<h2>Working with multiple files</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Open a file in a horizontal split   :sp path/to/file.txt\nOpen a file in a vertical split     :vsp path/to/file.txt\nMove to a split window page         &lt;CTRL>+w and a direction key (h, j, k, or l)\nMove to next window pane            &lt;CTRL>w w\nMake selected pane bigger           CTRL>w +  (yes, you need the shift key for the plus)\nMake selected pane smaller          &lt;CTRL>w -</code></pre></div>\n<h2>Searching</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Search for a word                           /&lt;word>\nGo to next match                            n\nFind and replace on line                    :s/&lt;find>/&lt;replace>\nFind and replace globally                   :%s/&lt;find>/&lt;replace>//gc\nGo to first quote, replace text in quotes:  ci\"</code></pre></div>\n<h2>Manipulating text</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">cut the current line                dd\ncopy the current line               yy\npaste below current line            p\npaste above current line            P\nRemove the character under cursor   x\nRemove the character before cursor  X\nDelete the word under cursor        de\nDelete to the end of the line       d$\n\nRemove five lines starting here     5dd\nCopy five lines starting here       5yy\n\nindent this line                    >>\nindent five lines starting here     5>>\n\nReplace mode (overtype)             r</code></pre></div>\n<h2>Visual Advanced selection</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Visual mode                         v\nVisual Line mode                    V\nVisual Block mode                   &lt;CTRL>v</code></pre></div>\n<h2>Working with NERDTree</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Open the NERDTree                   :NERDTree\nToggle the NERDTree on and off      :NERDTreeToggle\nOpen selected file                  &lt;ENTER>\nOpen selected file in horiz. split  i\nOpen selected file in vert. split   v\nFile menu                           m\nHelp                                ?</code></pre></div>\n<h2>Commands:</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Run a command                           :!&lt;command>\nOpen a shell                            :sh</code></pre></div>\n<p>For interactive tutorial, try <a href=\"https://openvim.com\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">openvim.com</a></p>\n<p>Follow me on <a href=\"https://github.com/rubiin\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Github</a></p>"}},{"node":{"frontmatter":{"title":"Caching your express app","description":null,"slug":"/pensieve/caching-expressjs-app","date":"2019-11-22","tags":["Nodejs","Redis","Express"],"draft":false},"html":"<p><img src=\"https://www.sohamkamani.com/static/18122218f0260b8206bc3bb69197ba7e/8ff1e/logo.png\" alt=\"Draft fail\"></p>\n<h1>Caching your express app</h1>\n<p><code class=\"language-text\">What is caching?</code>\nFetching something over the network is both slow and expensive. Large responses require many roundtrips between the client and server, which delays when they are available and when the browser can process them, and also incurs data costs for the visitor. As a result, the ability to cache and reuse previously fetched resources is a critical aspect of optimizing for performance\nRedis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. Redis always serves and modifies data in the server’s main memory. The impact is system will quickly retrieve data that will be needed. Redis works to help and improve load performance from relational databases or NoSQL by creating an excellent in-memory cache to reduce access latency. Using Redis we can store cache using SET and GET, besides that redis also can work with complex type data like Lists, Sets, ordered data structures, and so forth.</p>\n<h2>Installation</h2>\n<p>Installing redis is pretty staright forward.You can check the <a href=\"https://redis.io/topics/quickstart\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">link</a> for installation</p>\n<p>To add redis client to nodejs , simply do</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">npm i redis</span></code></pre></div>\n<p>For the sake of simplicity, we will use this example where I am fetching some data from an external api in an express app</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> responseTime <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'response-time'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\">//Load Express Framework</span>\n<span class=\"token keyword\">var</span> app <span class=\"token operator\">=</span> <span class=\"token function\">express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">//Create a middleware that adds a X-Response-Time header to responses.</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span><span class=\"token function\">responseTime</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> axios <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'axios'</span><span class=\"token punctuation\">)</span>\n\n\n<span class=\"token keyword\">async</span> <span class=\"token function\">getDogs</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span>res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">try</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">const</span> dogs <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> axios<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'https://dog.ceo/api/breeds/list/all'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span>dogs<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span> <span class=\"token keyword\">catch</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span>\n    res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Something went wrong!!!'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/dogs'</span><span class=\"token punctuation\">,</span> getDogs<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token number\">3000</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Your app is running on port 3000 !!!'</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>This is a simple express app which retrieves data from the Dogs' API, without putting Redis on the endpoint. Fairly simple\nHowever , the downward side is if we have already fetched data and if the data in the server is not modified, then there is no need to fetch the data on each request</p>\n<p>This is the same code but this time we have added a caching layer with <code class=\"language-text\">Redis</code></p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> responseTime <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'response-time'</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">const</span> redis <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'redis'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> client <span class=\"token operator\">=</span> redis<span class=\"token punctuation\">.</span><span class=\"token function\">createClient</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">//Load Express Framework</span>\n\n<span class=\"token keyword\">var</span> app <span class=\"token operator\">=</span> <span class=\"token function\">express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">//Create a middleware that adds a X-Response-Time header to responses.</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span><span class=\"token function\">responseTime</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> axios <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'axios'</span><span class=\"token punctuation\">)</span>\n\n\n<span class=\"token keyword\">async</span> <span class=\"token function\">getDogs</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span>res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">try</span> <span class=\"token punctuation\">{</span>\n\n\n  client<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'dogsdata'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">err<span class=\"token punctuation\">,</span> result</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>result<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\n     res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span>result<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">const</span> dogs <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> axios<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'https://dog.ceo/api/breeds/list/all'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token comment\">// Set the string-key:dogsdata in our cache. With he contents of the cache</span>\n    <span class=\"token comment\">// Set cache expiration to 1 hour (60 minutes)</span>\n\n    client<span class=\"token punctuation\">.</span><span class=\"token function\">setex</span><span class=\"token punctuation\">(</span><span class=\"token string\">'dogsdata'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3600</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span>dogs<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token punctuation\">}</span> <span class=\"token keyword\">catch</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span>\n    res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Something went wrong!!!'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/dogs'</span><span class=\"token punctuation\">,</span> getDogs<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token number\">3000</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Your app is running on port 3000 !!!'</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 are first fetching the data from the cache with the tag <code class=\"language-text\">dogsdata</code>. If the data exists in the cache, we serve the data else we make the request to the api via axios else we make the api request via axios. Everytime the api is called, the cache is updated with the latest data.\nRemember redis is a key value store thus the key with which you have set data must be equal to the key with which you fetch data in this case <code class=\"language-text\">dogsdata</code></p>\n<p>If you compare both the implementation, you can see that caching reduces the time taken to serve the request by almost more than 99%</p>\n<p>Follow me on <a href=\"https://github.com/rubiin\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Github</a></p>"}},{"node":{"frontmatter":{"title":"Docker basics/ cheatsheet","description":null,"slug":"/pensieve/docker-basics-cheatsheet-2mm4","date":"2019-11-22","tags":["Docker","Containers","Unix"],"draft":false},"html":"<p><img src=\"https://3kllhk1ibq34qk6sp3bhtox1-wpengine.netdna-ssl.com/wp-content/uploads/2017/01/how-to-deploy-java-apps-with-docker-a-quick-tutorial@3x-1560x760.png\" alt=\"Draft fail\"></p>\n<h3>Create image using this directory's Dockerfile</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">docker build -t &lt;somename></span></code></pre></div>\n<h3>Run <somename> mapping port 8000</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker run -p 8000:80 &lt;somename></span></code></pre></div>\n<h3>Same thing, but in detached mode</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker run -d -p 8000:80 &lt;somename></span></code></pre></div>\n<h3>Enter a running container (you can also do sh)</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker exec -it [container-id] bash</span></code></pre></div>\n<h3>See a list of all running containers</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker ps</span></code></pre></div>\n<h3>Gracefully stop the specified container</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker stop &lt;hash></span></code></pre></div>\n<h3>See a list of all containers , active and inactive</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker ps -a</span></code></pre></div>\n<h3>Force shutdown of the specified container</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker kill &lt;hash></span></code></pre></div>\n<h3>Remove the specified container from this machine</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker rm &lt;hash></span></code></pre></div>\n<h3>Remove all containers from this machine</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token command\"><span class=\"token info punctuation\"><span class=\"token path\">docker rm </span></span><span class=\"token shell-symbol important\">$</span><span class=\"token bash language-bash\"><span class=\"token punctuation\">(</span>docker <span class=\"token function\">ps</span> -a -q<span class=\"token punctuation\">)</span></span></span></code></pre></div>\n<h3>Show all images on this machine</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">docker images -a</span></code></pre></div>\n<h3>Remove the specified image from this machine</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">docker rmi &lt;imagename></span></code></pre></div>\n<h3>Remove all images from this machine</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token command\"><span class=\"token info punctuation\"><span class=\"token path\">docker rmi </span></span><span class=\"token shell-symbol important\">$</span><span class=\"token bash language-bash\"><span class=\"token punctuation\">(</span>docker images -q<span class=\"token punctuation\">)</span></span></span></code></pre></div>\n<h3>Log in this CLI session using your Docker credentials</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker login</span></code></pre></div>\n<h3>Tag <image> for upload to registry</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker tag &lt;image> username/repository:tag</span></code></pre></div>\n<h3>Upload tagged image to registry</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker push username/repository:tag</span></code></pre></div>\n<h3>Run image from a registry</h3>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\"> docker run username/repository:tag</span></code></pre></div>"}},{"node":{"frontmatter":{"title":"Custom jenkins images with plugins pre-installed","description":null,"slug":"/pensieve/custom-jenkins-images-with-plugins-pre-installed-1pok","date":"2019-10-12","tags":["Jenkins","Devops","CI/CD"],"draft":false},"html":"<p><img src=\"https://www.sohamkamani.com/static/18122218f0260b8206bc3bb69197ba7e/8ff1e/logo.png\" alt=\"Draft fail\"></p>\n<hr>\n<p>title:\npublished: true\ndescription:\ntags: jenkins,ci/cd,devops\ncover<em>image: <a href=\"https://miro.medium.com/max/3200/1*n7ba3rjWYs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://miro.medium.com/max/3200/1*n7ba3rjWYs</a></em>lZeYbKogKdw.jpeg</p>\n<hr>\n<p>If you are doing continous delivery or continous integration, you may frequntly run into jenkins.\nJenkins is a core part of our Continuous Integration and Continuous Deployment (CI/CD) pipeline. Here are the top 3 reasons we love Jenkins:</p>\n<ul>\n<li>It is an open system that is platform, language, and methodology agnostic allowing us to support our diverse client base with one non-proprietary CI/CD platform.</li>\n<li>It is the industry reference implementation for what a CI / CD system should be with a rich ecosystem of more than 1,100 plug-ins has emerged, enabling customers to add all sorts of functionality and integrate Jenkins with everything from Active Directory to GitHub to the OpenShift PaaS</li>\n<li>It is focused on CI/CD, and is not trying to be what it is not (Git repository, Chat client, Docker storage, etc.)\nThe beauty of using Jenkins is that you are not locked into any proprietary vendor. It has major support in the industry and many engineers are already familiar with it.</li>\n<li>Plus it offers plethora of features out of the box so you don't have to\nsearch google for hacks</li>\n</ul>\n<p>However one thing that has been a pain over period of using jenkins was the initial setup mostly during plugins installation. I mean this:</p>\n<p><img src=\"https://i.imgur.com/i09ODkv.png\"></p>\n<p><img src=\"https://media.giphy.com/media/VbnUQpnihPSIgIXuZv/giphy.gif\"></p>\n<p>So I wrote a custom jenkins image with all the plugins pre-installed,a default user, docker and docker-compose installed for people who want to use it without the hassle of configuring</p>\n<div class=\"gatsby-highlight\" data-language=\"dockerfile\"><pre class=\"language-dockerfile\"><code class=\"language-dockerfile\"><span class=\"token instruction\"><span class=\"token keyword\">FROM</span> jenkins/jenkins</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">ENV</span> JENKINS_USER admin</span>\n<span class=\"token instruction\"><span class=\"token keyword\">ENV</span> JENKINS_PASS admin</span>\n\n<span class=\"token comment\"># Skip initial setup</span>\n<span class=\"token instruction\"><span class=\"token keyword\">ENV</span> JAVA_OPTS -Djenkins.install.runSetupWizard=false</span>\n\n\n<span class=\"token instruction\"><span class=\"token keyword\">COPY</span> plugins.txt /usr/share/jenkins/plugins.txt</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> /usr/local/bin/install-plugins.sh &lt; /usr/share/jenkins/plugins.txt</span>\n<span class=\"token instruction\"><span class=\"token keyword\">USER</span> root</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> apt-get update <span class=\"token operator\">\\</span>\n    &amp;&amp; apt-get install -qqy apt-transport-https ca-certificates curl gnupg2 software-properties-common</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> add-apt-repository <span class=\"token operator\">\\</span>\n   <span class=\"token string\">\"deb [arch=amd64] https://download.docker.com/linux/debian \\\n   $(lsb_release -cs) \\\n   stable\"</span></span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> apt-get update  -qq <span class=\"token operator\">\\</span>\n    &amp;&amp; apt-get install docker-ce -y</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> usermod -aG docker jenkins</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> apt-get clean</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> curl -L <span class=\"token string\">\"https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)\"</span> -o /usr/local/bin/docker-compose &amp;&amp; chmod +x /usr/local/bin/docker-compose</span>\n<span class=\"token instruction\"><span class=\"token keyword\">USER</span> jenkins</span></code></pre></div>\n<p>Worrying about image size? Don't worry I got you covered. I also happen to write another alpine image for people who are space conservative :D</p>\n<div class=\"gatsby-highlight\" data-language=\"dockerfile\"><pre class=\"language-dockerfile\"><code class=\"language-dockerfile\"><span class=\"token instruction\"><span class=\"token keyword\">FROM</span> jenkins/jenkins:alpine</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">ENV</span> JENKINS_USER admin</span>\n<span class=\"token instruction\"><span class=\"token keyword\">ENV</span> JENKINS_PASS admin</span>\n\n<span class=\"token comment\"># Skip initial setup</span>\n<span class=\"token instruction\"><span class=\"token keyword\">ENV</span> JAVA_OPTS -Djenkins.install.runSetupWizard=false</span>\n\n\n<span class=\"token instruction\"><span class=\"token keyword\">COPY</span> plugins.txt /usr/share/jenkins/plugins.txt</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> /usr/local/bin/install-plugins.sh &lt; /usr/share/jenkins/plugins.txt</span>\n<span class=\"token instruction\"><span class=\"token keyword\">USER</span> root</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> apk add docker</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> apk add py-pip</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> apk add python-dev libffi-dev openssl-dev gcc libc-dev make</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> pip install docker-compose</span>\n<span class=\"token instruction\"><span class=\"token keyword\">USER</span> jenkins</span></code></pre></div>\n<p>For full source code and the plugins.txt file , you can checkout my repo at <a href=\"https://github.com/rubiin/jenkins\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://github.com/rubiin/jenkins</a></p>\n<blockquote>\n<p>Note: I specially wrote these images for a project where I was using docker-compose to manage my container stack, so feel free to omit the docker and compose setup</p>\n</blockquote>\n<p><img src=\"https://media.giphy.com/media/7XuPYJXaF1CBAmbwQQ/giphy.gif\"></p>"}},{"node":{"frontmatter":{"title":"Getting started with Api development on Nodejs","description":null,"slug":"/pensieve/getting-started-with-api-develpoment-on-nodejs-52bi","date":"2019-09-19","tags":["Nodejs","Backend","Express"],"draft":false},"html":"<p><img src=\"https://hackernoon.com/hn-images/1*26BcOdrwfRkbGk9OsREyLA.png\" alt=\"Draft fail\"></p>\n<p>Before we get our hands dirty and get a move on , I assume you have a sound knowledge of js .I am also assuming that you have nodejs installed on your machine.\nThere are plenty of web frameworks for nodejs . To point out a few popular ones, I might say express , koa and hapi. We will be going with express as it is the most used web framework for nodejs with tons of modules that are configured to work with it. Also the syntax and the learning curve is pretty easy to catch up and if you are to get stuck in a problem, there are pretty much answer to every query in stackoverflow.\nTo get started, lets create a directory and head over to it.After you are in the project directory, open up terminal/console and run npm init to start a nodejs project. Fill in the details as you like.After you are done , type in</p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">npm i — save express body-parser morgan nodemon</code></pre></div>\n<p>Hit enter and that should install and update the dependencies in package.json file.</p>\n<p><b>Nodemon</b> will monitor for any changes in your node.js application and automatically restart the server — perfect for development so you dont have to manually restart the server for viewing the changes.</p>\n<p><b>Morgan</b> is a HTTP request logger middleware for node.js .</p>\n<p><b>Body-parser</b> is Node.js body parsing middleware. It parses incoming request bodies in a middleware before your handlers, available under the req.body property.</p>\n<p>After all the modules have been installed , create a file app.js in the root folder and open it in your favorite editor. I use visual studio code but you could go with any any text editor of your perference.\nBasic Setup\nOpen app.js in the editor and require express module with</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> express<span class=\"token operator\">=</span><span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express'</span><span class=\"token punctuation\">)</span>\nNow create an express instance <span class=\"token keyword\">with</span>\n<span class=\"token keyword\">var</span> app<span class=\"token operator\">=</span> <span class=\"token function\">express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<h2>Routes</h2>\n<p>Now lets define some routes . A route method is derived from one of the HTTP methods, and is attached to an instance of the express class.\nThe following code is an example of routes that are defined for the GET and the POST methods to the root of the app.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">// GET method route</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'GET request to the homepage'</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\n<span class=\"token comment\">// POST method route</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">post</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'POST request to the homepage'</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><code class=\"language-text\">Express</code> supports methods that correspond to all HTTP request methods: get, post, and so on. For a full list, see app.METHOD.\nHere are some examples of route paths based on strings.\nThis route path will match requests to the root route, /. So if you visit the app in localhost:<your port>/ , you will get root printed on your window</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\">app<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'root'</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>This route path will match requests to /about.So if you visit the app in localhost:<your port>/ , you will get about printed on your window</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\">app<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/about'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'about'</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>You can also use regular expressions in routes to make your routes more dynamic.</p>\n<h2>Route parameters</h2>\n<p>To define routes with route parameters, simply specify the route parameters in the path of the route as shown below.This will simply output all parameters i.e userId and bookId as an object.You can also access a single param by using\nreq.params.paramname</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\">app<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/users/:userId/books/:bookId'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span>req<span class=\"token punctuation\">.</span>params<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>Summing all of the above\nThe following code is an example of a very basic route.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">var</span> app <span class=\"token operator\">=</span> <span class=\"token function\">express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// respond with \"hello world\" when a GET request is made to the homepage</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'hello world'</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\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/about'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'about'</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\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/:userId'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span>req<span class=\"token punctuation\">.</span>params<span class=\"token punctuation\">.</span>userId<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\napp<span class=\"token punctuation\">.</span><span class=\"token function\">post</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/post'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'this is a post request'</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\n<span class=\"token comment\">// tell express to run on port 8000</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token number\">8000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Thats all for this part .</p>\n<p>Follow me on <a href=\"https://github.com/rubiin\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Github</a></p>"}},{"node":{"frontmatter":{"title":"How to manage environment variables on NestJS","description":null,"slug":"/pensieve/how-to-manage-environment-variables-on-nestjs-30ge","date":"2019-09-18","tags":["Nodejs","Backend","NestJS"],"draft":false},"html":"<p><img src=\"https://i.imgur.com/C2C2cJW.png\" alt=\"Draft fail\"></p>\n<p>Environment variables allow us to manage the configuration of our applications separate from our codebase. Separating configurations make it easier for our application to be deployed in different environments(development,test,production).Also,large applications tend to have many environment variables.</p>\n<p>If you are working on some project with nestjs and want to configure your app secrets and credentials in a more efficient way, you came to the right place.In this article, I would like to share initial configuration and some tip to use environment variables with <b>Nestjs</b>.</p>\n<h2>Description</h2>\n<p>A common application level tool is <code class=\"language-text\">nestjs-easyconfig</code> which allows us to load environment variables from a file named .env[producton|dev|test].\n<a href=\"https://github.com/rubiin/nestjs-easyconfig\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Nestjs-easyconfig</a> loads configs from your <code class=\"language-text\">.env</code> (Wraps dotenv module) ⚙️ 🔥</p>\n<h2>Installation</h2>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ <span class=\"token function\">npm</span> <span class=\"token function\">install</span> nestjs-easyconfig\n$ <span class=\"token function\">yarn</span> <span class=\"token function\">add</span> nestjs-easyconfig</code></pre></div>\n<h2>Usage</h2>\n<h3>With config file supplied (basic):</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Module <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@nestjs/common'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> EasyconfigModule <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'nestjs-easyconfig'</span><span class=\"token punctuation\">;</span>\n\n@<span class=\"token function\">Module</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  imports<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>EasyconfigModule<span class=\"token punctuation\">.</span><span class=\"token function\">register</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> path<span class=\"token operator\">:</span> <span class=\"token string\">'./config/.env'</span> <span class=\"token punctuation\">}</span><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>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">AppModule</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></code></pre></div>\n<h3>With config file supplied and safe set to true:</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Module <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@nestjs/common'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> EasyconfigModule <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'nestjs-easyconfig'</span><span class=\"token punctuation\">;</span>\n\n@<span class=\"token function\">Module</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  imports<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>EasyconfigModule<span class=\"token punctuation\">.</span><span class=\"token function\">register</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> path<span class=\"token operator\">:</span> <span class=\"token string\">'./config/.env'</span><span class=\"token punctuation\">,</span> safe<span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><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>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">AppModule</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></code></pre></div>\n<p>By default safe is set to false. When safe is set to <code class=\"language-text\">true</code>, the module compares the supplied env\nfile with the sample env file to find missing keys. If any keys which are in .env.sample but not in the evironment used, it is immediately reported in console.</p>\n<p><code class=\"language-text\">Note</code>: To use this, a sample env file <code class=\"language-text\">.env.sample</code> should be placed in the root dir</p>\n<h3>Without config file supplied:</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Module <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@nestjs/common'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> EasyconfigModule <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'nestjs-easyconfig'</span><span class=\"token punctuation\">;</span>\n\n@<span class=\"token function\">Module</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  imports<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>EasyconfigModule<span class=\"token punctuation\">.</span><span class=\"token function\">register</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><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>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">AppModule</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></code></pre></div>\n<p>In this case, you have to pass in the <b>NODE<em>ENV</b> value and the <code class=\"language-text\">.env</code> file to read will be determined accordingly.\nLoads environment variables from <code class=\"language-text\">.env.[development|test|production][.local]</code> files\nFor example, <b>NODE</em>ENV=dev</b> will make the app read <code class=\"language-text\">.env.dev</code></p>\n<blockquote>\n<p>Note: The .env file also has to be in root folder</p>\n</blockquote>\n<h2>Stay in touch</h2>\n<ul>\n<li>Author - <a href=\"https://github.com/rubiin\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Rubin Bhandari</a></li>\n<li>Dev․to - <a href=\"https://dev.to/rubinsays\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">@rubinsays</a></li>\n<li>Discord - <a href=\"https://discordapp.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">@rubin#1186</a></li>\n</ul>\n<h2>License</h2>\n<p>The package is <a href=\"LICENSE\">MIT licensed</a>.</p>\n<p>Follow me on <a href=\"https://github.com/rubiin\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Github</a></p>"}},{"node":{"frontmatter":{"title":"Dynamically requiring all routes in express app","description":null,"slug":"/pensieve/docker-for-nodejs-3cjg","date":"2019-09-16","tags":["Nodejs","Webdev","Express"],"draft":false},"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>"}},{"node":{"frontmatter":{"title":"Docker for nodejs","description":null,"slug":"/pensieve/docker-for-nodejs-3cjg","date":"2019-09-16","tags":["Nodejs","Docker","Express"],"draft":false},"html":"<p><img src=\"https://miro.medium.com/max/2708/1*W4DwYGZCe2tMlcUIdDF0Sg.jpeg\" alt=\"Draft fail\"></p>\n<p>Docker is a great conterization platform with tons of out of the features out of the box. So, in this post we are going to skip the traditional hosting of app with packages like pm2 (although we can still use it inside docker).\nFirst of all, we will start by making a <code class=\"language-text\">Dockerfile</code>. A <code class=\"language-text\">Dockerfile</code> is a way to package your application.\nYou can learn the basics on docker from the <a href=\"https://www.docker.com/get-started\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">link</a></p>\n<p>The content of the Dockerfile will be like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"dockerfile\"><pre class=\"language-dockerfile\"><code class=\"language-dockerfile\"><span class=\"token instruction\"><span class=\"token keyword\">FROM</span> node:10</span>\n\n\n<span class=\"token instruction\"><span class=\"token keyword\">WORKDIR</span> /usr/src/app</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">COPY</span> package*.json ./</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> npm install</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">COPY</span> . .</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">EXPOSE</span> 8080</span>\n<span class=\"token instruction\"><span class=\"token keyword\">CMD</span> [ <span class=\"token string\">\"node\"</span>, <span class=\"token string\">\"server.js\"</span> ]</span></code></pre></div>\n<p>This will tell the docker engine to use the node:10 image and perform the steps.Though the file is self-explanatory, I will still do a lil bit of explaining</p>\n<ul>\n<li>First it will pull the image from dockerhub if it cannot find it in the machine</li>\n<li>Then it will use the directory <code class=\"language-text\">/usr/src/app</code> as the work directory for the project</li>\n<li>Thirdly, it will copy package.json and package-lock.json into the work directory and perform npm install which will inturn install all the dependencies required</li>\n<li>After, the dependencies are installed, it will copy all the files in the host machine to the container. Since we already have node_modules inside container, we may want to skip it. This can be done via <code class=\"language-text\">.dockerignore</code> file. Think of <code class=\"language-text\">dockerignore</code> same as <code class=\"language-text\">gitignore</code> but for docker</li>\n</ul>\n<p>A sample <b>.dockerignore</b> file</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"># Logs\nlogs\n*.log\n\n# Runtime data\npids\n*.pid\n*.seed\n\n# Directory for instrumented libs generated by jscoverage/JSCover\nlib-cov\n\n# Coverage directory used by tools like istanbul\ncoverage\n\n# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)\n.grunt\n\n# node-waf configuration\n.lock-wscript\n\ndist\n\nnode_modules\n\nserver/*.spec.js</code></pre></div>\n<ul>\n<li>The expose command will open a port in the container follwed by the port number which is 8080 in our case. Make sure to match this with the port used by the app</li>\n<li>CMD command will execute the command passed which is <code class=\"language-text\">node server.js</code> . It can even be a npm script like <code class=\"language-text\">npm start</code> . This should be the command that spins up the server</li>\n</ul>\n<h2>Building your image</h2>\n<p>Go to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it's easier to find later using the docker images command:</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">docker build -t &lt;your username>/node-web-app .</span></code></pre></div>\n<h2>Run the image</h2>\n<p>Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container. Run the image you previously built:</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">docker run -p 49160:8080 -d &lt;your username>/node-web-app</span></code></pre></div>\n<p>However this approach doesn't reflect the changes that you made in your code after the image is built. So for every change you have to perform the build and run step again and again.</p>\n<p>Luckily docker comes with something called volume mapping which instead of copying the file maps the working directory with the files from host machine. So every time a change occurs on any file in your app, it is automatically reflected inside the container as well and wont need to build the image again.\nTo use this approach , the dockerfile becomes</p>\n<div class=\"gatsby-highlight\" data-language=\"dockerfile\"><pre class=\"language-dockerfile\"><code class=\"language-dockerfile\"><span class=\"token instruction\"><span class=\"token keyword\">FROM</span> node:10</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">WORKDIR</span> /usr/src/app</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">COPY</span> package.json .</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> npm i</span>\n<span class=\"token instruction\"><span class=\"token keyword\">COPY</span> . .</span></code></pre></div>\n<p>Once you have modified the file, you can build the image as you did previously</p>\n<p>To run the built image though, there is a slight change</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">docker run -p 49160:8080 -v $(pwd):/usr/src/app -d &lt;your username>/node-web-app</span></code></pre></div>\n<p><code class=\"language-text\">pwd</code> is the command to get the current directory in linux so make sure to use the run command when you are inside the app directory</p>\n<p>Follow me on <a href=\"https://github.com/rubiin\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Github</a></p>"}},{"node":{"frontmatter":{"title":"How to manage multiple nodejs version in a single machine using nvm","description":null,"slug":"/pensieve/how-to-manage-multiple-nodejs-version-in-a-single-machine-using-nvm-170a","date":"2019-09-14","tags":["Nodejs","NPM","NVM"],"draft":false},"html":"<p><img src=\"https://miro.medium.com/max/3200/1*n7ba3rjWYs_lZeYbKogKdw.jpeg\" alt=\"Draft fail\"></p>\n<p>Ever got in a situation where you have to use different nodejs versions for different projects. or you want to have the stable and the latest release side by side. Installing the particular release when you need it might seem a probable solution. But what if you need multiple node versions frequently. The above process seems kind of illogical for the purpose. So NVM aka node version manager comes to the rescue allowing you to have more than one version of node on your machine so you can switch the version with just a command.For Mac and Linux the link is <a href=\"http://github.com/creationix/nvm\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">http://github.com/creationix/nvm</a> .For windows <a href=\"http://github.com/coreybutler/nvm-windows\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">http://github.com/coreybutler/nvm-windows</a> . All you have to do is go to the links depending upon your machine’s OS and download the latest release.\nGetting started\nAfter getting nvm installed on your machines , open up terminal or cmd and type in</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">nvm list</span></code></pre></div>\n<p>To list all the node versions installed on the machine. To install a particular node version use</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">nvm install &lt;node version number></span></code></pre></div>\n<p>for example,</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">nvm install 10</span></code></pre></div>\n<p>To use a particular node version installed on your machine do</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">nvm use &lt;node version></span></code></pre></div>\n<p>To uninstall a node version, do</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">nvm uninstall &lt;node version></span></code></pre></div>\n<p>You can also pass in optional parameter which is the 32 or 64 bit version you would like to install or uninstall with install and uninstall command.\nFor help type in</p>\n<div class=\"gatsby-highlight\" data-language=\"shell-session\"><pre class=\"language-shell-session\"><code class=\"language-shell-session\"><span class=\"token output\">nvm -help</span></code></pre></div>\n<p>Happy Node’ing</p>"}}]}},"pageContext":{}},"staticQueryHashes":["3115057458"]}