<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muzammil</title>
    <description>The latest articles on DEV Community by Muzammil (@earthboundmisfit).</description>
    <link>https://dev.to/earthboundmisfit</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%2F423581%2Faf707743-78d1-4fba-88f4-385bf187d77f.jpeg</url>
      <title>DEV Community: Muzammil</title>
      <link>https://dev.to/earthboundmisfit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/earthboundmisfit"/>
    <language>en</language>
    <item>
      <title>How to access the request context anywhere in your application ?</title>
      <dc:creator>Muzammil</dc:creator>
      <pubDate>Mon, 16 Jan 2023 18:49:37 +0000</pubDate>
      <link>https://dev.to/earthboundmisfit/what-to-do-if-you-lose-track-of-request-context-before-an-async-function-execution-completes-55om</link>
      <guid>https://dev.to/earthboundmisfit/what-to-do-if-you-lose-track-of-request-context-before-an-async-function-execution-completes-55om</guid>
      <description>&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%2Fuploads%2Farticles%2F1jsq1zyvnqt22be1ljl0.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1jsq1zyvnqt22be1ljl0.png" alt="lost track of request context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Losing track of the request context
&lt;/h3&gt;

&lt;p&gt;Languages like Java, C++ are multi-threaded which can have access to request contexts in any part of the thread execution flow or request lifecycle. Whereas, Javascript follows a single-threaded functional flow by default and our required arguments need to be passed through async functions until the request-response lifecycle has been completed. But there could be instances where we lose track of data from the request context that is hitting the application.&lt;/p&gt;

&lt;p&gt;We pass headers, query params and body on a request which can be highly crucial. Say we introduce a header &lt;code&gt;tracking-token&lt;/code&gt; which tracks the HTTP calls in a microservice architecture. This being sent with a request header hitting from a service, we have access to the header in the route file as well as multiple middleware. But there are service layer functions handling heavy logic to get the desired result. Under this scenario, we may have multiple service layer functions being called. Also, we would've introduced more number of routes and more number of functions to handle the routes. At this point of time, it would be cumbersome to pass the &lt;code&gt;request&lt;/code&gt; as arguments to each callback function in the entire asynchronous cycle to make use of the header named &lt;code&gt;tracking-token&lt;/code&gt;.&lt;/p&gt;

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

// routes.js
app.get('/get-users, () =&amp;gt; {
 const userIds = req.users.map(user =&amp;gt; user.id);
 await getUsers(userIds);
});

//users.js
const getUsers = async (userIds) =&amp;gt; {
 await db.findAllByIds(userIds);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above we have lost track of the &lt;code&gt;req&lt;/code&gt; in the &lt;code&gt;getUsers&lt;/code&gt; function. Now, the only way to pass &lt;code&gt;req.get('tracking-token')&lt;/code&gt; is as a parameter to &lt;code&gt;getUsers&lt;/code&gt; function. Imagine we have tens and hundreds of such functions without access to request context and we may need to access it everywhere. Got into a mess right?! &lt;/p&gt;

&lt;p&gt;&lt;code&gt;AsyncLocalStorage&lt;/code&gt; has got us covered!!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://nodejs.org/api/async_context.html#class-asynclocalstorage" rel="noopener noreferrer"&gt;AsyncLocalStorage to the rescue&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;There are two ways to approach this. There are projects which use the latest stable version of Node.js and can embrace this and have a go ahead with &lt;code&gt;AsyncLocalStorage&lt;/code&gt;. But there are applications where upgrading to a newer node version is not an easy step. We'll figure out the former first.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;AsyncLocalStorage&lt;/code&gt; is a class which uses &lt;code&gt;node:async-hooks&lt;/code&gt; which is stable after Node version 16. We can add a middleware with &lt;code&gt;AsyncLocalStorage&lt;/code&gt; store which would hold the request context.&lt;/p&gt;

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


//storeRequestMiddleWare.js
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

const storeRequestMiddleware = (req, res, next) =&amp;gt; {
  asyncLocalStorage.run(new Map(), () =&amp;gt; {
    asyncLocalStorage.getStore().set('context', 
        req.get('tracing-token'));
    next();
  });
};

module.exports = {
  storeRequestMiddleware,
  asyncLocalStorage,
};

//app.js

app.use(storeRequestMiddleware)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With the &lt;code&gt;storeRequestMiddleware&lt;/code&gt;, the &lt;code&gt;tracing-token&lt;/code&gt; from the request header is now held in the store initialised by &lt;code&gt;AsyncLocalStorage&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, we can fetch the &lt;code&gt;tracing-token&lt;/code&gt; from the store in any part of the application.&lt;/p&gt;

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

//random.js
const traceId = asyncLocalStorage.getStore().get('traceId');


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;code&gt;AsyncLocalStorage&lt;/code&gt; uses &lt;a href="https://nodejs.org/api/async_hooks.html" rel="noopener noreferrer"&gt;&lt;code&gt;async-hooks&lt;/code&gt; which is still in experimental state&lt;/a&gt;. Make sure to use the dependency with caution for production-ready applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  For older node versions
&lt;/h3&gt;

&lt;p&gt;As for projects using older versions of node, we have the &lt;code&gt;node-continuation-local-storage&lt;/code&gt; &lt;a href="https://github.com/othiym23/node-continuation-local-storage" rel="noopener noreferrer"&gt;package&lt;/a&gt; which solves the same problem but has it's own way of implementation as written below.&lt;/p&gt;


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

&lt;p&gt;import { createNamespace } from 'continuation-local-storage';&lt;br&gt;
import { NextFunction, Request, Response} from "express";&lt;/p&gt;

&lt;p&gt;export const requestStore = createNamespace('token-store');&lt;/p&gt;

&lt;p&gt;export const tracingTokenMiddleware = (req: Request, res: Response, next: NextFunction) =&amp;gt; {&lt;br&gt;
    requestStore.run(() =&amp;gt; {&lt;br&gt;
        requestStore.set('tracing-token', req.get('tracing- &lt;br&gt;
              token'));&lt;br&gt;
        next();&lt;br&gt;
    });&lt;br&gt;
};&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Drawbacks of &lt;code&gt;continuation-local-storage&lt;/code&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;The package makes use of &lt;code&gt;async-listeners&lt;/code&gt; which backfires in case of concurrent HTTP calls. We may lose the request context when multiple HTTP calls are made to another service.&lt;/p&gt;

&lt;p&gt;It depends on the version of node and the requirement we need to meet when implementing these dependencies. Hope we don't lose track of the required parameters from the request context anymore! &lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>5 Reasons why you should prefer Next.js</title>
      <dc:creator>Muzammil</dc:creator>
      <pubDate>Tue, 01 Feb 2022 10:20:28 +0000</pubDate>
      <link>https://dev.to/earthboundmisfit/5-reasons-why-nextjs-is-here-to-stay-34f6</link>
      <guid>https://dev.to/earthboundmisfit/5-reasons-why-nextjs-is-here-to-stay-34f6</guid>
      <description>&lt;p&gt;When a solid framework holds its ground in the Javascript arena, some contributors start optimizing and building on top of the framework to make it better for the open-source community. Such was a framework that was built on top of ReactJS and has taken its place for web developers who wish to create web applications. Ranging from portfolio sites to e-commerce applications, Next.js has been preferred due to its less steep learning curve than its peers.&lt;/p&gt;

&lt;p&gt;There are many more reasons for Next.js to stay despite its ease of development and everything is just getting better with it. Here are some of the reasons why the “React Framework for Production” as they term it will be used more and more by the developer community.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Pre-fetched routing 🌏
&lt;/h2&gt;

&lt;p&gt;Routing has been bothering Javascript developers from the moment it was adapted for browser development. We usually go with installing one or more dependencies - be it react-router, Vue-router, or setting up Angular routing modules. Next.js has it all covered, all we need is to create files under the &lt;code&gt;src/pages&lt;/code&gt; folder to add a route.&lt;/p&gt;

&lt;p&gt;We can handle dynamic routes in the same way as well. Say, we have a blog or product which has a unique identifier id, if we require a URL dynamically for each blog - we may just have to create a file named &lt;code&gt;[id].js&lt;/code&gt; . Well, now we may ask for nested routes - no worries, create nested folders and files to have nested routes. For example, a file inside folders such as &lt;code&gt;pages/page/[id].js&lt;/code&gt; will have corresponding routes like &lt;code&gt;pages/page/1&lt;/code&gt;, &lt;code&gt;pages/page/2&lt;/code&gt; etc..&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Image and Font Optimization 🌈
&lt;/h2&gt;

&lt;p&gt;The Next.JS component named &lt;code&gt;next/image&lt;/code&gt; will serve the purpose of optimizing our images based on the viewport and enhancing visual stability as per the &lt;a href="https://www.notion.so/%5B%3Chttps://developers.google.com/search/blog/2020/11/timing-for-page-experience%3E%5D(%3Chttps://developers.google.com/search/blog/2020/11/timing-for-page-experience%3E)"&gt;Core Web Vitals&lt;/a&gt; for browsers. Instead of the usual &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag, we may need to use the &lt;code&gt;Image&lt;/code&gt; component when adding images to the DOM to achieve this. Adding a priority property as mentioned below to a certain image would make Next.JS ensure the image is &lt;a href="https://web.dev/lcp/#what-elements-are-considered"&gt;LCP&lt;/a&gt; (Largest Contentful Paint) and boost performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Image&lt;/span&gt;

&lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/sample.png"&lt;/span&gt;

&lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"A sample picture to boost LCP score"&lt;/span&gt;

&lt;span class="na"&gt;priority&lt;/span&gt;

&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fonts are fetched in Next.JS during build-time and not during run time, this prevents the browser to go looking for the font source when the application is being loaded in the browser run time.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. API Routes 🔩
&lt;/h2&gt;

&lt;p&gt;We may have to keep in mind that Next.js is a full-stack framework. Yes, APIs can be written in NextJS. Again, you never have to set up all the middleware configurations like in an express backend, all we have to do is create a folder name &lt;code&gt;/api&lt;/code&gt; inside the pages folder. Any file we create inside this folder with a handler function will be treated as an API with our traditional &lt;code&gt;req&lt;/code&gt; and &lt;code&gt;res&lt;/code&gt; parameters. Here’s an example of a similar handler function. Say we have a file called &lt;code&gt;pages/api/dashboard.js&lt;/code&gt; with the following handler function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;

            &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is dashboard data&lt;/span&gt;&lt;span class="dl"&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;All we have to do is call &lt;code&gt;localhost:3000/api/dashboard&lt;/code&gt; and fetch the data. At times, we may not need an isolated backend service or we may just feel lazy to create a backend application for POS project - NextJS got you covered with the backend service as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Pre-rendering  ⌛
&lt;/h2&gt;

&lt;p&gt;Next.js supports two types of pre-rendering a page:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static Site Generation (SSG)&lt;/li&gt;
&lt;li&gt;Server-Side Rendering (SSR)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Static site generation is when we need to pre-fetch the data required for the page. Therefore, the HTML is built during build time, the data required for the page is pre-rendered and populated in the components as props. This enables us to perform better in terms of SEO and better performance of the application. We use two handler functions &lt;code&gt;getStaticProps&lt;/code&gt; and &lt;code&gt;getStaticPaths&lt;/code&gt; to fetch the required data for the page and to fetch all the paths available in the application correspondingly.&lt;/p&gt;

&lt;p&gt;Server-side rendering as we know provides the HTML Template from the server to be loaded in DOM, but in terms of NextJS - there is a slight enhancement. Next.js provides a function called &lt;code&gt;getServerSideProps&lt;/code&gt; which would be called each time a server-side request is made. This makes sure the data is pre-rendered and is up to date before the template is loaded. This differentiates from Static site generation as in &lt;code&gt;getStaticProps&lt;/code&gt; will be called only once during the build time and not on every server-side request. The creators of Next.js recommend using SSG for better performance unless it is required to go with SSR.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Middlewares ⏩
&lt;/h2&gt;

&lt;p&gt;Working with Node server-side applications would have made us explore middleware more than ever. We never know how many handler functions we may have written if the concept of middlewares were not introduced to the open-source community. The functions defined as middleware will be applied before we hit all the routes in the pages folder in chronological order.&lt;/p&gt;

&lt;p&gt;We just have to define functions inside &lt;code&gt;pages/_middleware.ts&lt;/code&gt; file. The creators of Next.js have developed middlewares to support various functionalities for authentication, server-side analytics, A/B testing to name a few.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Bonus - Comparison with Gatsby 👾 👾
&lt;/h2&gt;

&lt;p&gt;Gatsby has been the go-to solution with the advent static site generation frameworks and JAM stack, but Next.js has caught up as a full-stack solution. There are situations when we need to prefer one over the other, but to consider Next.js - here are a few inferences made.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Learning Curve&lt;/strong&gt; - Next.js has a smoother learning curve than Gatsby. With lesser pre-requisites, Gatsby would need a thorough understanding of technologies such as GraphQL and markdown to get started. All we need to kickstart with Next.js is foundational React knowledge.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Minimalism&lt;/strong&gt; - We do not need to include a lot of external dependencies when getting to know about Next.js, whereas Gatsby is popular with the number of plugins and communities built around it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data fetching&lt;/strong&gt; - Just understanding &lt;code&gt;getStaticProps&lt;/code&gt; and &lt;code&gt;getServerSideProps&lt;/code&gt; is all there is to know about data fetching in Next.js, whereas Gatsby has its perks in its way of fetching data using GraphQL but not to get started with when we have limited bandwidth on doing things.&lt;/p&gt;

&lt;p&gt;Looking for a write-up on a different framework on a different day !! Until then.... ✌️ 💜&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Get rid of try-catch in server-side development, here's a better way!</title>
      <dc:creator>Muzammil</dc:creator>
      <pubDate>Wed, 01 Dec 2021 17:32:55 +0000</pubDate>
      <link>https://dev.to/earthboundmisfit/get-rid-of-try-catch-in-server-side-development-heres-a-better-way-2im6</link>
      <guid>https://dev.to/earthboundmisfit/get-rid-of-try-catch-in-server-side-development-heres-a-better-way-2im6</guid>
      <description>&lt;p&gt;&lt;strong&gt;PS: This write-up goes well with node.js or express.js development&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;We'll go knocking on the try-catch statement door whenever we encounter an asynchronous function in javascript - the traditional way. It is and will always be the backbone to handling async-await functions in server-side development.&lt;br&gt;
It has been of great help when we had to avoid the callback function and got us out of callback hell several times.&lt;/p&gt;

&lt;p&gt;But what if we can enhance this and make the try-catch statements shorter and cleaner. Here goes our typical way of handling things.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function main(req, res, next) {
  try {
    var result = await doSomething();
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Being Javascript developers, we are familiar with the above code, &lt;code&gt;result&lt;/code&gt; is printed to the console if everything goes smooth or else &lt;code&gt;error&lt;/code&gt; gets printed - 🍰&lt;/p&gt;

&lt;h3&gt;
  
  
  A Cleaner Alternative
&lt;/h3&gt;

&lt;p&gt;Say if we're using Express framework and handling requests using try-catch, here's something cleaner and simpler that can be replaced for the never-ending lengthy statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const catchAsync = (fn) =&amp;gt; (req, res, next) =&amp;gt;{
    Promise.resolve(fn(req, res, next)).catch((err) =&amp;gt; next(err));
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;catchAsync&lt;/code&gt; function can be considered as a middleware that just accepts the parameters (req, res, next). If there's any error, then it returns the error.&lt;/p&gt;

&lt;p&gt;This is how we can call our enhanced error catching function to avoid try-catch statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sampleFunction = catchAsync(async (req, res) =&amp;gt; {
           const awaitedResponse = await getResponse();
           res.send(awaitedResponse);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;catchAsync&lt;/code&gt; can be called anywhere we need to implement our traditional try-catch logic. Let's keep enhancing our good old lengthy statements and make javascript simpler for budding developers&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Kubernetes for beginners: Where do I start?</title>
      <dc:creator>Muzammil</dc:creator>
      <pubDate>Sun, 07 Nov 2021 08:24:16 +0000</pubDate>
      <link>https://dev.to/earthboundmisfit/kubernetes-for-beginners-where-do-i-start-5fmh</link>
      <guid>https://dev.to/earthboundmisfit/kubernetes-for-beginners-where-do-i-start-5fmh</guid>
      <description>&lt;p&gt;Being developers, we get zoned out in those standup calls when the server admins take over and flaunt their container orchestration skills. Let's take over and it's our time to shine now.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Kubernetes though?
&lt;/h3&gt;

&lt;p&gt;We have been hearing a lot about containers and how they've been streamlining the way a development lifecycle works. Not getting deep into containers in this post, you can have a look into &lt;a href="https://dev.to/earthboundmisfit/containerization-for-beginners-running-a-nodejs-application-using-docker-4f58"&gt;this&lt;/a&gt;. When our applications become bigger or if we are getting into making it modular, multiple containers come into play. Here's where things get tricky. For example, if we are running an e-commerce application - our auth/login, payment services, product catalogues, inventory management will run as individual containers or services - this is the microservices approach. Say if we are running the application using a Docker engine, we never know when a container goes down or when to scale up a container. We wouldn't have any clue when our social media campaign clicks and there is a good amount of rapid traffic hitting our services. Imagine if we got a black Friday sale going on and my services running are taking longer than usual to respond - Docker engine turns a bling eye 😧. What if my payment service goes down? 😟 - It would be chaos. &lt;/p&gt;

&lt;p&gt;Enter Kubernetes 😎 !!! - the open-source container orchestration platform. Now, we have it covered, Kubernetes does the auto-scaling for our pods in which our containers run. It does anything and everything to keep all the services up and running without the mess unravelling on the Black Friday sale with so much ease.&lt;/p&gt;

&lt;h3&gt;
  
  
  When do I need to use Kubernetes?
&lt;/h3&gt;

&lt;p&gt;As long as we're going with a multi-container application, Kubernetes is a yes. If we're adopting microservices architecture for the same - we got to be going with Kubernetes. &lt;/p&gt;

&lt;p&gt;Some of us would be asking this - "Hold up mate...But most of the applications I run are just React / Angular applications as a side hustle, do I need to containerize and go with Kubernetes ?" Short answer: A big no. Setting up a Kubernetes cluster is a lengthy task if we are not going with a cloud-managed Kubernetes service like &lt;a href="https://aws.amazon.com/eks/"&gt;AWS EKS&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Kubernetes glossary
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Cluster&lt;/strong&gt;&lt;br&gt;
A Kubernetes cluster is a set of nodes that provisions us to run the containerized application. It enables us to run applications across multiple machines and environments but helps us manage them under a single window&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pod&lt;/strong&gt;&lt;br&gt;
Pods are the smallest unit in Kubernetes. They can just be called a wrapper to the containers we run - a shell that encloses our application. We can scale up and down the number of pods which is similar to altering the number of instances of a deployed application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nodes&lt;/strong&gt;&lt;br&gt;
Nodes are physical or virtual machines where the pods are run. Ideally, Kubernetes sets up one master node and multiple worker nodes where the pods are run. The master node as its name says has a control plane which is the decision-maker in a cluster - it decides which nodes should the pods run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deployments&lt;/strong&gt;&lt;br&gt;
Deployments are a familiar term for us - yes, they are the same. They provide the configuration for the pods and the containers which are to be managed. We got two approaches to make a deployment - using the CLI (Imperative approach) and using a YAML file (Declarative approach). I prefer the latter approach to have the configurations documented.&lt;/p&gt;

&lt;h3&gt;
  
  
  What next ?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Start with a practical guide that goes according to your pace. This one &lt;a href="https://www.udemy.com/course/docker-kubernetes-the-practical-guide/"&gt;here&lt;/a&gt; on Udemy would be a good kickstart if you're starting right away from containers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start playing around with &lt;a href="https://minikube.sigs.k8s.io/docs/start/"&gt;minikube&lt;/a&gt;, which is a single node cluster setup to learn the concepts of Kubernetes in our local environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This write up is minuscule than the tip of the iceberg. There's nothing more precise than the official Kubernetes documentation where they provide a practical lab to understand the concepts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As the cloud has made up our lives easier, it's no more a deal starting up a cluster and managing the orchestration of pods. Cloud-managed Kubernetes services have taken the burden of setting up things. It's high time we have a look at popular Kubernetes managed services like &lt;a href="https://aws.amazon.com/eks/"&gt;AWS EKS&lt;/a&gt;, &lt;a href="https://azure.microsoft.com/en-in/services/kubernetes-service/"&gt;Azure Kubernetes Service&lt;/a&gt;, &lt;a href="https://www.ibm.com/in-en/cloud/kubernetes-service"&gt;IBM Cloud Kubernetes&lt;/a&gt; service based on our preferences.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Last but not least, remain consistent in learning Kubernetes. It's a steep learning curve out there but with a slight focus on things makes things a lot easier. 😃&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
      <category>docker</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Containerization for beginners: Running a nodeJS application using Docker</title>
      <dc:creator>Muzammil</dc:creator>
      <pubDate>Mon, 11 Oct 2021 15:10:26 +0000</pubDate>
      <link>https://dev.to/earthboundmisfit/containerization-for-beginners-running-a-nodejs-application-using-docker-4f58</link>
      <guid>https://dev.to/earthboundmisfit/containerization-for-beginners-running-a-nodejs-application-using-docker-4f58</guid>
      <description>&lt;p&gt;There are a few questions that would come up when seeing the title. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;"Hey, why would I run my nodeJS application in a container? I can use node server, nodemon or n number of live servers available"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"When would I have to run it as a container?"&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The answer is pretty simple if you're planning to not go through the tedious docker documentation out there.&lt;/p&gt;

&lt;h4&gt;
  
  
  Collaboration
&lt;/h4&gt;

&lt;p&gt;We mostly work as a team. Our peers install and work with dependencies, pulling from the repository and getting the code running in our development server is usually cumbersome. Dependencies may be missing, system requirements need to be met, storage issues, version control, quite a lot of other obstacles. Here comes the containers!!&lt;/p&gt;

&lt;h4&gt;
  
  
  Isolation
&lt;/h4&gt;

&lt;p&gt;Applications running in containers are isolated from each other and have their own storage and environments. We all would've gone through issues in production which cannot be captured in our development environment. Containers solve this right away.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to start a NodeJS container
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pre-requisites: Docker desktop or docker-engine needs to be installed in the local machine&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A sample NodeJS app that listens in port 3000 is being taken here to run in a container using &lt;a href="https://docs.docker.com/"&gt;Docker&lt;/a&gt; engine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.js

const express = require('express')
const mongoose = require('mongoose');

const app = express();

app.get('/', (req, res) =&amp;gt; {
   res.send('Yayyy!! Containers are a piece of cake');
});

app.listen(3000, () =&amp;gt; {
   console.log('Container running with db connection');
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  STEP 1: Create a Dockerfile
&lt;/h3&gt;

&lt;p&gt;Create a file named &lt;code&gt;Dockerfile&lt;/code&gt; without any file extensions with the following content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:14

WORKDIR /app

COPY . .

RUN npm install

EXPOSE 3000

CMD [ "node", "app.js" ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try figuring out what the above steps mean. They represent layers of a docker image. It is nothing but telling the Docker engine what procedure to follow when building the image. Wait a minute, what do you mean by an &lt;strong&gt;image&lt;/strong&gt; ? Got you covered 😉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.docker.com/engine/reference/commandline/images/"&gt;Docker images&lt;/a&gt; can be explained as blueprints to run an application in a container, just like how blueprints of a building help us to navigate through a block. &lt;/p&gt;

&lt;h3&gt;
  
  
  STEP 2: Run the docker engine
&lt;/h3&gt;

&lt;p&gt;Open docker desktop or just run the &lt;code&gt;docker&lt;/code&gt; command in the terminal (on Linux systems) to start the docker engine.&lt;/p&gt;

&lt;h3&gt;
  
  
  STEP 3: Build the docker image
&lt;/h3&gt;

&lt;p&gt;Navigate to the root directory of our application in the terminal. Run the following command to build the docker image of our application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.&lt;/code&gt; in the end is just pointing to the root directory where our &lt;code&gt;Dockerfile&lt;/code&gt; is placed. This command would download the base &lt;code&gt;node&lt;/code&gt; image from the &lt;a href="https://hub.docker.com/"&gt;Docker Hub&lt;/a&gt; or otherwise called a container registry. The above command would build the application container image which is to be run in a container.&lt;/p&gt;

&lt;p&gt;After successfully building the image, the terminal presents us with a docker image id - something similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; =&amp;gt; writing image sha256:d8e42706********9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;d8e42706********9&lt;/code&gt; is the image id here.&lt;/p&gt;

&lt;h3&gt;
  
  
  STEP 4: Run the docker container
&lt;/h3&gt;

&lt;p&gt;The last step is to run the container using the image id we have obtained. Run the following command to start our application in a container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 3000:3000 d8e42706********9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would route the exposed port 3000 in the container to port 3000 in the local machine. Navigate to &lt;code&gt;localhost:3000&lt;/code&gt; in your browser and check for the response from the server&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yayyy!! Containers are a piece of cake&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;🍰 🍰&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>docker</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Multiple arrow operators in a single function</title>
      <dc:creator>Muzammil</dc:creator>
      <pubDate>Fri, 14 May 2021 15:13:39 +0000</pubDate>
      <link>https://dev.to/earthboundmisfit/multiple-arrow-operators-in-a-single-function-551d</link>
      <guid>https://dev.to/earthboundmisfit/multiple-arrow-operators-in-a-single-function-551d</guid>
      <description>&lt;p&gt;We may have come across arrow functions in javascript and are almost saturated learning about the difference between normal functions and arrow functions, the difference between ES5 and ES6 and everything that is connected to it. But everytime we come across multiple arrows in a single function, we keep struggling - or it could just be a "me" problem. Here goes, what multiple arrows in a function mean.&lt;/p&gt;

&lt;p&gt;A simple ES6 function that demonstrates it:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = x =&amp;gt; y =&amp;gt; x + y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be written in ES5 functional format like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(x){
 return function(y){
  return x + y
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code explains what is going on with multiple arrow operators. It returns a function which in turn accepts a parameter, the nested returned function maintains the state of x. This methodology is called &lt;a href="https://blog.bitsrc.io/understanding-currying-in-javascript-ceb2188c339"&gt;currying&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;To call the above function, we follow a slightly different syntax. React developers who have worked on Redux would have come across such function calls when using the &lt;a href="https://react-redux.js.org/api/connect"&gt;connect function&lt;/a&gt;. Here we go:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add(2)(3)
// This would return 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's just another javascript basics that could help you crack your dream job interview. Thank me later! 😉&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>angular</category>
      <category>node</category>
    </item>
    <item>
      <title>Is "..." in javascript a spread operator or are they rest parameters?</title>
      <dc:creator>Muzammil</dc:creator>
      <pubDate>Thu, 22 Apr 2021 12:23:57 +0000</pubDate>
      <link>https://dev.to/earthboundmisfit/is-it-a-spread-operator-or-rest-parameters-24la</link>
      <guid>https://dev.to/earthboundmisfit/is-it-a-spread-operator-or-rest-parameters-24la</guid>
      <description>&lt;p&gt;Short answer? They're both. Here's to another question that choked up an interview process recently. Every time we come across the three dots in our javascript code, either it makes our life look so easy or it's another day of googling "Spread operator". Just remember the two points below if you're looking for a TL;DR or continue reading if you want to dig deeper.&lt;/p&gt;

&lt;p&gt;Keep in mind:-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The three dots (...) act as rest parameters if we aren't  sure of the number of parameters in a function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The three dots (...) act as spread operator when we have to merge two or more  objects.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Rest Parameters
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sumOfAllArgs(...args){
 let result = 0;
 args.forEach((arg) =&amp;gt; {
     result += arg;
 })
 return result
}

sumOfAllArgs(1,2,3,4,5) // would return 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above function which uses the rest parameters doesn't have a limit on the number of arguments to be passed to it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Spread Operator
&lt;/h2&gt;

&lt;p&gt;When you need to unpack an array or join an array with another or in case of copying the values of an array to another we can use the same operator (...) for a different cause&lt;/p&gt;

&lt;h4&gt;
  
  
  Concatenating an array
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myArr = ["jack", "jill", "hill"]
const yourArr = ["water", ...myArr]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the value of "yourArr" would be ["water", "jack", "jill", "hill"]. &lt;/p&gt;

&lt;h4&gt;
  
  
  Copying arrays
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a1 = [1, 2, 3]
const a2 = [...a1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any changes made to a1 wont affect array a2. &lt;/p&gt;

&lt;p&gt;The spread operator can also be used to perform the same actions on objects as well. Hope you get it right the next time! :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>angular</category>
      <category>react</category>
      <category>vue</category>
    </item>
  </channel>
</rss>
