<?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[Tushar Khatri Blog | Python | Backend | Full-stack | Node.js | Express.js]]></title><description><![CDATA[Tushar Khatri blogs about nodejs, expressjs, mongodb, firebase, python, dart ,flutter and other Software technologies.
If you are a coder/programmer or a student his blogs will help you in many ways.]]></description><link>https://blog.tusharkhatri.in</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 21:02:52 GMT</lastBuildDate><atom:link href="https://blog.tusharkhatri.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Use Environment Files (.env) in Node.js | Tushar Khatri]]></title><description><![CDATA[When I was learning back-end development I encountered some major problems while hosting my apps, one of these was how to upload the code to a remote repository and at the same time prevent my secrets from being exposed.

Data such as API keys or Dat...]]></description><link>https://blog.tusharkhatri.in/how-to-use-environment-variables-in-nodejs</link><guid isPermaLink="true">https://blog.tusharkhatri.in/how-to-use-environment-variables-in-nodejs</guid><category><![CDATA[how to use .env files]]></category><category><![CDATA[nodejs tutorial]]></category><category><![CDATA[env files tutorial]]></category><category><![CDATA[environmental variables tutorial]]></category><category><![CDATA[what are .env files in programming]]></category><dc:creator><![CDATA[Tushar Khatri]]></dc:creator><pubDate>Fri, 28 Oct 2022 14:54:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1666968180750/0GtlhlXii.gif" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When I was learning back-end development I encountered some major problems while hosting my apps, one of these was how to upload the code to a remote repository and at the same time prevent my secrets from being exposed.</p>
<blockquote>
<p>Data such as <code>API keys</code> or <code>Database URI</code> or any key from cloud services like <code>AWS</code> or <code>Google Cloud</code> are counted as secrets and are not to be exposed under any circumstance.</p>
</blockquote>
<p>We need to learn about 2 things in order to keep our secrets unexposed i.e.</p>
<ul>
<li>dotenv or .env</li>
<li>.gitignore</li>
</ul>
<h3 id="heading-dotenv">dotenv</h3>
<p><a target="_blank" href="https://www.npmjs.com/package/dotenv">dotenv</a> is a <a target="_blank" href="https://www.npmjs.com/">npm</a> package, it helps us store secrets in a separate file called <code>.env</code></p>
<ul>
<li>Run <code>npm i dotenv</code> from the root of the project to install the package.</li>
<li>import and configure dotenv package in your project <code>require('dotenv').config();</code>. Make sure to require it at the very top of your app.js file so that it can be used throughout the project.</li>
<li>Create a <code>.env</code> file in your root and add all the secrets in this file as shown below:</li>
</ul>
<pre><code class="lang-shell">API_KEY=anyApiKey
SECRET_KEY=YOURSECRETKEYGOESHERE # comment
SECRET_HASH="something-with-a-#-hash"
</code></pre>
<p>You must use double quotes when reserved keywords like hash are appearing in the secret.</p>
<ul>
<li>Finally, you can use these secrets anywhere in your project by using <code>process.env.API_KEY</code>.</li>
</ul>
<p>you just got to add your variable name after <code>process.env</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//import and configure dotenv at the very top of your file.</span>
<span class="hljs-built_in">require</span>(<span class="hljs-string">'dotenv'</span>).config();

app.get(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
      <span class="hljs-keyword">const</span> apiKey = process.env.API_KEY;
      <span class="hljs-keyword">const</span> secretKey = process.env.SECRET_KEY;
      <span class="hljs-keyword">const</span> secretHash = process.env.SECRET_HASH;
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Secrets stored in .env file are <span class="hljs-subst">${apiKey}</span> and <span class="hljs-subst">${secretKey}</span> and <span class="hljs-subst">${secretHash}</span>`</span>);
});
</code></pre>
<h3 id="heading-gitignore-exclude-unwanted-files-from-git-tracking"><code>.gitignore</code>, exclude unwanted files from git tracking.</h3>
<p>Now once you have your secrets in place you need to make sure that these variables must not appear in your commit history. Whenever you commit a change <code>git</code> saves all the newly added files and lines of code.  We can ignore tracking of these files by following the steps below:</p>
<ul>
<li>Create a <code>.gitignore</code> file in the root of your project.</li>
<li>Add the file or folder name you want to ignore.</li>
</ul>
<pre><code class="lang-gitignore">/node_modules
.env
</code></pre>
<p>Adding <code>.env</code> in the <code>.gitignore</code> file tells <code>git</code> to ignore this file and any changes made to this file. Thus, making your secrets real secrets. Likewise by adding <code>/node_modules</code> git will ignore the whole folder.</p>
<h3 id="heading-how-to-run-your-apps-without-uploading-env-file-to-the-cloud-or-hosting-platform">How to run your apps without uploading <code>.env</code> file to the cloud or hosting platform?</h3>
<p>Every hosting provider has a tab called <code>Variable</code> where you can add the secrets and your app will have access to them.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666965817910/qlIVAi3lL.png" alt="dotenv in nodejs | Tushar Khatri.png" /></p>
<p>You don't need to upload <code>.env</code> file anywhere, we use the <code>.env</code> file to access and store our secrets separately in our local environment and to safeguard them while uploading code to remote repos.</p>
<p>If this article was helpful please consider following me on <a target="_blank" href="https://instagram.com/tusharkhatri.in">Instagram</a> or <a target="_blank" href="https://github.com/tusharkhatriofficial">GitHub</a>. If you wish to find more such articles in your inbox please consider subscribing to my <a target="_blank" href="https://blog.tusharkhatri.in/newsletter">newsletter</a>. Your appreciation is my fuel 😌.</p>
]]></content:encoded></item><item><title><![CDATA[Send automated emails from your node.js app | Google OAuth2 and Nodemailer.]]></title><description><![CDATA[I was searching for a viable option for the contact form of my personal website, believe me this is the best you can do for your forms.
Get all the required secrets from Google Developer Console.
Before doing anything in our code we have to first cre...]]></description><link>https://blog.tusharkhatri.in/send-automated-emails-nodejs-oauth2-nodemailer</link><guid isPermaLink="true">https://blog.tusharkhatri.in/send-automated-emails-nodejs-oauth2-nodemailer</guid><category><![CDATA[automated emails]]></category><category><![CDATA[nodemailer]]></category><category><![CDATA[OAuth2]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[googleapis]]></category><dc:creator><![CDATA[Tushar Khatri]]></dc:creator><pubDate>Wed, 26 Oct 2022 14:22:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1666793093222/9F7lwooyt.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I was searching for a viable option for the contact form of my personal website, believe me this is the best you can do for your forms.</p>
<h1 id="heading-get-all-the-required-secrets-from-google-developer-console">Get all the required secrets from Google Developer Console.</h1>
<p>Before doing anything in our code we have to first create a project in the <a target="_blank" href="https://console.developers.google.com/">Google Developers Console</a>.</p>
<h3 id="heading-create-project">Create Project.</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666765066614/cpDweiQpH.png" alt="Screenshot 2022-10-26 at 11.41.58 AM.png" /></p>
<h3 id="heading-rename-the-project">Rename the project.</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666765347345/kbTl9TgYA.png" alt="Screenshot 2022-10-26 at 11.51.19 AM.png" /></p>
<h3 id="heading-configure-the-oauth-consent-screen-in-the-test-mode">Configure the OAuth consent screen in the <code>Test Mode</code>.</h3>
<blockquote>
<p>You may need to verify your website to make it work in production mode as test secrets are only valid for 7 days.</p>
</blockquote>
<ul>
<li>Select User Type as External and click CREATE.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666765679111/lMkzenIqS.png" alt="Screenshot 2022-10-26 at 11.53.38 AM.png" /></p>
<ul>
<li>Fill only the mandatory fields for now as we want to stay in the test mode.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666766195922/8u8JPV8jT.png" alt="Screenshot 2022-10-26 at 12.04.27 PM.png" />
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666766208360/SeT3GGM-i.png" alt="Screenshot 2022-10-26 at 12.05.04 PM.png" /></p>
<ul>
<li>Make no changes to the Scopes, click SAVE AND CONTINUE.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666766493306/vp5Bq6_lJ.png" alt="Screenshot 2022-10-26 at 12.10.06 PM.png" /></p>
<ul>
<li>Enter the email address you want to send automatic emails from.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666766929879/ix5d3Dcc7.png" alt="Screenshot 2022-10-26 at 12.14.24 PM.png" /></p>
<ul>
<li>Check the Summary and click BACK TO DASHBOARD.</li>
</ul>
<h3 id="heading-create-credentials">Create Credentials.</h3>
<ul>
<li>Navigate to the <code>Credentials</code> tab on the sidebar and click on <code>+ CREATE CREDENTIALS</code> and <code>OAuth client ID</code>.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666767821333/dkg0PpcdB.png" alt="Screenshot 2022-10-26 at 12.32.10 PM.png" /></p>
<ul>
<li>Fill all the mandatory fields and set <code>Authorized redirect URIs</code> to <code>https://developers.google.com/oauthplayground</code>.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666768172373/QG6Z5x8Vj.png" alt="Screenshot 2022-10-26 at 12.38.35 PM.png" /></p>
<ul>
<li>Copy your <code>Client Id</code> and <code>Client Secret</code> to a safe place as we will use them later.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666768425701/2luj0MUUL.png" alt="Screenshot 2022-10-26 at 12.41.17 PM.png" /></p>
<h3 id="heading-authorize-api">Authorize API</h3>
<ul>
<li>Head over to <code>https://developers.google.com/oauthplayground</code> and fill in the recently generated <code>Client Id</code> and <code>Client Secret</code> in the <code>OAuth 2.0 configuration</code> shown below.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666769530319/l06od8jUl.png" alt="Screenshot 2022-10-26 at 12.59.01 PM.png" /></p>
<ul>
<li>fill in <code>https://mail.google.com</code> in the <code>Authorize API's field</code> and click <code>Authorize API's</code>.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666770011781/-AQvO_rZb.png" alt="Screenshot 2022-10-26 at 1.09.39 PM.png" /></p>
<p>As you hit <code>Authorize API's</code> button, Google will ask you to verify your Gmail Id. Make sure to verify the Id you entered as the <code>Test User</code> while configuring the consent screen.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666771046683/TaywJB7bN.png" alt="Screenshot 2022-10-26 at 1.14.38 PM.png" />
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666771291617/EjODr4Lv4.png" alt="Screenshot 2022-10-26 at 1.15.18 PM.png" />
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666771311526/6pSXp6gSb.png" alt="Screenshot 2022-10-26 at 1.15.52 PM.png" /></p>
<ul>
<li>Click on <code>Exchange Authorization code for tokens</code> and copy the <code>Refresh token</code> from here.</li>
</ul>
<p>We don't need to copy the <code>Access token</code> from here as it is not permanent and keeps on expiring. We will generate an <code>Access token</code> on the fly as and when needed from our node app.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666771328531/8MD3NDz8R.png" alt="Screenshot 2022-10-26 at 1.16.39 PM.png" /></p>
<h3 id="heading-create-a-env-file-in-the-root-folder-of-your-project-and-include-all-the-secrets-listed-below-in-the-same-format">Create a <code>.env</code> file in the root folder of your project and include all the secrets listed below in the same format.</h3>
<blockquote>
<p>you have to install and configure <code>dotenv</code> package to make this work. If you are not sure what that is, please check the <a target="_blank" href="https://www.freecodecamp.org/news/how-to-use-node-environment-variables-with-a-dotenv-file-for-node-js-and-npm/#:~:text=To%20use%20DotEnv%2C%20first%20install,config()%20.">installation guide here!</a></p>
</blockquote>
<pre><code class="lang-shell">CLIENT_ID="clientIdFromGoogleDeveloperConsole"
CLIENT_SECRET="clientSecretFromGoogleDeveloperConsole"
REDIRECT_URI="https://developers.google.com/oauthplayground"
REFRESH_TOKEN="refreshTokenFromGoogleDeveloperConsole"
</code></pre>
<h3 id="heading-create-a-form-with-the-post-method">Create a form with the <code>post</code> method.</h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">"/submit"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"post"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"name"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Your Name"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"name"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"email"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"email"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Email"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"email"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"message"</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">"6"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"message"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Message"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<h3 id="heading-install-the-required-packages">Install the required packages</h3>
<p>Run the following command from the root of your project to install <code>googleapis</code> and <code>nodemailer</code> <a target="_blank" href="https://www.npmjs.com/">npm</a> packages.</p>
<pre><code class="lang-shell">npm i googleapis nodemailer
</code></pre>
<h3 id="heading-configure-oauth2client-using-the-secrets-stored-in-the-env-file">Configure oAuth2Client using the secrets stored in the <code>.env</code> file.</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> oAuth2Client = <span class="hljs-keyword">new</span> google.auth.OAuth2(process.env.CLIENT_ID, process.env.CLIENT_SECRET, process.env.REDIRECT_URI);

oAuth2Client.setCredentials({ <span class="hljs-attr">refresh_token</span>: process.env.REFRESH_TOKEN});
</code></pre>
<h3 id="heading-handle-post-request-and-write-the-function-to-send-emails">Handle <code>post</code> request and write the function to send emails</h3>
<p>The form we created recently will send a <code>post</code> request to our server with the required form data, we will store this form data in Javascript constants.</p>
<pre><code class="lang-javascript">app.post(<span class="hljs-string">"/submit"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
       <span class="hljs-keyword">const</span> sender name = req.body.name;
       <span class="hljs-keyword">const</span> senderEmail = req.body.email;
       <span class="hljs-keyword">const</span> senderMessage = req.body.message;
});
</code></pre>
<p>We will now write a function that takes <code>senderName, senderEmail, senderMessage</code> as parameters. </p>
<ul>
<li>Create a try-catch block inside the function</li>
<li>In the try block generate the ACCESS_TOKEN on the fly using the <code>oAuth2Client.getAccessToken();</code></li>
<li>Create a  transport using <code>nodemailer.createTransport({});</code></li>
<li>Create an object <code>mailOptions</code> with the following key-value pairs.<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> mailOptions = {
    <span class="hljs-attr">from</span>: <span class="hljs-string">""</span>,
    <span class="hljs-attr">to</span>: <span class="hljs-string">""</span>,
    <span class="hljs-attr">subject</span>: <span class="hljs-string">``</span>,
    <span class="hljs-attr">text</span>: <span class="hljs-string">``</span>,
  }
</code></pre>
</li>
<li>Use <code>transport.sendMail(mailOptions);</code> to send the email.</li>
<li>Catch the error if any in the catch block.</li>
</ul>
<h3 id="heading-this-is-how-the-function-must-look-after-putting-everything-together">This is how the function must look after putting everything together.</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sendMail</span>(<span class="hljs-params">senderName, senderEmail, senderMessage</span>)</span>{
  <span class="hljs-keyword">try</span>{
    <span class="hljs-keyword">const</span> ACCESS_TOKEN = <span class="hljs-keyword">await</span> oAuth2Client.getAccessToken();
    <span class="hljs-keyword">const</span> transport = nodemailer.createTransport({
      <span class="hljs-attr">service</span>: <span class="hljs-string">'gmail'</span>,
      <span class="hljs-attr">auth</span>: {
        <span class="hljs-attr">type</span>: <span class="hljs-string">'OAuth2'</span>,
        <span class="hljs-attr">user</span>: <span class="hljs-string">'developersonline.org@gmail.com'</span>,
        <span class="hljs-attr">clientId</span>: process.env.CLIENT_ID,
        <span class="hljs-attr">clientSecret</span>: process.env.CLIENT_SECRET,
        <span class="hljs-attr">refreshToken</span>: process.env.REFRESH_TOKEN,
        <span class="hljs-attr">accessToken</span>: ACCESS_TOKEN,
      },
    });

    <span class="hljs-keyword">const</span> mailOptions = {
      <span class="hljs-attr">from</span>: <span class="hljs-string">"Bot &lt;developersonline.org@gmail.com&gt;"</span>,
      <span class="hljs-attr">to</span>: <span class="hljs-string">"hello@tusharkhatri.in"</span>,
      <span class="hljs-attr">subject</span>: <span class="hljs-string">`<span class="hljs-subst">${senderEmail}</span> sent you a message`</span>,
      <span class="hljs-attr">text</span>: <span class="hljs-string">`Message from <span class="hljs-subst">${senderName}</span>: <span class="hljs-subst">${senderMessage}</span>`</span>,
    }

    <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> transport.sendMail(mailOptions);
    <span class="hljs-keyword">return</span> result;


  }<span class="hljs-keyword">catch</span> (error) {
    <span class="hljs-keyword">return</span> error;
  }
}
</code></pre>
<h3 id="heading-call-the-function">Call the function. ✌️</h3>
<pre><code class="lang-javascript">app.post(<span class="hljs-string">"/submit"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
       <span class="hljs-keyword">const</span> sender name = req.body.name;
       <span class="hljs-keyword">const</span> senderEmail = req.body.email;
       <span class="hljs-keyword">const</span> senderMessage = req.body.message;

       sendMail(senderName, senderEmail, senderMessage)
       .then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span>  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Message Sent"</span>))
       .catch(<span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(error.message));
});
</code></pre>
<p>If this article was helpful please consider following me on <a target="_blank" href="https://instagram.com/tusharkhatri.in">Instagram</a> or <a target="_blank" href="https://github.com/tusharkhatriofficial">GitHub</a>. If you wish to find more such articles in your inbox please consider subscribing to my <a target="_blank" href="https://blog.tusharkhatri.in/newsletter">newsletter</a>. Your appreciation is my fuel 😌.</p>
]]></content:encoded></item><item><title><![CDATA[Dart Programming Language: Why do I love it?]]></title><description><![CDATA[There are several options when it comes to programming languages, and deciding which one to use entails a lot of considerations. Choosing the right programming languages is half the problem done, some may choose Java, some prefer Python, while some o...]]></description><link>https://blog.tusharkhatri.in/why-do-i-love-dart-lang</link><guid isPermaLink="true">https://blog.tusharkhatri.in/why-do-i-love-dart-lang</guid><category><![CDATA[Dart]]></category><category><![CDATA[#dart language]]></category><category><![CDATA[dart programming tutorial]]></category><category><![CDATA[#dart-for-beginners]]></category><category><![CDATA[Flutter]]></category><dc:creator><![CDATA[Tushar Khatri]]></dc:creator><pubDate>Sat, 22 Oct 2022 06:35:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1666419686491/Xj8xRmF4D.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>There are several options when it comes to programming languages, and deciding which one to use entails a lot of considerations. Choosing the right programming languages is half the problem done, some may choose Java, some prefer Python, while some others stick to C, depending on what they want the end result to be. In certain scenarios, there is some amount of developer freedom when it comes to programming languages and frameworks.</p>
<p>Dart is one such programming language that developers vouch for. In this article, we will be discussing Dart, a programming language developed by Google developers in 2011.</p>
<h1 id="heading-what-is-dart">What is Dart?</h1>
<p>Dart is a high-level, interpreted programming language that lets you build mobile, server, desktop and web applications. Some people even consider it to be an alternative solution to Javascript. Though the first version of Dart was available by mid-autumn, its popularity grew when Flutter (ideal choice for many for cross-platform development) was released in 2017.</p>
<p>A major difference that Dart has from other programming languages is that it comes with its own package manager known as Pub; developers can use these packages to build Dart and Flutter apps.</p>
<p>If you’ve heard of Dart, but still not sure about its key benefits, read on:</p>
<h2 id="heading-1-easy-to-learn-language">1. Easy to learn language</h2>
<p>Dart is a fairly easy language to learn, and Google developers have put in a tremendous effort in the documentation part. With its Java-like syntax, developers with OOPS background can quickly plunge into programming if they know the basics. Dart also allows for easy editing as they can test small sections of code even if the complete application is not ready yet. Dart is fairly easy to grasp, modern, functional, flexible and competitive. The ecosystem is simple, understanding the terminologies, the proper tools and SDKs for the language is easy, and accessing the frameworks and libraries is easier. If a developer is familiar with any programming language, not just necessarily an OOP language, they can intuitively start using Dart.</p>
<h2 id="heading-2-comes-with-good-documentation">2. Comes with good documentation</h2>
<p>Developers find that Dart is a good first programming language to learn because it has an excellent introduction and very good documentation. Getting started is also easy; just type the Dartpad url, and you can get started. More and more people have switched to Dart, thanks to its simple syntax, excellent community support, easy features that guides developers when they are in the training process.</p>
<h2 id="heading-3-high-performance-factor">3. High performance factor</h2>
<p>Applications run in Dart run faster than in other programming languages. And features like JIT compilation and AOT compilation add to the performance feature of Dart. JIT compilation or Just in Time compilation helps you to enable hot reloads, while AOT or Ahead of Time compilation helps with fast startup and better execution of the app.</p>
<h2 id="heading-4-dart-syntax-is-clean">4. Dart syntax is clean</h2>
<p>Dart looks almost similar to Java as it has clean syntax. So developers can pick up the code easily, but there is a chance they could get confused with many Dart language features.</p>
<h2 id="heading-5-excellent-tooling-support">5. Excellent tooling support</h2>
<p>The programming language has incredible tools to support app development.</p>
<p>While looking at the advantages, you must be aware of the drawbacks as well, so as to help make a wiser decision.</p>
<h2 id="heading-6-can-compile-to-self-contained-snapshots">6. Can compile to self-contained snapshots</h2>
<p>This feature is possible with other languages, but it is fast and simple with Dart. The Dart scripts can compile to self-contained snapshots on its own, i.e without requiring any other programs or libraries</p>
<h2 id="heading-7-can-write-the-first-program-without-installation-or-configuration">7. Can write the first program without installation or configuration</h2>
<p>Dart comes with DartPad, a very simple interface, eliminating the need of installation or configuration. Just write the code, and click on Run command to execute the code. There is support for libraries, but it’s restricted to basic level.</p>
<h2 id="heading-8-a-good-support-for-the-programmer">8. A good support for the programmer</h2>
<p>Programmers can opt to treat Dart as an ordinary, dynamically typed language, especially if they do not want to deal with type systems at all. So Dart is an optionally typed language. Developers can also benefit from the extra documentation that comes along with the type annotations in the code. Dart alerts the programmers about possible type inconsistencies and oversights and not errors. These alerts are calibrated to support the developers.</p>
<h2 id="heading-9-more-type-safe-than-javascript">9. More type-safe than Javascript</h2>
<p>If you compare Dart with Javascript, the former has a few advantages in certain aspects. For example, Javascript is not a type-safe language. It is only during the run-time, will you see the programming errors. On the other hand, Dart supports both strong and loose prototyping, where you can see the programming errors during compilation. So it is more type-safe than JS.</p>
<h2 id="heading-10-dart-is-portable">10. Dart is portable</h2>
<p>There is no need for any specific hardware configurations or architecture for running Dart, as it functions on any operating system and in all web browsers.</p>
<h3 id="heading-tools-that-integrate-with-dart">Tools that integrate with Dart:</h3>
<ul>
<li>Dart Webdev — This is a framework for building web apps</li>
<li>Angular Dart — Acts as an alternative to WebDev</li>
<li>Aqueduct — Is a multi-threaded server-side framework</li>
<li>Flutter — Google’s Cross-platform mobile UI framework</li>
</ul>
<h1 id="heading-familiarize-yourself-with-some-risks-as-well">Familiarize yourself with some risks as well</h1>
<p>There are some disadvantages to Dart that you must be aware of:</p>
<h2 id="heading-the-dart-is-still-developing">The Dart is still developing</h2>
<p>That’s not exactly a risk, but it is important that you know that things won’t be documented correctly and there is a chance the API will change at any time.</p>
<h2 id="heading-absence-of-native-support">Absence of native support</h2>
<p>In order to see the complete demo of Dart’s capabilities, the developers have to bring it to users’ browsers for lack of browser support. So they have to provide native support for all the browsers they are targeting the application to work.</p>
<h2 id="heading-it-is-not-easy-to-find-a-dart-programmer-sometimes">It is not easy to find a Dart programmer sometimes</h2>
<p>Since Dart is a fairly new program, there aren’t many developers around, yet. So if you look for a Dart programmer, or a job that requires such a programmer, you may find the search to be hard.</p>
<p>So, Dart wasn’t as popular as React Native until Flutter was introduced. When Flutter became popular, Dart also became popular, almost as popular as React Native. And this combination is used by big brands like Google Ads, BMW, Tencent, Groupon, eBay etc. A good choice when businesses need to make cross-platform apps, perfect for startups, small projects, big brands, etc.</p>
<p>With Dart in the picture, you can write a web client, mobile app and backend in the same language, database applications and scripting and all the things you love about Java and C#. There are some stark differences between Dart and C#, but it is a favourite choice among developers, as it is a multi-platform, general purpose language.</p>
<p>Backed by Google, Dart is a good choice for command-line applications, is scalable across projects, amazingly similar to Javascript. Though a simple language, Dart has enough features to build complex applications as well, using modern paradigms. So if you are planning to use both Flutter and Dart in building your applications, it would be a good choice.</p>
<p>If the above information was helpful in any way please consider following me on GitHub and subscribing to my newsletter . 🙂👏</p>
<p>Thanks,<br />
Tushar Khatri</p>
]]></content:encoded></item><item><title><![CDATA[px vs em vs rem vs %. Which one do I use while designing web pages?]]></title><description><![CDATA[There are more than a few units to size an element using CSS. You might have wondered which one to use. let me help you choose the right one for your needs.
Difference Between PX, EM, REM, and %
let me make it simple for you:
16px = 1em = 1rem = 100%...]]></description><link>https://blog.tusharkhatri.in/difference-between-px-em-rem</link><guid isPermaLink="true">https://blog.tusharkhatri.in/difference-between-px-em-rem</guid><category><![CDATA[rem]]></category><category><![CDATA[#em]]></category><category><![CDATA[px]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[CSS3]]></category><dc:creator><![CDATA[Tushar Khatri]]></dc:creator><pubDate>Tue, 24 May 2022 03:00:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1666503437440/yeK3zB1aj.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>There are more than a few units to size an element using CSS. You might have wondered which one to use. let me help you choose the right one for your needs.</p>
<h2 id="heading-difference-between-px-em-rem-and">Difference Between PX, EM, REM, and %</h2>
<p>let me make it simple for you:</p>
<p><code>16px = 1em = 1rem = 100%</code></p>
<p>If you are here to know about the conversion formula. 16px is generally the base size, and I’m considering 16px as the base size here:</p>
<p><strong>pixels to convert</strong> ÷ <strong>16 = em</strong></p>
<p>For example, If you are willing to convert <code>40px</code> to <code>em</code> and the base size is <code>16px</code> then:</p>
<p><code>40÷16 = 2.5em</code></p>
<p>Now If you are still with me probably you want to learn about the core differences between these two.</p>
<p><strong>Core Difference:</strong></p>
<p>I will talk about <code>rem</code> later in this blog, for the time being, consider <code>em</code> and <code>rem</code> as the same units. So basically, <code>px</code> is a static unit but <code>em</code>, and <code>%</code> are dynamic units, let us understand this by an example.</p>
<p>Let me create an <code>index.html</code> file and link it with a CSS file</p>
<p>I will define the size of the text in the paragraph tag in pixels using CSS.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {  
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">40px</span>;  
}

<span class="hljs-selector-tag">p</span> {  
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;  
}
</code></pre>
<p>and the output looks something like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666500980240/B2lan3W7p.png" alt="a web page showing the output of html and css code" /></p>
<p>Now let’s head over to the <code>chrome://settings/appearance</code> and change the font size to very large.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666500982171/uWyOSxGxF.png" alt="Changing the font size in chrome://settings/appearance by Tushar Khatri" /></p>
<p>Now you will notice that the size of the Paragraph tag is not increasing even if we change the default font size and this is because pixel is a static unit and it stays as it is until we zoom in on the web page using <code>Ctrl</code> and <code>+</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666500984511/J0khFNB_X.gif" alt /></p>
<p>Did you notice something unusual? I can see something changing its size. Oh! it is the <code>h1</code> tag, now all this is happening because the size of the <code>h1</code> tag is pre-defined in <code>em</code>, and as I told you <code>em</code> is a <strong>dynamic unit</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666500986132/VDWoRmyI3.png" alt="default css rules of h1 tag by Tushar Khatri" /></p>
<p>It is considered better to design your website using dynamic units as it helps people with visual impairments to scale up the text and makes your website much more reliable. <code>%</code> is also a dynamic unit. Which one you will use? well, it is up to you now.</p>
<h3 id="heading-what-is-rem">What is rem?</h3>
<p>Let me be quick. Let’s take an example:</p>
<p><code>index.html</code></p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>  
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> <span class="hljs-attr">dir</span>=<span class="hljs-string">"ltr"</span>&gt;</span>  
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>The Difference Between PX, EM, REM, %<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"css/styles.css"</span>&gt;</span>  
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>  
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Lorem ipsum dolor sit amet<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>  
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>  
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>  
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><code>css/styles.css</code></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {  
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2em</span>;  
}

<span class="hljs-selector-class">.container</span> {  
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">40px</span>;  
}

<span class="hljs-selector-tag">p</span> {  
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">3em</span>;  
}
</code></pre>
<p>In the above example, we have defined font size as <code>2em</code> for the body and <code>3em</code> for the paragraph tag. Now the sum of <code>2em</code> and <code>3em</code> is <code>5em</code> and this is what is gonna be displayed on the web page.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666500987650/QKakaelzE.png" alt="web page with em and rem example" /></p>
<p><strong>Even though the <code>2em</code> is crossed out, it still applies! making it a total of <code>5em</code>.</strong></p>
<p>so, <code>rem</code> stands for <strong>root em,</strong> and using <code>rem</code> we can override the size defined for the body instead of inheriting it.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {  
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2em</span>;  
}

<span class="hljs-selector-class">.container</span> {  
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">40px</span>;  
}

<span class="hljs-selector-tag">p</span> {  
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">3rem</span>;  
}
</code></pre>
<p>After making the above changes the paragraph will have a font size of <code>3em</code> only.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666500989030/bd5lKgt7t.png" alt="rem in css" /></p>
<p><em>If this article was helpful please consider following me on <a target="_blank" href="https://github.com/tusharkhatriofficial">GitHub</a> and subscribing to my <a target="_blank" href="https://blog.tusharkhatri.in/newsletter">newsletter</a>. Your appreciation is my fuel. Namaste guys🙏</em>.</p>
]]></content:encoded></item></channel></rss>