<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kristian-Roopnarine</title>
    <description>The latest articles on DEV Community by Kristian-Roopnarine (@kristianroopnarine).</description>
    <link>https://dev.to/kristianroopnarine</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F516047%2F2f72c573-8a48-469d-b8da-4e23acda2f84.jpeg</url>
      <title>DEV Community: Kristian-Roopnarine</title>
      <link>https://dev.to/kristianroopnarine</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kristianroopnarine"/>
    <language>en</language>
    <item>
      <title>How to separate your test, development and production databases using NODE_ENV.</title>
      <dc:creator>Kristian-Roopnarine</dc:creator>
      <pubDate>Fri, 27 Nov 2020 18:22:42 +0000</pubDate>
      <link>https://dev.to/kristianroopnarine/how-to-separate-your-test-development-and-production-databases-using-nodeenv-anl</link>
      <guid>https://dev.to/kristianroopnarine/how-to-separate-your-test-development-and-production-databases-using-nodeenv-anl</guid>
      <description>&lt;h1&gt;
  
  
  Why should you separate your test, development and production databases?
&lt;/h1&gt;

&lt;p&gt;As your applications become more complex you're going to incorporate different methods of testing to help ensure that your application works as intended. Of course this doesn't make your application 100% bullet proof, which is why proper testing is important, but why is it important to separate our testing environment from development and production? When running test suites, there is usually some degree of changes made to the database in the form of updating entries, adding, deleting, forcing errors etc. We want to make sure that when we run our tests, they don't rely on previous data to ensure that one test doesn't influence another. An easy way to do this is to implement some clean up method before and after a test is run, which usually means clearing a database and filling it with the same dummy data. &lt;br&gt;
Running a test suite on a production database would be irresponsible because a test might have unwanted side effects and alter the state of our database, or worse it would reveal information about our tests and source code. The data inside of the production database is always changing as well, which means that a test suite might work one day, but fail another day because the same data was altered causing our tests to fail.&lt;/p&gt;
&lt;h1&gt;
  
  
  How to separate application environments using NODE_ENV
&lt;/h1&gt;

&lt;p&gt;We can store environment variables in the node process (highly encourage you to read up on this if you're not familiar). Then we can access any of these variables in any part of our program. These variables are also secure so it is common to store API keys, credentials and other secrets inside of the node process. When running our node applications we can set the NODE_ENV and conditionally change the applications settings based on this NODE_ENV. It is a common practice to store these variables as key=value pairs inside of a .env file shown below. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The environment is set to development by default and one implementation of this is found below by changing our the URI to our database. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This does not have to be used with mongoose, it was just an example. But depending on the environment our application will use a different database. This can be used to change the settings of other features as well, maybe you want to enter a testing environment for stripe, or sandbox environment to test an SDK.&lt;/p&gt;

&lt;h1&gt;
  
  
  How to set NODE_ENV
&lt;/h1&gt;

&lt;p&gt;Now that we know how to conditionally render settings using NODE_ENV, how do we set NODE_ENV when running our application? A common approach is to set them in &lt;code&gt;package.json&lt;/code&gt; scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  ...
  "scripts": {
    "start": "NODE_ENV=production node ./app"
  }
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way you can start an app in either a test, development or production environment.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>backend</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Add docker to your NodeJS workflow in just 4 simple steps</title>
      <dc:creator>Kristian-Roopnarine</dc:creator>
      <pubDate>Wed, 18 Nov 2020 09:13:17 +0000</pubDate>
      <link>https://dev.to/kristianroopnarine/add-docker-to-your-nodejs-workflow-in-just-4-simple-steps-1ggn</link>
      <guid>https://dev.to/kristianroopnarine/add-docker-to-your-nodejs-workflow-in-just-4-simple-steps-1ggn</guid>
      <description>&lt;h1&gt;
  
  
  What is docker and why should you use it?
&lt;/h1&gt;

&lt;p&gt;Docker is a platform as a service which allows you to isolate an entire operating system via Linux containers. The files we create below are instructions for how Docker should build the Linux container. Dockerizing (is that a word?) your application should be the first step in your workflow as it provides a good base for your development and production environments. Onboarding new members to the project is easy too, just provide them the respective &lt;code&gt;Dockerfile&lt;/code&gt; and they’re good to go. Docker can become extremely complicated and you can add a lot of configuration to your application with Docker alone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You will definitely benefit from spending time researching complex features of Docker!&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Add docker to your NodeJS app in 4 steps
&lt;/h1&gt;

&lt;p&gt;I don’t explain what the following code does, but docker has great documentation on it, &lt;a href="https://docs.docker.com/engine/reference/builder/"&gt;https://docs.docker.com/engine/reference/builder/&lt;/a&gt; .&lt;br&gt;
All of the following config files will be at the root of your project. These files will work for probably 90% of your NodeJS applications, barring specific configuration.&lt;br&gt;
If this is an application that will be shipped to production, create a separate &lt;code&gt;Dockerfile.prod&lt;/code&gt; that contains configuration for your production application. These config settings can be googled because there are many people who have used Docker in production. This helps separate dev vs prod configuration with ease.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;Dockerfile.dev&lt;/code&gt; with the following code:&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;docker-compose.yml&lt;/code&gt; with the following code:&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;.dockerignore&lt;/code&gt; and add &lt;code&gt;node_modules&lt;/code&gt; to it. Any files/folders in the &lt;code&gt;.dockerignore&lt;/code&gt; will not be copied over to the Docker container. Since &lt;code&gt;node_modules&lt;/code&gt; is usually a large directory, adding this to a &lt;code&gt;.dockerignore&lt;/code&gt; this will speed up your build times. &lt;strong&gt;Add other files/directories you do not want to be copied into your docker container here!&lt;/strong&gt; &lt;/li&gt;
&lt;/ol&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Execute the following commands in the root directory of your project depending on what you need:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;docker-compose up -d --build&lt;/code&gt; : Builds the container using Dockerfile.dev and starts your docker container in detached mode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;docker-compose up -d&lt;/code&gt; Starts your docker container in detached mode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;docker-compose down&lt;/code&gt; &lt;strong&gt;ALWAYS&lt;/strong&gt; run this command when stopping your container&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;docker ps&lt;/code&gt; Lists your current active containers&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the initial build is successful execute &lt;code&gt;docker ps&lt;/code&gt; and ensure that your container is running. If so, head on over to &lt;code&gt;localhost:5000&lt;/code&gt; where your application is hosted locally. You can change this port through the &lt;code&gt;port&lt;/code&gt; setting in the &lt;code&gt;docker-compose.yml&lt;/code&gt; .&lt;/p&gt;

</description>
      <category>docker</category>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
