<?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>Forem: Umesh Yadav</title>
    <description>The latest articles on Forem by Umesh Yadav (@umesh).</description>
    <link>https://forem.com/umesh</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%2F462336%2Ffe75dae2-ca31-4999-8a10-17bb44c1b04a.jpeg</url>
      <title>Forem: Umesh Yadav</title>
      <link>https://forem.com/umesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/umesh"/>
    <language>en</language>
    <item>
      <title>How to Use SVG Icon as AvatarBadge in Chakra UI</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Sat, 07 Nov 2020 18:39:46 +0000</pubDate>
      <link>https://forem.com/umesh/how-to-use-svg-icon-as-avatarbadge-in-chakra-ui-4agf</link>
      <guid>https://forem.com/umesh/how-to-use-svg-icon-as-avatarbadge-in-chakra-ui-4agf</guid>
      <description>&lt;p&gt;I was recently building a side project and I was using Chakra UI + Next.js. I wanted to put the Google logo as a Badge in the bottom right of an Avatar Image. Something like this👇🏻.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604769488117%2FK9QW6laaG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604769488117%2FK9QW6laaG.png" alt="Screenshot 2020-11-07 at 10.47.55 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Chakra UI provides an &lt;code&gt;Avatar&lt;/code&gt; and &lt;code&gt;AvatarBadge&lt;/code&gt; component. You can use these two to create an avatar image with a badge(bottom right). Here is an example from Chakra UI docs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604770379784%2FOxP9Dv8qq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604770379784%2FOxP9Dv8qq.png" alt="Screenshot 2020-11-07 at 11.02.54 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now suppose instead of the dot you want to insert an SVG icon. Here is how you can do it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Avatar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;AvatarBadge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Icon&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@chakra-ui/core&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;GoogleIcon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;svg&lt;/span&gt; &lt;span class="nx"&gt;viewBox&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0 0 533.5 544.3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;
      &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;M533.5 278.4c0-18.5-1.5-37.1-4.7-55.3H272.1v104.8h147c-6.1 33.8-25.7 63.7-54.4 82.7v68h87.7c51.5-47.4 81.1-117.4 81.1-200.2z&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#4285f4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;
      &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;M272.1 544.3c73.4 0 135.3-24.1 180.4-65.7l-87.7-68c-24.4 16.6-55.9 26-92.6 26-71 0-131.2-47.9-152.8-112.3H28.9v70.1c46.2 91.9 140.3 149.9 243.2 149.9z&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#34a853&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;
      &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;M119.3 324.3c-11.4-33.8-11.4-70.4 0-104.2V150H28.9c-38.6 76.9-38.6 167.5 0 244.4l90.4-70.1z&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#fbbc04&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;
      &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;M272.1 107.7c38.8-.6 76.3 14 104.4 40.8l77.7-77.7C405 24.6 339.7-.8 272.1 0 169.2 0 75.1 58 28.9 150l90.4 70.1c21.5-64.5 81.8-112.4 152.8-112.4z&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#ea4335&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/svg&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Avatar&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AvatarBadge&lt;/span&gt; &lt;span class="nx"&gt;boxSize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1.25em&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;borderColor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;transparent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;bg&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;white&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Icon&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;GoogleIcon&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/AvatarBadge&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Avatar&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604769488117%2FK9QW6laaG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604769488117%2FK9QW6laaG.png" alt="Screenshot 2020-11-07 at 10.47.55 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, There is &lt;code&gt;GoogleIcon&lt;/code&gt; SVG. Then I am creating an &lt;code&gt;Icon&lt;/code&gt; component using &lt;code&gt;GoogleIcon&lt;/code&gt; SVG. Then we pass this &lt;code&gt;Icon&lt;/code&gt; component as children to &lt;code&gt;AvatarBadge&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, if you want to create a Badge with an image. You can just replace the &lt;code&gt;Icon&lt;/code&gt; component with &lt;code&gt;Image&lt;/code&gt; and it will render the image as a badge.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Avatar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;AvatarBadge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@chakra-ui/core&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Avatar&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AvatarBadge&lt;/span&gt; &lt;span class="nx"&gt;boxSize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1.25em&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;borderColor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;transparent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://raw.githubusercontent.com/chakra-ui/chakra-ui/develop/logo/logomark-colored%402x.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/AvatarBadge&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Avatar&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604771234282%2F0bmbW5k4V.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604771234282%2F0bmbW5k4V.png" alt="Screenshot 2020-11-07 at 11.16.58 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Chakra UI is an awesome library that provides modular components. You can use this library as a building block for creating custom components. It was very simple to build custom components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter &lt;a href="https://twitter.com/imuesh18" rel="noopener noreferrer"&gt;@imumesh18&lt;/a&gt;. Also please follow me here and on Twitter for future blogs which I write over &lt;a href="https://blog.umesh.wtf" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>css</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How File-System Based Routing Works in Next.js</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Fri, 06 Nov 2020 19:18:29 +0000</pubDate>
      <link>https://forem.com/umesh/how-file-system-based-routing-works-in-next-js-2eif</link>
      <guid>https://forem.com/umesh/how-file-system-based-routing-works-in-next-js-2eif</guid>
      <description>&lt;p&gt;Next.js has file-system based routing built-in. You don't have to explicitly define the path in the router. It's as easy as adding a file inside the folder and you are done. There are multiple ways you can define a path using the file structure. Anything we add inside the &lt;code&gt;pages&lt;/code&gt; directory is served as a path. Definitely there are exceptions like &lt;code&gt;_app.js&lt;/code&gt; and &lt;code&gt;_document.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are two types of routes in Next.js&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API Routes: Any file you put inside &lt;code&gt;pages/api&lt;/code&gt; is treated as an API endpoint and is served at &lt;code&gt;https://hostname/api/*&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Page Routes: Any file which is inside &lt;code&gt;pages&lt;/code&gt; can be served as a web page to the user, it can be a Static Generation or Server-side Rendered. Any file which is inside &lt;code&gt;pages/api&lt;/code&gt; is not treated as a web page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will go through all of these in detail in this blog post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup a Next.js Project
&lt;/h2&gt;

&lt;p&gt;We will first create a Next.js project for this. We will name this &lt;code&gt;nextjs-routing&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app
# or
yarn create next-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Page Routing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Index Routing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once your project is ready it should look something like this. You can see that there is &lt;code&gt;index.js&lt;/code&gt; inside the pages folder. So if you run your application and visit &lt;code&gt;http://localhost:3000&lt;/code&gt; you will see the welcome next.js page, which is mapped to the &lt;code&gt;index.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tree -I node_modules
.
├── README.md
├── package.json
├── pages
│   ├── _app.js
│   ├── api
│   │   └── hello.js
│   └── index.js
├── public
│   ├── favicon.ico
│   └── vercel.svg
├── styles
│   ├── Home.module.css
│   └── globals.css
└── yarn.lock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is how these pages are mapped to path by router.&lt;br&gt;
&lt;code&gt;pages/index.js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/&lt;/code&gt;&lt;br&gt;
&lt;code&gt;pages/posts/index.js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/posts/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Any &lt;code&gt;index.js&lt;/code&gt; is mapped to the directory path it belongs to.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nested Routing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now suppose you want to display deeply nested pages like dashboard settings etc.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/about.js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/about/&lt;/code&gt;&lt;br&gt;
&lt;code&gt;pages/blog/hello.js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/blog/hello&lt;/code&gt;&lt;br&gt;
&lt;code&gt;pages/blog/series/hello.js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/blog/series/hello&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Routing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose you don't have the exact route beforehand and it depends upon API data or some dynamic data. Here is how you can create these routes and map them.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/blog/[slug].js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/blog/:slug&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;over here slug can be anything and you can use &lt;code&gt;getStaticPaths&lt;/code&gt; to set the dynamic path.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/blog/[...slug].js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/blog/hello&lt;/code&gt; as well as &lt;code&gt;http://[hostname]/blog/series/hello&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The above example is to catch all route. Basically, it can map a path to any level of nesting which happens after &lt;code&gt;blog/&lt;/code&gt;. If you replace &lt;code&gt;[...slug].js&lt;/code&gt; -&amp;gt; &lt;code&gt;[[...slug]].js&lt;/code&gt;, it will match to &lt;code&gt;/blog&lt;/code&gt; along with other routes like &lt;code&gt;blog/hello&lt;/code&gt;, &lt;code&gt;blog/series/hello&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can't use catch-all routes with the directory. Eg: &lt;code&gt;pages/blog/[[..slug]]/comments.js&lt;/code&gt; will give you Error: Catch-all must be the last part of the URL.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;pages/[username]/dashboard&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/:username/dashboard&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above example, a username can be a dynamic value.  It can be used to generate user-specific static pages.&lt;/p&gt;

&lt;h2&gt;
  
  
  API Routing
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Next.js&lt;/code&gt; provides a way to directly create API. You can consume these API from anywhere inside your project. Creating an API is pretty much straight forward, you just have to create a file inside &lt;code&gt;pages/api&lt;/code&gt; and it gets mapped to &lt;code&gt;api/*&lt;/code&gt;. It won't get exposed on the client-side and will only be used on the server-side. API routing is very much similar to page routing. Let's see how this works.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Index Routing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The router will automatically route files named index to the root of the directory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/api/blog/index.js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/api/blog&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nested Routing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now suppose you want to create a deeply nested API. You can create nested folder structure files and it will get mapped in the same way.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/api/users/blog/create.js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/api/users/blog/create&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Routing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can use brackets to define the dynamic routes for your API.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/api/blog/[slug].js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/api/blog/hello&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above example, the slug can take any value. You can get this value inside your code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/api/blog/[...slug].js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/api/blog/hello&lt;/code&gt; or &lt;code&gt;http://[hostname]/api/blog/hello/world&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above example is to catch all routes. All the routes with &lt;code&gt;api/blog/*&lt;/code&gt; get routed to this.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/api/blog/[[...slug]].js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/api/blog&lt;/code&gt; or &lt;code&gt;http://[hostname]/api/blog/hello&lt;/code&gt; or &lt;code&gt;http://[hostname]/api/blog/hello/world&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can't use catch-all routes with the directory. Eg: &lt;code&gt;pages/api/blog/[[..slug]]/comments.js&lt;/code&gt; will give you Error: Catch-all must be the last part of the URL.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above example is for optional catch-all routes. Basically it makes the slug optional.&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Predefined routes take precedence over dynamic routes. So if you have &lt;code&gt;pages/api/blog/create.js&lt;/code&gt; and &lt;code&gt;pages/api/blog/[slug].js&lt;/code&gt; then &lt;code&gt;pages/api/blog/create.js&lt;/code&gt; gets mapped to &lt;code&gt;http://[hostname]/api/blog/create&lt;/code&gt; and &lt;code&gt;pages/api/blog/[slug].js&lt;/code&gt; gets mapped to &lt;code&gt;http://[hostname]/api/blog/abc&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is no way to specify which REST calls are allowed on an API route, you have to handle them from your handler code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now suppose you want to create &lt;code&gt;api/blogs/:slug/comments/:commentId&lt;/code&gt;. People you are familiar with the concept of resources in REST know there are use cases where we might need a nested resource.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pages/api/blogs/[slug]/comments/[commentId].js&lt;/code&gt; -&amp;gt; &lt;code&gt;http://[hostname]/api/blogs/:slug/comments/:commentId&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using Next.js routing is pretty much easy and straightforward and has very less overhead. It covers most of the use cases, I really couldn't think or find a use case which you won't be able to solve it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter &lt;a href="https://twitter.com/imuesh18"&gt;@imumesh18&lt;/a&gt;. Also please follow me here and on Twitter for future blogs which I write over &lt;a href="https://blog.umesh.wtf"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Linux Commands: watch</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Sun, 01 Nov 2020 20:08:48 +0000</pubDate>
      <link>https://forem.com/umesh/linux-commands-watch-176e</link>
      <guid>https://forem.com/umesh/linux-commands-watch-176e</guid>
      <description>&lt;p&gt;&lt;code&gt;watch&lt;/code&gt; is used to run a command repeatedly and display its output and error at regular intervals. This is useful when you want to run a command repeatedly and observe its output getting changed over a period of time. By default, the command is run every 2 seconds and the watch will run until interrupted(Ctrl+c). &lt;code&gt;watch&lt;/code&gt; command can be used to check the status of service, the uptime of machine, disk usage, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;watch [options] command
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see the &lt;code&gt;watch&lt;/code&gt; command in the action. We will run &lt;code&gt;watch uptime&lt;/code&gt; to check the uptime of the machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ya8W_yyi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604257023841/ZwEayKczj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ya8W_yyi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604257023841/ZwEayKczj.png" alt="Screenshot 2020-11-02 at 12.26.58 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the above image after you run the &lt;code&gt;watch&lt;/code&gt; command it will start running in the fullscreen mode. On the top left corner, you can see the refresh interval(&lt;code&gt;Every 2.0s: uptime&lt;/code&gt;) which is two seconds as we haven't specified any interval, it takes the default one.&lt;/p&gt;

&lt;p&gt;Let's see what all options we can provide to the &lt;code&gt;watch&lt;/code&gt; command.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-d, –differences: This option highlights the values which have changed between successive updates. You can also pass the &lt;code&gt;-d=permanent&lt;/code&gt; option to make the changes highlighted even if they have changed at least once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will just run &lt;code&gt;watch -d uptime&lt;/code&gt;. The output looks something like below.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eWbwy63c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604257667965/zO5CKoaiv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eWbwy63c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604257667965/zO5CKoaiv.png" alt="Screenshot 2020-11-02 at 12.37.40 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try to run &lt;code&gt;watch -d=permanent uptime&lt;/code&gt;. Now the values which are changed at least once stay highlighted throughout.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TAR1qGbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604257763094/Iszcth7zu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TAR1qGbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604257763094/Iszcth7zu.png" alt="Screenshot 2020-11-02 at 12.39.05 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-n, –interval seconds: This option specifies the refresh interval. It can't be quicker than 0.1 seconds. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can change the refresh interval using &lt;code&gt;watch -n 1 uptime&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rj-RwRES--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604258280436/2VMQKyMyf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rj-RwRES--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604258280436/2VMQKyMyf.png" alt="Screenshot 2020-11-02 at 12.47.54 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-p, --precise: This option makes sure the update fractional seconds stays (nearly) the same, as opposed to normal mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can check this command by running &lt;code&gt;watch -p date +"%T.%s"&lt;/code&gt;. Observe the time with and without the precision option you should see that with the precision flag the time difference is pretty much the same for each refresh while that's not true for normal mode.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-t, –no-title: This option is used to turn off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x6tQVYbD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604259533751/FT-w8nJnJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x6tQVYbD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604259533751/FT-w8nJnJ.png" alt="Screenshot 2020-11-02 at 1.08.49 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-b, –beep: This option will give beep if the command has a non-zero exit. &lt;/li&gt;
&lt;li&gt;-e, --errexit: This option freeze updates on command error, and exit after a keypress.&lt;/li&gt;
&lt;li&gt;-g, --chgexit: This option exit when the output of the command changes.&lt;/li&gt;
&lt;li&gt;-h, --help:  Display help text and exit.&lt;/li&gt;
&lt;li&gt;-v, --version: Display version information and exit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--699U7X4_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604259782914/lmupcJAG1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--699U7X4_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604259782914/lmupcJAG1.png" alt="Screenshot 2020-11-02 at 1.12.58 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Advance Usage
&lt;/h3&gt;

&lt;p&gt;You can also &lt;code&gt;watch&lt;/code&gt; the output of multiple commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ watch "[command] | [command]"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, the following command will print all the node process running on my machine. It uses ps to output the process and then utilizes &lt;code&gt;grep&lt;/code&gt; to filter the node process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d-jD6bEJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604260500916/OW9mYyfvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d-jD6bEJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604260500916/OW9mYyfvf.png" alt="Screenshot 2020-11-02 at 1.20.33 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;watch&lt;/code&gt; is a very powerful Linux command which you can use to monitor the output of anything you want to do repeatedly. I have used &lt;code&gt;watch&lt;/code&gt; to check daemon status while deploying service in production and while debugging crashing services in production. Do let me know how you are using the &lt;code&gt;watch&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter &lt;a href="https://twitter.com/imuesh18"&gt;@imumesh18&lt;/a&gt;. Also please follow me here and on Twitter for future blogs which I write over &lt;a href="https://blog.umesh.wtf"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>linux</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Linux Commands: env</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Thu, 29 Oct 2020 19:27:02 +0000</pubDate>
      <link>https://forem.com/umesh/linux-commands-env-2efg</link>
      <guid>https://forem.com/umesh/linux-commands-env-2efg</guid>
      <description>&lt;p&gt;&lt;code&gt;env&lt;/code&gt; can be used to print the environment variables or pass environment variables to a utility/command without setting them in the current shell session. &lt;/p&gt;

&lt;p&gt;Let's run &lt;code&gt;env&lt;/code&gt; without any argument and see what happens.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--USfeBDWI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603996605316/4qP3UTvs9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--USfeBDWI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603996605316/4qP3UTvs9.png" alt="Screenshot 2020-10-30 at 12.06.36 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It simply prints all the environment variables in the current session. You can also run &lt;code&gt;env&lt;/code&gt; with various options.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-i: Runs a command with only specified environment variables and clears all the environment variables already set.&lt;/li&gt;
&lt;li&gt;-u: Runs a command by removing the specified environment variables while keeping the other values intact.&lt;/li&gt;
&lt;li&gt;-v: Prints the verbose output for each step of processing done by the env utility.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's try to print the environment variables inside a python script by passing it via the &lt;code&gt;env&lt;/code&gt;. You can choose to run in any language of your choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'BLOG'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'PATH'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will use &lt;code&gt;env -i /usr/local/bin/python3 main.py&lt;/code&gt; to run our python script. You can't use the simple python command to run the code as the &lt;code&gt;PATH&lt;/code&gt; value which specifies the binary path for executable will also be reset. The output should be as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ env -i /usr/local/bin/python3 main.py
None
None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in the above output that all the values are set to &lt;code&gt;None&lt;/code&gt; because we have used the option &lt;code&gt;-i&lt;/code&gt;. Even the system PATH variable is set to &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's run this &lt;code&gt;env -i BLOG=hashnode /usr/local/bin/python3 main.py&lt;/code&gt; in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ env -i BLOG=hashnode /usr/local/bin/python3 main.py
hashnode
None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above output, you can see that we have set the &lt;code&gt;BLOG=hashnode&lt;/code&gt; and it gets printed in the output.&lt;/p&gt;

&lt;p&gt;You can also use &lt;code&gt;-u&lt;/code&gt; to unset certain environment variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'HOME'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'SHELL'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will use this &lt;code&gt;env -u HOME /usr/local/bin/python3 main.py&lt;/code&gt; command to run the above code. Its output looks like this. You can see that &lt;code&gt;HOME&lt;/code&gt; doesn't get printed while the &lt;code&gt;SHELL&lt;/code&gt; value does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ env -u HOME /usr/local/bin/python3 main.py
None
/bin/zsh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To print more verbose output of the &lt;code&gt;env&lt;/code&gt; command and it's step. You can pass &lt;code&gt;-i&lt;/code&gt; as an option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ env -v -u HOME /usr/local/bin/python3 main.py
#env unset:     HOME
#env executing: /usr/local/bin/python3
#env    arg[0]= '/usr/local/bin/python3'
#env    arg[1]= 'main.py'
None
/bin/zsh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;env&lt;/code&gt; in various scenarios like while debugging local code as well as some production issues to see the effect of each and every environment variable on your code or service.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter &lt;a href="https://twitter.com/imuesh18"&gt;@imumesh18&lt;/a&gt;. Also please follow me here and on Twitter for future blogs which I write over &lt;a href="https://blog.umesh.wtf"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>linux</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Linux Commands: printenv</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Sun, 18 Oct 2020 18:39:15 +0000</pubDate>
      <link>https://forem.com/umesh/linux-commands-printenv-2fop</link>
      <guid>https://forem.com/umesh/linux-commands-printenv-2fop</guid>
      <description>&lt;p&gt;&lt;code&gt;printenv&lt;/code&gt; is used to print out the environment variables. Environment variables are a common form of setting global values across a terminal session. Environment variables can be set by the systems, automation scripts, or by the user.&lt;/p&gt;

&lt;p&gt;You can print your all environment variables using just &lt;code&gt;printenv&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603040828326%2FB5CBX8Ici.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603040828326%2FB5CBX8Ici.png" alt="Screenshot 2020-10-18 at 10.37.02 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to print the value of a specific variable you can use &lt;code&gt;printenv &amp;lt;variable name&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603040908883%2FPLr8z_kOF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603040908883%2FPLr8z_kOF.png" alt="Screenshot 2020-10-18 at 10.38.24 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;If you are ever stuck and want to know what was the value of the environment variable you can use it. I have found myself using this quite often when debugging issues in production or on my local machine.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter &lt;a href="https://twitter.com/imuesh18" rel="noopener noreferrer"&gt;@imumesh18&lt;/a&gt;. Also please follow me here and on Twitter for future blogs which I write over &lt;a href="https://blog.umesh.wtf" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>linux</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Linux Commands: du</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Sat, 17 Oct 2020 19:10:02 +0000</pubDate>
      <link>https://forem.com/umesh/linux-commands-du-48pc</link>
      <guid>https://forem.com/umesh/linux-commands-du-48pc</guid>
      <description>&lt;p&gt;&lt;code&gt;du&lt;/code&gt; is used to display disk usage statistics. It's a simple tool to calculate and print the disk space used by files or directories. Let's see how to use it.&lt;/p&gt;

&lt;p&gt;You can simply type &lt;code&gt;du&lt;/code&gt; or &lt;code&gt;du &amp;lt;directory-name&amp;gt;&lt;/code&gt; in your terminal and it will print the size.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602960370350%2Fh2UKm1weA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602960370350%2Fh2UKm1weA.png" alt="Screenshot 2020-10-18 at 12.16.02 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, it gives you the size of the directory. The number &lt;code&gt;8&lt;/code&gt; represents the size in bytes.&lt;/p&gt;

&lt;p&gt;To print each and every file and their respective size we will need to use the &lt;code&gt;du -a&lt;/code&gt; command. If you just want to print the sizes in the root of the directory and don't want to go deep inside each directory you can use &lt;code&gt;du *&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602960433839%2FyxIW0IlTP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602960433839%2FyxIW0IlTP.png" alt="Screenshot 2020-10-18 at 12.17.09 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can apply various options to manipulate the output.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-a: Display an entry for each file in a file hierarchy.&lt;/li&gt;
&lt;li&gt;-c: Display a total size.&lt;/li&gt;
&lt;li&gt;-d: Singnifes till what level deep this command should print the files.&lt;/li&gt;
&lt;li&gt;-h: Human-readable output.&lt;/li&gt;
&lt;li&gt;-g: Display the size in Gigabytes.&lt;/li&gt;
&lt;li&gt;-k: Display the size in Kilobytes.&lt;/li&gt;
&lt;li&gt;-m: Display the size in Megabytes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602960327928%2F4DIQ2Gt59.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602960327928%2F4DIQ2Gt59.png" alt="Screenshot 2020-10-18 at 12.15.21 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Practical Usage
&lt;/h3&gt;

&lt;p&gt;Suppose you want to print the top files that are consuming the most disk space. You can pipe the output of the &lt;code&gt;du&lt;/code&gt; to sort and then print the head.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602960817453%2FSfCILRVt_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602960817453%2FSfCILRVt_.png" alt="Screenshot 2020-10-18 at 12.23.29 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I have been extensively using this command to resolve production issues. Whenever we have an issue related to space on VM boxes I use this command to figure out what is exhausting the space. So it comes in handy in solving the real-world problem faced while debugging production issues which is related to disk space.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter &lt;a href="https://twitter.com/imuesh18" rel="noopener noreferrer"&gt;@imumesh18&lt;/a&gt;. Also please follow me here and on Twitter for future blogs which I write over &lt;a href="https://blog.umesh.wtf" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>linux</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Fathom vs Plausible: Which privacy-focused Google Analytics alternative should you use?</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Thu, 15 Oct 2020 18:30:13 +0000</pubDate>
      <link>https://forem.com/umesh/fathom-vs-plausible-which-privacy-focused-google-analytics-alternative-should-you-use-4l5l</link>
      <guid>https://forem.com/umesh/fathom-vs-plausible-which-privacy-focused-google-analytics-alternative-should-you-use-4l5l</guid>
      <description>&lt;p&gt;Today  &lt;a href="https://w3techs.com/technologies/history_overview/traffic_analysis"&gt;84%&lt;/a&gt; websites use Google Analytics on their website to track user activities. The reason for its popularity is that it's &lt;strong&gt;free&lt;/strong&gt;(not literally), I mean who doesn't love free stuff. But there is always a cost and that cost in the case of Google Analytics is data, they simply take this data from these websites and use it for their ads.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/StFrWoMq05gzAHLW1F/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/StFrWoMq05gzAHLW1F/giphy.gif" alt="Nothing is free"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://usefathom.com/ref/DTLQX0"&gt;Fathom&lt;/a&gt;  and  &lt;a href="https://plausible.io/"&gt;Plausible&lt;/a&gt;  are privacy-focused web analytics. They are simple and clutter-free. They don't store unnecessary data regarding the user, also they don't use cookies or other mechanisms to identify the user. So now we know what these two are, let's compare them and see what each of them offers. I will try to cover most of the points if not all so that you will get a good idea of what to look for when choosing between them.&lt;/p&gt;

&lt;h3&gt;
  
  
  UI/UX
&lt;/h3&gt;

&lt;p&gt;Both of them offer a single page simple and minimalistic analytics page. You can see all the basic things by just scrolling up and down. But plausible has few extra features like UTM and referrer for tracking custom campaigns. Also recently plausible launched the filter option where you can filter the analytics-based upon the country, referrer, etc. Fathom is working on v3 of their dashboard which will add more features, they haven't officially launched it yet but they have been working on pretty cool stuff. This one was very close probably will change once fathom launches their v3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Winner: Plausible&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Script size and speed.
&lt;/h3&gt;

&lt;p&gt;Both of them have a lightweight script that you can add to their website. &lt;a href="https://usefathom.com/ref/DTLQX0"&gt;Fathom&lt;/a&gt; script is 2KB and &lt;a href="https://plausible.io/"&gt;Plausible&lt;/a&gt; one 938B. If you compare them with Google analytics, plausible is 45x smaller, and fathom is 22x smaller. The smaller size of these scripts results in your website loading faster as compared to google analytics. But if you look at the size here fathom is 2x bigger than plausible.&lt;/p&gt;

&lt;p&gt;P.S: I came to know that plausible doesn’t have a globally available CDN network and their script takes almost 203ms whereas fathom takes only 7ms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Winner: Fathom&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eFXT2W45--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602748590346/Bd28yyIB2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eFXT2W45--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602748590346/Bd28yyIB2.png" alt="Screenshot 2020-10-14 at 10.59.20 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Pricing
&lt;/h3&gt;

&lt;p&gt;Fathom starts at $14 for 100k page views. Whereas plausible starts at $4 for 10k pageviews. This is something I like about plausible that they are making it affordable for the hobbyist and developer community who doesn't have much traffic right now but want to make sure their user's privacy is intact. Other than this both of their pricing almost the same as you go higher not much difference and both of them offer a custom plan if you don't find anything suitable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Winner: Plausible&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reliability and Infrastructure
&lt;/h3&gt;

&lt;p&gt;Fathom has been pretty much open about its infrastructure. Most of their components are hosted on AWS. They make sure the services are running on an industry-grade machine. Also recently they announced they will moving their database to AWS DyanamoDB. Also, they are moving stats to elastic search. Plausible infrastructure is not known at the time of writing this blog, but I would know what they use. But overall they seem to be handling the current traffic pretty well. Also if you look at their &lt;a href="https://plausible.instatus.com/incidents/1"&gt;status dashboard&lt;/a&gt; they seem to have very frequent incidents every month. Their uptime stands at 99.98% which pretty much huge.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Winner: Fathom&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  GDPR, CCPA, and PECR Compliant
&lt;/h3&gt;

&lt;p&gt;Both of them are GDPR, CCPA, and PECR compliant because they don't collect and store user data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Winner: tie&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Ad-Blockers
&lt;/h3&gt;

&lt;p&gt;Both of them kind of failed to avoid uBlock origin. But once I moved to the custom domain they both started to work. So no worries over there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Winner: tie&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Fathom&lt;/th&gt;
&lt;th&gt;Plausible&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;UI/UX&lt;/td&gt;
&lt;td&gt;Simple, minimalistic, and intuitive. But lacks some features like filtering and UTM tracking&lt;/td&gt;
&lt;td&gt;Almost identical to fathom but have UTM tracking and filtering based upon country, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Script Size&lt;/td&gt;
&lt;td&gt;2.0KB&lt;/td&gt;
&lt;td&gt;938B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pricing&lt;/td&gt;
&lt;td&gt;Starts at $14 for 100k page views. Has various other options&lt;/td&gt;
&lt;td&gt;Starts at $4 for 10k page views. Affordable for the hobbyist.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reliability and Infrastructure&lt;/td&gt;
&lt;td&gt;hosted on AWS. They have globally available CDN for serving their scripts.&lt;/td&gt;
&lt;td&gt;They have 99.98% uptime. Also, their servers and are located completely in the EU.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GDPR, CCPA and PECR Compliant&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ad-Blockers&lt;/td&gt;
&lt;td&gt;Yes and No. Yes if you are using a custom domain and No if fathom domain. (tested on uBlock origin)&lt;/td&gt;
&lt;td&gt;Yes and No. Yes if you are using a custom domain and No if fathom domain. (tested on uBlock origin)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reports Email&lt;/td&gt;
&lt;td&gt;Weekly&lt;/td&gt;
&lt;td&gt;Both weekly and monthly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Goal Tracking&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Share Dashboard&lt;/td&gt;
&lt;td&gt;You can share them as publicly accessible or make a private URL&lt;/td&gt;
&lt;td&gt;You can share them as publicly accessible or make a private URL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Open Source&lt;/td&gt;
&lt;td&gt;It was open source till v1 but then the creators decided to make it closed source because there was very less contribution from the community&lt;/td&gt;
&lt;td&gt;
&lt;a href="https://github.com/plausible/analytics"&gt;Open source&lt;/a&gt; and they recently changed their license &lt;a href="https://plausible.io/blog/open-source-licenses"&gt;AGPL&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;If we see the above comparison then Plausible seems to be a winner for people who want cheap entry point and privacy-focused web analytics whereas if you want more reliable and battle-tested you should go with fathom. It's not because plausible is not battle-tested and doesn't offer reliability but fathom has better elasticity and resilience to handle the surge in traffic as they use autoscale to handle the surge in load. But in my opinion, they both are the winner. They both are trying to revolutionize the web-analytics industry which was a monopoly for Google. I am pretty much excited about what these two will do in the future.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter &lt;a href="https://twitter.com/imumesh18"&gt;@imumesh18&lt;/a&gt;. You can also follow me for future posts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;P.S: The blog includes my referral link for fathom🙈.&lt;/p&gt;

</description>
      <category>privacy</category>
      <category>saas</category>
    </item>
    <item>
      <title>How to Implement HTTP Basic Auth in Go(Golang)</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Tue, 13 Oct 2020 08:19:18 +0000</pubDate>
      <link>https://forem.com/umesh/how-to-implement-http-basic-auth-in-go-golang-43ac</link>
      <guid>https://forem.com/umesh/how-to-implement-http-basic-auth-in-go-golang-43ac</guid>
      <description>&lt;p&gt;Today the web is full of people using various platforms. Every platform has its own authentication mechanism to identify users specific to their platform. Authentication helps your application to know that the person who sent a request to your application is actually who they say they are. Today we will learn how to implement one of the most simple and easiest authentication mechanisms, Basic Authentication. This tutorial will walk you through how can you implement a Go server which implements basic authentication.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Access Authentication
&lt;/h3&gt;

&lt;p&gt;Basic authentication is a simple authentication scheme built into the HTTP protocol. It doesn't require cookies, session identifiers, or login pages. The client sends HTTP requests with the standard Authorization header that contains the word Basic followed by space and a base64-encoded string &lt;code&gt;username:password&lt;/code&gt;.&lt;br&gt;
For example, the header for username &lt;code&gt;test&lt;/code&gt; and password &lt;code&gt;secret&lt;/code&gt; will look something shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Authorisation: Basic dGVzdDpzZWNyZXQ=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foqiijr9rxe9kn6huotwy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foqiijr9rxe9kn6huotwy.jpg" alt="Basic Auth Flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This diagram outlines the flow of basic auth between client and server. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the client doesn't provide the &lt;code&gt;Authorisation&lt;/code&gt; header the server returns &lt;code&gt;401 Unauthorised&lt;/code&gt; status code and provides information on how to authorize with a WWW-Authenticate response header containing at least one challenge.&lt;/li&gt;
&lt;li&gt;If the client provides the &lt;code&gt;Authorisation&lt;/code&gt; the server simply responds with &lt;code&gt;200 OK&lt;/code&gt; and the resource(In this case it's a simple welcome message but it can be any information.)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;After authenticating the server can perform various other checks like whether the client has access to the resource requested etc and respond to the client accordingly. In the above example, we are simply returning a welcome message without these checks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Creating the HTTP server
&lt;/h3&gt;

&lt;p&gt;Let's create a simple HTTP server in Go.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In case you want to learn about it you can refer to my other blog &lt;a href="https://blog.umesh.wtf/create-golang-http-server-in-15-lines" rel="noopener noreferrer"&gt;Create Golang HTTP Server in 15 Lines&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"meesage": "welcome to golang world!"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting Server at port :8080"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will add a check for basic auth in the greeting handler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BasicAuth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"WWW-Authenticate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;`Basic realm="Give username and password"`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "No basic auth present"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isAuthorised&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"WWW-Authenticate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;`Basic realm="Give username and password"`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "Invalid username or password"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "welcome to golang world!"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you look at the above carefully, you will see we are using the &lt;code&gt;r.BasicAuth()&lt;/code&gt; method to extract the username password. Here is the method signature &lt;code&gt;func (r *Request) BasicAuth() (username, password string, ok bool)&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4qc91lbvn7knip995xbl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4qc91lbvn7knip995xbl.jpg" alt="Authentication flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the method simply checks if the authorization header is not present it returns an empty username and password with ok as false else it decodes the base64 string and splits them by: and returns the username and password along with ok as true.&lt;/p&gt;

&lt;p&gt;After that, we check if the username and password provided by the client are present in our system or not. I have created a simple map of username and password. Here is the code for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var users = map[string]string{
    "test": "secret",
}

func isAuthorised(username, password string) bool {
    pass, ok := users[username]
    if !ok {
        return false
    }

    return password == pass
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's take a look at the entire example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"secret"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;isAuthorised&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;pass&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BasicAuth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"WWW-Authenticate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;`Basic realm="Give username and password"`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "No basic auth present"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isAuthorised&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"WWW-Authenticate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;`Basic realm="Give username and password"`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "Invalid username or password"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "welcome to golang world!"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting Server at port :8080"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me make a curl request and show it to you the output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$curl -v http://localhost:8080
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
&amp;gt; GET / HTTP/1.1
&amp;gt; Host: localhost:8080
&amp;gt; User-Agent: curl/7.64.1
&amp;gt; Accept: */*
&amp;gt; 
&amp;lt; HTTP/1.1 401 Unauthorized
&amp;lt; Content-Type: application/json
&amp;lt; Www-Authenticate: Basic realm="Give username and password"
&amp;lt; Date: Sun, 11 Oct 2020 12:20:23 GMT
&amp;lt; Content-Length: 36
&amp;lt; 
* Connection #0 to host localhost left intact
{"message": "No basic auth present"}* Closing connection 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you see the above message you will realize I made the call without the &lt;code&gt;Authorisation&lt;/code&gt; header is unauthenticated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl -v -u test:secret http://localhost:8080
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'test'
&amp;gt; GET / HTTP/1.1
&amp;gt; Host: localhost:8080
&amp;gt; Authorization: Basic dGVzdDpzZWNyZXQ=
&amp;gt; User-Agent: curl/7.64.1
&amp;gt; Accept: */*
&amp;gt; 
&amp;lt; HTTP/1.1 200 OK
&amp;lt; Content-Type: application/json
&amp;lt; Date: Sun, 11 Oct 2020 12:21:15 GMT
&amp;lt; Content-Length: 39
&amp;lt; 
* Connection #0 to host localhost left intact
{"message": "welcome to golang world!"}* Closing connection 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now after providing the correct username and password it returns success and the body with the message.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you open the localhost:8080 in the browser it will give you a pop-up window to enter the username and password. This happens because of the header &lt;code&gt;Www-Authenticate: Basic realm="Give username and password"&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Create a client
&lt;/h3&gt;

&lt;p&gt;To create the client we use &lt;code&gt;func (r *Request) SetBasicAuth(username, password string)&lt;/code&gt; to set the header. It basically takes the username and password then encodes it using base 64 and then add the header &lt;code&gt;Authorisation: Basic &amp;lt;bas64 encoded string&amp;gt;&lt;/code&gt;. Voila, you have successfully added the basic auth to your client request. Let me show you the full code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"io/ioutil"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MethodGet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"http://localhost:8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetBasicAuth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"secret"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;

    &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Do&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusCode&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Non 2xx response from server, request"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ioutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I am using panic here to handle the error but when you are writing this for a production handle it gracefully either by returning it or by logging it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you want to check out the full code you can go &lt;a href="https://github.com/imumesh18/basic-auth-go" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;/p&gt;

&lt;p&gt;Creating a client and server with basic auth support for Go is pretty easy. You don't even need an external library, the inbuilt support is more than sufficient to implement it. Also to better improve this you can move the basic auth logic into middleware and then you can attach that middleware to any handler which requires the basic auth. Just a warning basic auth is not the most secure mechanism for authentication for public-facing API.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on twitter &lt;a href="https://twitter.com/imuesh18" rel="noopener noreferrer"&gt;@imumesh18&lt;/a&gt;. Also please follow me here and on twitter for future blogs which I write over &lt;a href="https://blog.umesh.wtf" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>go</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Linux Commands: wc</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Sun, 11 Oct 2020 18:53:11 +0000</pubDate>
      <link>https://forem.com/umesh/linux-commands-wc-25f3</link>
      <guid>https://forem.com/umesh/linux-commands-wc-25f3</guid>
      <description>&lt;p&gt;&lt;code&gt;wc&lt;/code&gt; stands for &lt;strong&gt;word count&lt;/strong&gt;. It is used to display the number of lines, words, characters, and byte for a file or input received via pipe(|).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--syx_Jo0Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602436869935/aXrMwvjq3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--syx_Jo0Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602436869935/aXrMwvjq3.png" alt="Screenshot 2020-10-11 at 10.50.59 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the output shown above is explained below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 - Number of lines.&lt;/li&gt;
&lt;li&gt;2 - Number of words&lt;/li&gt;
&lt;li&gt;14 - Number of bytes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are four options that can be used with &lt;code&gt;wc&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; -c: The number of bytes in each input file is written to the standard output.&lt;/li&gt;
&lt;li&gt;-l: The number of lines in each input file is written to the standard output.&lt;/li&gt;
&lt;li&gt;-m: The number of characters in each input file is written to the standard output.  If the current locale does not support multi-byte characters, this is equivalent to the -c option.&lt;/li&gt;
&lt;li&gt;-w: The number of words in each input file is written to the standard output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see these options in action.&lt;/p&gt;

&lt;p&gt;This will count the lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ wc -l hashnode.txt
       1 hashnode.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will count the words.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ wc -w hashnode.txt
       2 hashnode.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will count the number of bytes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$  wc -c hashnode.txt
      14 hashnode.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But sometimes you have a file with multi-byte(non-ASCII charsets). For example, the Unicode character where one character is represented by multiple bytes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7zjCeQYb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602437662071/OZR9fHip6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7zjCeQYb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602437662071/OZR9fHip6.png" alt="Screenshot 2020-10-11 at 11.04.09 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you see this carefully, we added an emoji character to the end of the file. Both -c and -m output differ from each other because of that.&lt;/p&gt;

&lt;p&gt;You can also pass more than one file to the &lt;code&gt;wc&lt;/code&gt; as input.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8g30rs2h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602437946888/CKZxe-7cY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8g30rs2h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602437946888/CKZxe-7cY.png" alt="Screenshot 2020-10-11 at 11.08.56 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Advance usage.
&lt;/h3&gt;

&lt;p&gt;You can also apply the &lt;code&gt;wc&lt;/code&gt; on the output of other commands.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x_eNhaKd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602438108459/IYeVSQNao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x_eNhaKd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1602438108459/IYeVSQNao.png" alt="Screenshot 2020-10-11 at 11.11.41 PM.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-al&lt;/span&gt; | &lt;span class="nb"&gt;wc
     &lt;/span&gt;118    1065    7139
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;wc&lt;/code&gt; is very helpful and can be used quite frequently. Let me know in comment how you use it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this post please share it with others so that it can help them as well. You can tag me on twitter &lt;a href="https://twitter.com/imuesh18"&gt;@imumesh18&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>linux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Linux Commands: pwd</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Thu, 08 Oct 2020 18:56:52 +0000</pubDate>
      <link>https://forem.com/umesh/linux-commands-pwd-9j7</link>
      <guid>https://forem.com/umesh/linux-commands-pwd-9j7</guid>
      <description>&lt;p&gt;&lt;code&gt;pwd&lt;/code&gt; is one of the most helpful commands for linux newbie. Whenever you are lost just type it in your terminal and you will find where are you exactly. Let me show you how you can do it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602180574730%2FrtZa_5XQJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602180574730%2FrtZa_5XQJ.png" alt="Screenshot 2020-10-08 at 11.39.27 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see it tells me my current working directory. Usually, there are two ways you can invoke this command. One way you already saw in the above example. The other way is through &lt;code&gt;/bin/pwd&lt;/code&gt; they are almost the same. But &lt;code&gt;pwd&lt;/code&gt; is your shell in-built command while &lt;code&gt;/bin/pwd&lt;/code&gt; is calling the binary version of that. You can always verify which &lt;code&gt;pwd&lt;/code&gt; is getting invoked by using &lt;code&gt;which&lt;/code&gt; or &lt;code&gt;type&lt;/code&gt; command shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602181052648%2F3661qXpf3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602181052648%2F3661qXpf3.png" alt="Screenshot 2020-10-08 at 11.47.27 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pwd&lt;/code&gt; has two flags one is &lt;code&gt;-L&lt;/code&gt; and &lt;code&gt;-P&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-L&lt;/code&gt;: Display the logical current working directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-P&lt;/code&gt;: Display the physical current working directory (all symbolic links resolved).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see them in action and understand what exactly one does.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will create a symbolic link of folder &lt;code&gt;/var/log&lt;/code&gt; as h in your home directory and move to it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ln -s /var/log h
$ cd h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We will print the working directory using the logical flag &lt;code&gt;pwd -L&lt;/code&gt;. It will print the exact working directory from the environment even if it contains symlinks.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pwd -L
/Users/umesh/h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After that we will print the working directory using physical flag &lt;code&gt;pwd -P&lt;/code&gt;. It will print actual physical current working directory by resolving all symbolic links.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pwd -P
/private/var/log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Lastly we will check what happens when we run just &lt;code&gt;pwd&lt;/code&gt;. From the output, it's pretty much clear that &lt;code&gt;pwd&lt;/code&gt; takes &lt;code&gt;-L&lt;/code&gt; as the default flag if no options are provided.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pwd
/Users/umesh/h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602181616047%2FYfNsnuV3M.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602181616047%2FYfNsnuV3M.png" alt="Screenshot 2020-10-08 at 11.56.48 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I will be writing more on these simple and yet useful commands in my coming blog. Linux commands look simple and probably you remember it but if you know a bit in-depth about them they can come in handy.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this blog please feel free to share with others, if you are sharing it on twitter please tag &lt;a href="https://twitter.com/imumesh18" rel="noopener noreferrer"&gt;@imumesh18&lt;/a&gt; . If you are interested in more of these please subscribe to my newsletter.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>linux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to change the port of a Next.js application</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Wed, 07 Oct 2020 18:48:17 +0000</pubDate>
      <link>https://forem.com/umesh/how-to-change-the-port-of-a-next-js-application-5d3j</link>
      <guid>https://forem.com/umesh/how-to-change-the-port-of-a-next-js-application-5d3j</guid>
      <description>&lt;p&gt;I have been building my personal website using Next.js. By default, Next.js server runs on port &lt;code&gt;3000&lt;/code&gt; which is good but sometimes you want to run the server on a different port maybe because you already have a service running on it, or maybe it's not your favorite port(which in my case is &lt;code&gt;8080&lt;/code&gt;). So, let's see how can we change it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a next.js project.
&lt;/h3&gt;

&lt;p&gt;Creating a next.js project is pretty simple and straight forward. Just simply run the below command in your terminal.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Assumption: You already have yarn or npm install on your local.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create-next-app
# or
yarn create next-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can change the port using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn dev -p 8080
or
npm run dev -p 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;visit &lt;a href="http://localhost:8080/" rel="noopener noreferrer"&gt;http://localhost:8080/&lt;/a&gt; to see your website up and running. This command is temporary and will run the server on &lt;code&gt;8080&lt;/code&gt; whenever you enter the above command. But what if you want to make this permanently. &lt;/p&gt;

&lt;h3&gt;
  
  
  Change the Port Permanently
&lt;/h3&gt;

&lt;p&gt;Once you have your project setup check the &lt;code&gt;package.json&lt;/code&gt; file in the main folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "hashnode-blog-app",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "9.5.3",
    "react": "16.13.1",
    "react-dom": "16.13.1"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now what we need to change here is in the dev key inside scripts.&lt;/p&gt;

&lt;p&gt;Change it to &lt;code&gt;"dev": "next dev -p 8080",&lt;/code&gt; from &lt;code&gt;"dev": "next dev",&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now run the below command to run the server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn dev
or
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;visit the &lt;a href="http://localhost:8080/" rel="noopener noreferrer"&gt;http://localhost:8080/&lt;/a&gt; you should be able to see the website. Something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602094939706%2FMJzFNlOt0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602094939706%2FMJzFNlOt0l.png" alt="Screenshot 2020-10-07 at 11.52.07 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Linux Commands: ls</title>
      <dc:creator>Umesh Yadav</dc:creator>
      <pubDate>Tue, 06 Oct 2020 11:16:45 +0000</pubDate>
      <link>https://forem.com/umesh/linux-commands-ls-3fa1</link>
      <guid>https://forem.com/umesh/linux-commands-ls-3fa1</guid>
      <description>&lt;p&gt;&lt;code&gt;ls&lt;/code&gt; is the Linux command to list the content of a directory. You can list all the files and folders inside a directory. Let's try it in your terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h0mfs6Ty--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1601973911469/PDK4dLZYc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h0mfs6Ty--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1601973911469/PDK4dLZYc.png" alt="Screenshot 2020-10-06 at 2.14.52 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can pass arguments to &lt;code&gt;ls&lt;/code&gt; to change the output. Let's take a look at &lt;code&gt;ls -al&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kfwCLtA4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1601973957615/ayc-S2uQc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kfwCLtA4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1601973957615/ayc-S2uQc.png" alt="Screenshot 2020-10-06 at 2.15.44 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It has more detailed information as compared to &lt;code&gt;ls&lt;/code&gt;. It uses two flags &lt;code&gt;l&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt;. &lt;code&gt;a&lt;/code&gt; is used to show all the files even if the files are hidden(Hidden files are the one which starts with a dot at the beginning). &lt;code&gt;l&lt;/code&gt; is used to show details about each file and folder. Let me explain to you what exactly are those. We will take one of the lines from output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drwxr-xr-x  12 umesh  staff   384 Dec 11  2018 .git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;drwxr-xr-x: This defines the permission of the file.&lt;/li&gt;
&lt;li&gt;12: It tells the number of links to that file.&lt;/li&gt;
&lt;li&gt;umesh: This signifies the username of the owner of the file&lt;/li&gt;
&lt;li&gt;staff: This signifies the group of the file. &lt;/li&gt;
&lt;li&gt;384: file size in bytes.&lt;/li&gt;
&lt;li&gt;Dec 11 2018: file created/updated datetime.&lt;/li&gt;
&lt;li&gt;.git: filename.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are a bunch of flags available that can be used to modify the output of the &lt;code&gt;ls&lt;/code&gt; command. I am listing a few of them down below try them out in your terminal and let me know your experience and which one is your favorite in the comments.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;l - List files and directories in the long format&lt;/li&gt;
&lt;li&gt;a - Include directory entries whose names begin with a dot (.).&lt;/li&gt;
&lt;li&gt;d - Directories are listed as plain files (not searched recursively).&lt;/li&gt;
&lt;li&gt;i - For each file, print the file's file serial number (inode number).&lt;/li&gt;
&lt;li&gt;G – Enable colorized output.&lt;/li&gt;
&lt;li&gt;h – List the size of the files and directories in Human Readable format.&lt;/li&gt;
&lt;li&gt;S - Sort files by size&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this blog please feel free to share with others, if you are sharing it on twitter please tag  &lt;a href="https://twitter.com/imumesh18"&gt;@imumesh18&lt;/a&gt; . If you are interested in more of these please subscribe to my newsletter.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>linux</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
