<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ganesh Agrawal - An enthusiastic programmer]]></title><description><![CDATA[Hi, I’m Ganesh. I write about Javascript stack, DS & Algo, and productivity hacks. If you’d like to say hello, follow me on Twitter or Github.]]></description><link>https://blog.ganeshagrawal.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1594990491103/bzIHx31LW.png</url><title>Ganesh Agrawal - An enthusiastic programmer</title><link>https://blog.ganeshagrawal.com</link></image><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 04:19:18 GMT</lastBuildDate><atom:link href="https://blog.ganeshagrawal.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to setup HTTPS locally with create-react-app]]></title><description><![CDATA[Generally, we saw that the React app running locally on http://localhost:3000. But when we deploy and run the app on production we mostly host on HTTPS. Running HTTPS in development is helpful when you need to consume an API that is also serving requ...]]></description><link>https://blog.ganeshagrawal.com/how-to-setup-https-locally-with-create-react-app</link><guid isPermaLink="true">https://blog.ganeshagrawal.com/how-to-setup-https-locally-with-create-react-app</guid><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[SSL]]></category><category><![CDATA[https]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Ganesh Agrawal]]></dc:creator><pubDate>Fri, 31 Jul 2020 13:27:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1596198330993/gNdE07sG8.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Generally, we saw that the React app running locally on <code>http://localhost:3000</code>. But when we deploy and run the app on production we mostly host on HTTPS. Running HTTPS in development is helpful when you need to consume an API that is also serving requests via HTTPS.</p>
<p>Hello, Today we are going to discuss <strong>How to serve a local React app via HTTPS.</strong> In this article, we will be setting up HTTPS in development for our create-react-app with our SSL certificate.</p>
<p> <a target='_blank' rel='noopener noreferrer'  href="https://reactjs.org/docs/create-a-new-react-app.html">Create-react-app (CRA)</a>  is a convenient and easy way to set up initial boilerplate when developing React App.  To start building React App using CRA, we generally use the following steps to start the setup and run the default CRA app.</p>
<pre><code class="lang-bash">npx create-react-app dummy-app
<span class="hljs-built_in">cd</span> dummy-app
npm start
</code></pre>
<p>After running these steps our react app runs locally at <code>http://localhost:3000</code>. But we want to runs it at <code>https://localhost:3000</code>. So, the question is how do we run or test the app locally on https.</p>
<p>This is a very simple process for setup own SSL certificate and HTTPS in development. For this guide,  <a target='_blank' rel='noopener noreferrer'  href="https://brew.sh/">brew</a>  pre-installed required.</p>
<h3 id="step-1-adding-https">Step 1: Adding HTTPS</h3>
<p>In your <code>package.json</code>, update the start script.</p>
<pre><code class="lang-javascript"><span class="hljs-string">"scripts"</span>: {
    <span class="hljs-string">"start"</span>: <span class="hljs-string">"HTTPS=true react-scripts start"</span>,
    <span class="hljs-string">"build"</span>: <span class="hljs-string">"react-scripts build"</span>,
    <span class="hljs-string">"test"</span>: <span class="hljs-string">"react-scripts test"</span>,
    <span class="hljs-string">"eject"</span>: <span class="hljs-string">"react-scripts eject"</span>
}
</code></pre>
<p>Now run <code>npm start</code> or <code>yarn start</code> and your browser screen show this-</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1596199757184/zo_oZAjfQ.png" alt="Privacy Error"></p>
<h3 id="step-2-creating-an-ssl-certificate">Step 2: Creating an SSL Certificate</h3>
<p>At the previous stage, you&#39;re already set to go with https. But you don&#39;t have a valid certificate, so your connection is assumed to be insecure. So now we create our SSL certificate that works with the localhost. The easiest way to obtain a certificate is via <a target='_blank' rel='noopener noreferrer'  href="https://github.com/FiloSottile/mkcert">mkcert</a>.</p>
<p>Run these commands in the terminal-</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Install mkcert tool</span>
brew install mkcert

<span class="hljs-comment"># Setup mkcert on your machine (creates a CA)</span>
mkcert -install
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1596200142159/1W5O0tt0_.png" alt="Setup mkcert on your machine"></p>
<p>After running the above commands, you&#39;ll have created a certificate authority on your machine which enables you to generate certificates for all of your future projects.</p>
<p>From the root of your <code>create-react-app</code> project, you should now run:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Create .cert directory if it doesn't exist</span>
mkdir -p .cert

<span class="hljs-comment"># Generate the certificate (run from the root of this project)</span>
mkcert -key-file ./.cert/key.pem -cert-file ./.cert/cert.pem <span class="hljs-string">"localhost"</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1596200390565/Qb1Q68do7.png" alt="Generate the certificate"></p>
<p>We&#39;ll be storing our generated certificates in the <code>.cert</code> directory. These should not be committed to version control, so you should update your <code>.gitignore</code> to include the <code>.cert</code> directory.</p>
<h3 id="step-3-include-ssl-certificate-in-start-script">Step 3: Include SSL certificate in start script</h3>
<p>Next, we need to update the start script again to include our newly created certificate:</p>
<pre><code class="lang-javascript"><span class="hljs-string">"scripts"</span>: {
    <span class="hljs-string">"start"</span>: <span class="hljs-string">"HTTPS=true SSL_CRT_FILE=./.cert/cert.pem SSL_KEY_FILE=./.cert/key.pem react-scripts start"</span>,
    <span class="hljs-string">"build"</span>: <span class="hljs-string">"react-scripts build"</span>,
    <span class="hljs-string">"test"</span>: <span class="hljs-string">"react-scripts test"</span>,
    <span class="hljs-string">"eject"</span>: <span class="hljs-string">"react-scripts eject"</span>
}
</code></pre>
<p>When you run <code>npm start</code> or  <code>yarn start</code> again, you should now see that your connection is secure.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1596200731237/BGSOnUvj-.png" alt="How to setup HTTPS locally with create-react-app"></p>
<blockquote>
<p><strong>Note:</strong> start script may have some changes for Windows, Linux, and Mac users.</p>
</blockquote>
<p>Don&#39;t be a non-interactive person! Feel free to comment down your suggestions and questions, I am always open for your queries. </p>
<p>Follow me on <a target='_blank' rel='noopener noreferrer'  href="https://instagram.com/iamganeshagrawal">Instagram</a> or  <a target='_blank' rel='noopener noreferrer'  href="https://www.linkedin.com/in/iamganeshagrawal/">Linkedin</a>. Subscribe my weekly  <a target='_blank' rel='noopener noreferrer'  href="https://tinyletter.com/ganeshagrawal">Newsletter</a> , I assure its not going to spam your mailbox.</p>
]]></content:encoded></item><item><title><![CDATA[How to use environment variables in Node.js application]]></title><description><![CDATA[Deploying an application requires developers to put thought and consideration into how it is configured. Many apps are deployed in a development environment before being deployed to the production environment. We need to ensure each environment is co...]]></description><link>https://blog.ganeshagrawal.com/how-to-use-environment-variables-in-nodejs-application</link><guid isPermaLink="true">https://blog.ganeshagrawal.com/how-to-use-environment-variables-in-nodejs-application</guid><category><![CDATA[Node.js]]></category><category><![CDATA[node]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Ganesh Agrawal]]></dc:creator><pubDate>Thu, 23 Jul 2020 14:26:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1595510663873/8epWd0qWB.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Deploying an application requires developers to put thought and consideration into how it is configured. Many apps are deployed in a development environment before being deployed to the production environment. We need to ensure each environment is configured correctly, it could be disastrous if our production application was using our development database, for example.</p>
<p>Environment variables are helpful in software development. Many programs and applications set or get environment variables.</p>
<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.</p>
<h2 id="what-are-environment-variables-">What are environment variables?</h2>
<p>Environment variables are variables external to our application that reside in the OS or the container of the app is running in. An environment variable is simply a name mapped to a value.</p>
<p>By convention, the name is capitalized e.g. <code>JWT_REFRESH_SALT=a3889318b21d7b92</code>. The values are strings.</p>
<p>If you open the terminal or command line application in Linux, Mac OS, or Windows and enter <code>set</code>, you will see a list of all environment variables for your user.</p>
<blockquote>
<h3 id="did-you-know-">Did you know?</h3>
<p>When we install any software (like Java SDK, Language compiler, or databases, ...). In the case of many software, we have to set a <code>PATH</code> variable for that. That PATH variable also a part of the environment variable of your operating system.</p>
</blockquote>
<h2 id="why-do-we-need-environment-variables-">Why do we need environment variables?</h2>
<p>Environment variables are excellent for decoupling application configurations. Typically, our applications require many variables to be set in order for them to work. By relying on external configurations, your app can easily be deployed in different environments. These changes are independent of code changes, so they do not require your application to be rebuilt to change.</p>
<ul>
<li><strong>Security:</strong> Some things like API keys should not put in plain text in the code and thereby directly visible.</li>
<li><strong>Flexibility:</strong> you can reduce conditional statements e.g. <em>“If production server then do X else if test server then do Y else if localhost then do Z …”.</em></li>
<li><strong>Adoption:</strong> Popular services like GitLab or Heroku support the usage of environment variables.</li>
</ul>
<p>Data that changes depending on the environment your app is running on should be set as environment variables. Some common examples are:</p>
<ul>
<li>HTTP Port and Address</li>
<li>Database, cache, and other storage connection information</li>
<li>Location of static files/folders</li>
<li>Endpoints of external services<ul>
<li>For example, in a development environment, your app will point to a test API URL, whereas in a production environment your app will point to the live API URL.</li>
</ul>
</li>
</ul>
<blockquote>
<p>Sensitive data like API keys should not be in the source code or known to persons who do not need access to those external services.</p>
</blockquote>
<h2 id="how-does-a-env-file-look-like-">How does a .env file look like?</h2>
<p>A .env file is more or less a plain text document. It should be placed in the root of your project. You specify key-value pairs and you can write single-line comments using #.</p>
<p>Example of .env file:</p>
<pre><code class="lang-env"># JWT
AUTH_TOKEN_SALT=a7f1681c86cee718
REFRESH_TOKEN_SALT=df4f2ffea4ddf7ef

# SERVER CONFIG
PORT=8080

# DATABASE
DB_HOSTNAME=mysql.example.com
DB_HOSTPORT=3606
DB_USERNAME=user
DB_PASSWORD=demo123
DB_DBNAME=testDb
</code></pre>
<h2 id="how-to-use-custom-environment-variables-in-node-js">How to use custom environment variables in Node.js</h2>
<ol>
<li>Create a <strong>.env</strong> file. The file should be placed in the root of your project.</li>
<li>Install the <code>dotenv</code> npm module using:<pre><code class="lang-sh">npm install dotenv --save
<span class="hljs-comment"># OR</span>
yarn add dotenv
</code></pre>
</li>
<li>Require <code>dotenv</code> as early as possible (e.g. in app.js):<pre><code class="lang-javascript"><span class="hljs-built_in">require</span>(<span class="hljs-string">'dotenv'</span>).config({path: __dirname + <span class="hljs-string">'/.env'</span>})
</code></pre>
</li>
<li>Wherever you need to use environment variables (e.g. in GitLab, in Jenkins, in Heroku, …) you need to add your environment variables. The way depends on the platform but it is usually easy to do.</li>
</ol>
<h2 id="how-to-access-environment-variables-in-node-js">How to access environment variables in Node.js</h2>
<p>Consider a hello world Node.js application with environment variables for the host and port the app runs on.</p>
<p>Create a new file called app.js in a workspace of your choice and add the following:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);

<span class="hljs-comment">// Load custom environment variables</span>
<span class="hljs-built_in">require</span>(<span class="hljs-string">'dotenv'</span>).config();

<span class="hljs-comment">// Read the host address and the port from the environment</span>
<span class="hljs-keyword">const</span> hostname = process.env.HOST;
<span class="hljs-keyword">const</span> port = process.env.PORT;

<span class="hljs-comment">// Return JSON regardless of HTTP method or route our web app is reached by</span>
<span class="hljs-keyword">const</span> server = http.createServer((req, res) =&gt; {
    res.statusCode = <span class="hljs-number">200</span>;
    res.setHeader(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'application/json'</span>);
    res.end(<span class="hljs-string">`{"message": "Hello World"}`</span>);
});

<span class="hljs-comment">// Start a TCP server listening for connections on the given port and host</span>
server.listen(port, hostname, () =&gt; {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Server running at http://<span class="hljs-subst">${hostname}</span>:<span class="hljs-subst">${port}</span>/`</span>);
});
</code></pre>
<p>Also, create a new file called <code>.env</code> in the root of your app.js file and add the following:</p>
<pre><code class="lang-env"># Server Port &amp; Host
PORT=4000
HOST=localhost
</code></pre>
<p>Node.js provides a global variable <code>process.env</code>, an object that contains all environment variables available to the user running the application. It is expecting the hostname and the port that the app will run on to be defined by the environment.</p>
<p>You can run this application by entering this command in the terminal, <code>node app.js</code>, granted you have Node.js installed. You will notice the following message on your console:</p>
<pre><code class="lang-bash">Server running at http://localhost:4000/
</code></pre>
<blockquote>
<p><strong>Note:</strong> Even if dotenv is used in the application, it is completely optional. If no .env file is found, the library fails silently. You can continue to use environment variables defined outside the file.</p>
</blockquote>
<h2 id="production-usage">Production Usage</h2>
<p>Storing your environment variables in a file comes with one golden rule: never commit it to the source code repository. You do not want outsiders to gain access to secrets, like API keys. If you are using <code>dotenv</code> to help manage your environment variables, be sure to include the <code>.env</code> file in your <code>.gitignore</code> or the appropriate blacklist for your version control tool.</p>
<p>If you cannot commit the <code>.env</code> file, then there needs to be some way for a developer to know what environment variables are required to run the software. It is common for developers to list the environment variables necessary to run the program in a <strong>README</strong> or similar internal documentation.</p>
<p>Some developers create and maintain a <code>.sample-env</code> file in the source code repository. This sample file would list all the environment variables used by the app, for example:</p>
<pre><code class="lang-env"># Server Port &amp; Host
PORT=
HOST=
</code></pre>
<p>A developer would then clone the repository, copy the <code>.sample-env</code> file into a new <code>.env</code> file and fill in the values.</p>
<p>If your app is running on a docker container or a Platform-as-a-Service provider like Heroku or Openshift, then you will be able to configure environment variables without having to use the <code>.env</code> file.</p>
<p><strong>Remember</strong>, it fails silently so it would not affect the running of the app if the file is missing.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Thanks for reading this article. As you can see, it is easy to use and work with environment variables in Node.js. Like Node.js other languages also supports environment variable and have their respective modules for handling them. How are you using environment variables? Let me know in the comments.</p>
]]></content:encoded></item><item><title><![CDATA[Hello World!]]></title><description><![CDATA[A “Hello World!” program is often used as a simplistic introduction to programming languages, as it’s generally quite self-explanatory, and simple enough that even those with little or no programming experience can understand its function. However, d...]]></description><link>https://blog.ganeshagrawal.com/hello-world</link><guid isPermaLink="true">https://blog.ganeshagrawal.com/hello-world</guid><category><![CDATA[hello]]></category><category><![CDATA[Hello World]]></category><category><![CDATA[coding]]></category><category><![CDATA[programmer]]></category><dc:creator><![CDATA[Ganesh Agrawal]]></dc:creator><pubDate>Thu, 09 Jul 2020 14:57:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1594645715165/q01-kk5fR.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A <strong>“Hello World!”</strong> program is often used as a simplistic introduction to programming languages, as it’s generally quite self-explanatory, and simple enough that even those with little or no programming experience can understand its function. However, do not mistake its simplicity for a lack of utility. The program needs a functioning language compiler and run-time to accomplish its task. This makes it ideal for a quick and easy sanity test for new systems.</p>
<p>I remember when I started writing codes about a couple of years ago, the very first thing I learned was the famous hello world statement which is sure its the same across all languages.</p>
<blockquote>
<p>Despite the myth that “Hello World!” is the first thing to be output by all computers, its prevalence in nearly all facets of technology is undeniable.</p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1594646598566/m_XhZnyMT.jpeg" alt="ilya-pavlov-OqtafYT5kTw-unsplash.jpg"></p>
<p>Getting started in programming is quite fun and the best part is you can write codes at any age! wonderful right? Maybe you can get a little confused at the initial stage regarding what language is the best for you to learn. But believe me, after some time you are going to enjoy typing every single line of code and their outcomes.</p>
<blockquote>
<p>Whether you want to uncover the secrets of the universe, or you just want to pursue a career in the 21st century, basic computer programming is an essential skill to learn.” - <em>Stephen Hawking</em></p>
</blockquote>
<p>As they say, the best time to start is now, don&#39;t procrastinate, as long you have the passion and determination their nothing difficult to learn.</p>
<pre><code class="lang-c"><span class="hljs-comment">// "Hello World!" in C</span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>{
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello World!"</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Language maybe is different but &quot;Hello world!&quot; always be there for showing you the richness of that language.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Simple NodeJS Server Application with "Hello World!"</span>

<span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>)
<span class="hljs-keyword">const</span> app = express()
<span class="hljs-keyword">const</span> port = process.env.PORT || <span class="hljs-number">3000</span>

app.get(<span class="hljs-string">'/'</span>, (req, res) =&gt; res.send(<span class="hljs-string">'Hello World!'</span>))

app.listen(port, () =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Express app listening on port <span class="hljs-subst">${port}</span>!`</span>))
</code></pre>
<p>There is no better way to start programming than to start. The most recommended thing is to learn the basics, and for this, the best thing is to rack your brains trying to make a concrete project or an application that you want to create or develop. Having a specific goal helps a lot.</p>
<blockquote>
<p><strong>TL;DR</strong> - It has been a long one and remember to <em>keep reading code, keep writing code, do not forget to try what it does</em>. Once comfortable in a language, moving on to the next will be easier and will bring you new skills.</p>
</blockquote>
<p><code>Ufff!</code> In this &quot;Hello World!&quot; myth I forgot that this is actually my first post here, so I have to introduce myself to you or maybe it&#39;s my own &quot;Hello World!&quot;.</p>
<h2 id="about-me">About Me</h2>
<p>Hello! my name is Ganesh Agrawal. If you want to know more about me you can see <a target='_blank' rel='noopener noreferrer'  href="https://www.linkedin.com/in/iamganeshagrawal/">here</a>, you must be new here, so I decided to write my first post talking a little about my motivations to have a blog.</p>
<p>I intend to share the knowledge that I have been acquiring as a way to validate, memorize, and help other beginners who are going through the same problems and doubts.</p>
<p>Also, I am thinking about post web development tips mainly, a little bit of theory about what I&#39;ve been studied in my academic life, reviews of technology books, events, courses, and whatever else comes to mind.</p>
<p>So that&#39;s it, feel free, pour yourself a coffee, and wait for new posts to exchange ideas about the world <strong><code>dev</code></strong>.</p>
]]></content:encoded></item></channel></rss>