<?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: Kunal Rohitas</title>
    <description>The latest articles on DEV Community by Kunal Rohitas (@kunal4now).</description>
    <link>https://dev.to/kunal4now</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%2F758885%2Fdd8c741c-92ce-4cc3-966e-f54c17441e1e.jpg</url>
      <title>DEV Community: Kunal Rohitas</title>
      <link>https://dev.to/kunal4now</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kunal4now"/>
    <language>en</language>
    <item>
      <title>Deploy your Node.js App on Heroku using GitHub</title>
      <dc:creator>Kunal Rohitas</dc:creator>
      <pubDate>Mon, 25 Apr 2022 04:22:11 +0000</pubDate>
      <link>https://dev.to/kunal4now/deploy-your-nodejs-app-on-heroku-using-github-31jp</link>
      <guid>https://dev.to/kunal4now/deploy-your-nodejs-app-on-heroku-using-github-31jp</guid>
      <description>&lt;p&gt;In this tutorial, we will go over the process of deploying a Node.js application on Heroku for free. Deploying an application can be a tedious task but if we follow the steps properly then it's a cakewalk.&lt;/p&gt;

&lt;p&gt;When I was deploying my first app I had to browse many articles in order to know the complete steps, to save your time here I've listed step-by-step procedures to deploy your app.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To get started you will require a heroku account that you can easily create&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here I’ve taken a simple demo project to demonstrate the deployment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kz11dtvI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2468/1%2AaNngbpH7F-vb0GWysaiswA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kz11dtvI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2468/1%2AaNngbpH7F-vb0GWysaiswA.png" alt="" width="880" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how our Node.js project looks like, it only contains an index.html file that will be rendered on the homepage of the website.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Initialize a git repository in the project
&lt;/h2&gt;

&lt;p&gt;To initialize a git repository, open a terminal in your project directory and run the following command.&lt;/p&gt;

&lt;p&gt;or if your project already has a git repo then you can skip this part :)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  2. Configure your app using Environment Variables
&lt;/h2&gt;

&lt;p&gt;Environment variables allow us to configure our application without re-building it. It externalizes the environment-specific aspects of the application.&lt;/p&gt;

&lt;p&gt;Environment variables are helpful to configure parameters such as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The port on which your application is running.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If your project uses an API or any other thing that contains sensitive information.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First, you need to install the ‘dotenv’ package which can be done in your terminal&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;you need to add to your index.js file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv).config();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, create a .env file in your project directory and add the parameters that you want to configure externally.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m_yQ7Wif--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2260/1%2Arf8134osbCKWNpfZi7GoXA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m_yQ7Wif--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2260/1%2Arf8134osbCKWNpfZi7GoXA.png" alt="" width="880" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;similarly, you can add other parameters.&lt;/p&gt;

&lt;p&gt;To use these variables in your app you can access them using&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“process.env.” in your index file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s_tdVkmG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2660/1%2ARhuX740Z_vfW0t-6UB2ddw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s_tdVkmG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2660/1%2ARhuX740Z_vfW0t-6UB2ddw.png" alt="" width="880" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, I've added the port using the syntax I've stated above.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;IMPORTANT!&lt;br&gt;
 Do not forget to add your .env file to .gitignore because if you don't do that when you push your project to GitHub your API keys and sensitive data will be exposed. Also add node_modules folder to .gitignore.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After following the above steps your .gitignore file should have at least .env and node_modules in it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0vp81rQv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2314/1%2AXYk0A2Rf7FdVN-VPU-Mjqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0vp81rQv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2314/1%2AXYk0A2Rf7FdVN-VPU-Mjqg.png" alt="" width="880" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Create a Procfile
&lt;/h2&gt;

&lt;p&gt;A Procfile is a file that specifies &lt;strong&gt;the commands that are executed by the app on startup.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a file named Procfile into your project directory and add the below command to it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web: node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;after making the changes to the project it is a good time to commit your changes, to push your repo to GitHub you can follow &lt;a href="https://docs.github.com/en/github/importing-your-projects-to-github/importing-source-code-to-github/adding-an-existing-project-to-github-using-the-command-line"&gt;this&lt;/a&gt; article.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Download Heroku CLI and Sign In
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;&lt;strong&gt;The Heroku CLI&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Download and install the CLI from the above link.&lt;/p&gt;

&lt;p&gt;Log in into the CLI by running the command and it will take you to Heroku login page and you can fill your credentials there.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku login&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  

&lt;ol&gt;
&lt;li&gt;Create a new app on Heroku
&lt;/li&gt;
&lt;/ol&gt;
&lt;/h2&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mXuFaL9r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3832/1%2ArNcxnjTso9E2iFISiKsebg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mXuFaL9r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3832/1%2ArNcxnjTso9E2iFISiKsebg.png" alt="" width="880" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a new app and it will prompt you to enter the name of the app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gZX4cMa1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3826/1%2Aq9qexf64QP0DWFZAzORjJA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gZX4cMa1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3826/1%2Aq9qexf64QP0DWFZAzORjJA.png" alt="" width="880" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your application uses an API key and you have added it in .env file then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to the settings&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In config vars section add the API keys or your secret keys in the key-value input.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: the inputs in key, values should match with the names of the variables you have given in .env file&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  6. Connecting your GitHub Repository with Heroku and deploying your application
&lt;/h2&gt;

&lt;p&gt;Well, that was quite a lot of work ;)&lt;/p&gt;

&lt;p&gt;Now in your application dashboard in the deploy section, select the deployment method as GitHub.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CJp60c1h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3828/1%2AWDO79uPDOJocMzgmv-NB6Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CJp60c1h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3828/1%2AWDO79uPDOJocMzgmv-NB6Q.png" alt="enter the repository name that you want to deploy and then connect." width="880" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After your repository is connected just scroll down and deploy your app and after a minute or two of processing it will ask you to visit your application that has been deployed.&lt;/p&gt;

&lt;p&gt;In our case this is how it looks xD.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HPuu_VbK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3826/1%2AW-_qOzFXoc_iv-M0gwlEoQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HPuu_VbK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3826/1%2AW-_qOzFXoc_iv-M0gwlEoQ.png" alt="" width="880" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations you have successfully deployed your Node.js application, now you can flex your projects in front of the world :)&lt;/p&gt;

&lt;p&gt;If you liked this tutorial, please feel free to leave a comment below. Open to feedback.&lt;/p&gt;

&lt;p&gt;You can connect with me on &lt;a href="https://twitter.com/Kunal_rts"&gt;Twitter&lt;/a&gt;, &lt;a href="https://github.com/Kunal4now"&gt;GitHub&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/kunal-rohitas-a96205a9"&gt;LinkedIn&lt;/a&gt;, I would love to answer any question that you have!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
