<?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: Chandra Prakash Pal</title>
    <description>The latest articles on Forem by Chandra Prakash Pal (@palchandu_dev).</description>
    <link>https://forem.com/palchandu_dev</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%2F707011%2F24fc8aca-b0da-4409-9ed2-679fe32dc12b.jpeg</url>
      <title>Forem: Chandra Prakash Pal</title>
      <link>https://forem.com/palchandu_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/palchandu_dev"/>
    <language>en</language>
    <item>
      <title>Recursion</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Thu, 11 Sep 2025 12:57:11 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/recursion-311p</link>
      <guid>https://forem.com/palchandu_dev/recursion-311p</guid>
      <description>&lt;h3&gt;
  
  
  What is Recursion?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Recursion&lt;/strong&gt; is a programming technique where a function calls itself to solve smaller instances of a problem until it reaches a base case (a stopping condition). It's widely used for problems that can be broken down into similar sub-problems.&lt;/p&gt;




&lt;h3&gt;
  
  
  Real-Life Example of Recursion
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Example: Russian Dolls (Matryoshka Dolls)&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Imagine you have a set of Russian dolls, each nested inside a larger one.&lt;br&gt;&lt;br&gt;
To open all dolls:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You open the outermost doll.&lt;/li&gt;
&lt;li&gt;Inside, you find another doll and repeat the process.&lt;/li&gt;
&lt;li&gt;You stop when you find the smallest doll, which doesn’t contain any more dolls.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This mimics recursion:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Recursive Step:&lt;/strong&gt; Open the next doll (function calls itself).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base Case:&lt;/strong&gt; The smallest doll (no more dolls inside).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Tips and Tricks for Solving Problems Using Recursion
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Identify the Base Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The base case is the simplest version of the problem where recursion will stop.&lt;/li&gt;
&lt;li&gt;Without a base case, recursion leads to infinite loops or stack overflow.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Break Down the Problem&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Divide the problem into smaller, similar sub-problems.&lt;/li&gt;
&lt;li&gt;Each recursive call should work on a smaller input.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Think in Terms of “What’s Left to Do?”&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What part of the problem can you handle now, and what can you delegate to future recursive calls?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Avoid Redundant Calculations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;memoization&lt;/strong&gt; or &lt;strong&gt;dynamic programming&lt;/strong&gt; for optimization: store results of sub-problems to avoid re-computation.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Understand Stack Limitations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Recursive calls use the call stack. Deep recursions can lead to stack overflows. Iterative solutions may be preferred in such cases.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Test with Small Inputs&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Debug your recursion logic with simple and small inputs to verify base case and recursive steps.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Must Applicable Conditions for Using Recursion
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Problem Can Be Divided into Similar Sub-Problems:&lt;/strong&gt;
E.g., tree traversal, factorial calculation, Fibonacci sequence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base Case Exists:&lt;/strong&gt;
There is a clear condition to stop further recursive calls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sub-Problems Are Independent:&lt;/strong&gt;
Recursive calls do not interfere with each other’s results.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recursive Solution Is More Elegant or Simpler than Iterative:&lt;/strong&gt;
Some problems are naturally recursive (e.g., traversing hierarchical data).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Example in Code: Factorial
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Base&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Recursive&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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;strong&gt;Summary:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Recursion works best when problems are self-similar and have a clear stopping point. Always ensure a base case, break the problem down, and watch out for performance and stack limitations. Use recursion when it makes your code cleaner, simpler, and easier to understand!&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested category example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AllCategories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Electronics&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;children&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;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mobile Phones&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;children&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;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Smartphones&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;children&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;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Feature Phones&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;children&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;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Laptops&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;children&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;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Home Appliances&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;children&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;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for &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;category&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;categories&lt;/span&gt;&lt;span class="p"&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;product_count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;childrenWithCount&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;childrenWithCount&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;processCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;product_count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;childrenWithCount&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="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;processCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AllCategories&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;h2&gt;
  
  
  Result
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Electronics"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"children"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Mobile Phones"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"children"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Smartphones"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"children"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"product_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Feature Phones"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"children"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"product_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"product_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Laptops"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"children"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"product_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"product_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Home Appliances"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"children"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"product_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Microservices Architecture</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Thu, 19 Jun 2025 08:03:53 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/microservices-architecture-1c2</link>
      <guid>https://forem.com/palchandu_dev/microservices-architecture-1c2</guid>
      <description>&lt;p&gt;Great! Let’s go step-by-step and thoroughly understand Microservices architecture from the perspective of a Node.js backend developer. I'll explain:&lt;/p&gt;




&lt;p&gt;🔍 1. What is Microservices Architecture?&lt;/p&gt;

&lt;p&gt;Microservices is an architectural style where an application is divided into a set of loosely coupled, independently deployable services, each handling a specific business capability.&lt;/p&gt;

&lt;p&gt;Instead of building one large monolithic application, you break it into small services that communicate over a network.&lt;/p&gt;




&lt;p&gt;🏗 Example: eCommerce Platform&lt;/p&gt;

&lt;p&gt;Let’s compare Monolith vs Microservices.&lt;/p&gt;

&lt;p&gt;🔸 Monolithic App&lt;/p&gt;

&lt;p&gt;A single codebase and server handles:&lt;/p&gt;

&lt;p&gt;User Authentication&lt;/p&gt;

&lt;p&gt;Product Catalog&lt;/p&gt;

&lt;p&gt;Orders&lt;/p&gt;

&lt;p&gt;Payments&lt;/p&gt;

&lt;p&gt;Admin Panel&lt;/p&gt;

&lt;p&gt;If something crashes, the whole app fails.&lt;/p&gt;

&lt;p&gt;🔸 Microservices App&lt;/p&gt;

&lt;p&gt;Each service is a separate app:&lt;/p&gt;

&lt;p&gt;Microservice    Responsibility  Port / DB   Language&lt;/p&gt;

&lt;p&gt;user-service    Login, register, profile    MongoDB Node.js&lt;br&gt;
product-service Catalog management  PostgreSQL  Go&lt;br&gt;
order-service   Order placement, status MySQL   Node.js&lt;br&gt;
payment-service Payments, refunds   Redis   Python&lt;/p&gt;

&lt;p&gt;Services communicate over the network (TCP/HTTP/gRPC/RabbitMQ/etc).&lt;/p&gt;




&lt;p&gt;🧩 2. Key Characteristics&lt;/p&gt;

&lt;p&gt;Feature Description&lt;/p&gt;

&lt;p&gt;Independent Deploys Each service can be deployed separately&lt;br&gt;
Polyglot    Use different languages, databases&lt;br&gt;
Resilience  One service failure doesn’t crash the entire app&lt;br&gt;
Scalability Scale only the heavy-loaded services&lt;br&gt;
DevOps Friendly CI/CD is easier with small units&lt;/p&gt;




&lt;p&gt;🔌 3. Communication Methods Between Services&lt;/p&gt;

&lt;p&gt;There are multiple inter-service communication mechanisms:&lt;/p&gt;

&lt;p&gt;Method  Description Example Use&lt;/p&gt;

&lt;p&gt;HTTP REST   Simple APIs over HTTP   API Gateway to Microservice&lt;br&gt;
TCP Fast, persistent connections    NestJS TCP transport&lt;br&gt;
gRPC    Binary, performant RPC calls    Real-time apps&lt;br&gt;
Message Bus RabbitMQ / Kafka / NATS Event-driven architecture&lt;br&gt;
WebSockets  Bidirectional live connections  Chat / real-time systems&lt;/p&gt;




&lt;p&gt;🧭 4. What is an API Gateway in Microservices?&lt;/p&gt;

&lt;p&gt;An API Gateway is a single entry point for clients to interact with microservices.&lt;/p&gt;

&lt;p&gt;🧱 Role of API Gateway&lt;/p&gt;

&lt;p&gt;Feature Description&lt;/p&gt;

&lt;p&gt;Routing Directs client requests to appropriate services&lt;br&gt;
Aggregation Combines data from multiple services&lt;br&gt;
Security    Handles authentication, rate-limiting&lt;br&gt;
Logging/Monitoring  Logs requests and errors&lt;br&gt;
Versioning  API version control&lt;/p&gt;




&lt;p&gt;🔁 Real-World Example&lt;/p&gt;

&lt;p&gt;Request: GET /orders&lt;/p&gt;

&lt;p&gt;API Gateway:&lt;/p&gt;

&lt;p&gt;Validates JWT&lt;/p&gt;

&lt;p&gt;Forwards to order-service&lt;/p&gt;

&lt;p&gt;Fetches user details from user-service&lt;/p&gt;

&lt;p&gt;Merges both&lt;/p&gt;

&lt;p&gt;Sends single response to frontend&lt;/p&gt;




&lt;p&gt;🧰 5. Do I Build the Gateway or Use Third-Party?&lt;/p&gt;

&lt;p&gt;✅ Option 1: Build Your Own (recommended for control/custom logic)&lt;/p&gt;

&lt;p&gt;Use:&lt;/p&gt;

&lt;p&gt;NestJS with HTTP + TCP clients&lt;/p&gt;

&lt;p&gt;Express.js&lt;/p&gt;

&lt;p&gt;Fastify&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;p&gt;Full control&lt;/p&gt;

&lt;p&gt;Tailor-made logic&lt;/p&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;p&gt;You manage load balancing, security, rate-limiting manually&lt;/p&gt;

&lt;p&gt;✅ Option 2: Use API Gateway Providers (recommended for production-scale infra)&lt;/p&gt;

&lt;p&gt;Gateway Features&lt;/p&gt;

&lt;p&gt;Kong    Open source, plugins, rate limit, auth&lt;br&gt;
NGINX   Lightweight, reverse proxy&lt;br&gt;
AWS API Gateway Fully managed, serverless&lt;br&gt;
Traefik Auto-routing with Docker/K8s&lt;br&gt;
Istio (K8s) Advanced service mesh with observability&lt;/p&gt;

&lt;p&gt;Note: These tools often integrate with service discovery (e.g., Consul, Eureka) in large setups.&lt;/p&gt;




&lt;p&gt;🏗 6. Development Methodologies for Microservices&lt;/p&gt;

&lt;p&gt;There are several ways to design and build microservices:&lt;/p&gt;

&lt;p&gt;🔹 A. REST-based Microservices&lt;/p&gt;

&lt;p&gt;Services communicate over REST APIs&lt;/p&gt;

&lt;p&gt;Lightweight and easy to debug&lt;/p&gt;

&lt;p&gt;Ideal for basic CRUD services&lt;/p&gt;

&lt;p&gt;🔹 B. Event-Driven Microservices&lt;/p&gt;

&lt;p&gt;Communicate via events (Kafka, RabbitMQ, NATS)&lt;/p&gt;

&lt;p&gt;Services publish/subscribe events&lt;/p&gt;

&lt;p&gt;Useful for loosely coupled and scalable systems&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example: order-service emits order_placed → payment-service listens to it&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;🔹 C. RPC-based Microservices (TCP/gRPC)&lt;/p&gt;

&lt;p&gt;Services talk over Remote Procedure Calls&lt;/p&gt;

&lt;p&gt;High performance&lt;/p&gt;

&lt;p&gt;Often used in NestJS (TCP) or gRPC (Google)&lt;/p&gt;




&lt;p&gt;🧪 Example Architecture in NestJS&lt;/p&gt;

&lt;p&gt;Client (React App)&lt;br&gt;
       |&lt;br&gt;
       v&lt;br&gt;
+-------------------+&lt;br&gt;
|   API Gateway     |  &amp;lt;-- NestJS REST app&lt;br&gt;
+-------------------+&lt;br&gt;
    |     |     |&lt;br&gt;
    v     v     v&lt;br&gt;
+--------+ +--------+ +--------+&lt;br&gt;
|  User  | | Product| | Orders |&lt;br&gt;
|Service | |Service | |Service |&lt;br&gt;
+--------+ +--------+ +--------+&lt;/p&gt;

&lt;p&gt;Communication: REST → Gateway → TCP (NestJS transport) between services.&lt;/p&gt;




&lt;p&gt;✅ Summary&lt;/p&gt;

&lt;p&gt;Topic   Key Point&lt;/p&gt;

&lt;p&gt;Microservices   Split app into small, independent services&lt;br&gt;
Communication   Use TCP, REST, gRPC, or Messaging&lt;br&gt;
API Gateway Front door to your services&lt;br&gt;
Build vs Buy Gateway    You can build (NestJS) or use Kong, AWS&lt;br&gt;
Development Styles  REST-based, Event-driven, RPC&lt;br&gt;
NestJS Support  Built-in support for TCP/gRPC/RMQ/Kafka microservices&lt;/p&gt;




&lt;p&gt;Would you like to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Learn Event-Driven architecture next&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Learn gRPC vs TCP vs REST comparison&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set up a message broker (like RabbitMQ) in your microservice system&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Learn about Service Discovery and dynamic microservices registration?&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let me know!&lt;/p&gt;

</description>
      <category>node</category>
      <category>microservices</category>
      <category>nestjs</category>
    </item>
    <item>
      <title>https://zenduty.com/blog/observability-tools/</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Mon, 09 Jun 2025 22:43:06 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/httpszendutycomblogobservability-tools-15i</link>
      <guid>https://forem.com/palchandu_dev/httpszendutycomblogobservability-tools-15i</guid>
      <description></description>
      <category>monitoring</category>
      <category>tooling</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Use async await with Array.map</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Wed, 02 Apr 2025 16:45:29 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/use-async-await-with-arraymap-4ia7</link>
      <guid>https://forem.com/palchandu_dev/use-async-await-with-arraymap-4ia7</guid>
      <description>&lt;p&gt;Problem Context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [1,2,3,4,5];

var results: number[] = await arr.map(async (item): Promise&amp;lt;number&amp;gt; =&amp;gt; {
        await callAsynchronousOperation(item);
        return item + 1;
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Error Context:&lt;br&gt;
TS2322: Type 'Promise[]' is not assignable to type 'number[]'. Type 'Promise is not assignable to type 'number'.&lt;/p&gt;

&lt;p&gt;Solution Context:&lt;/p&gt;

&lt;p&gt;The problem here is that you are trying to await an array of promises rather than a Promise. This doesn't do what you expect.&lt;/p&gt;

&lt;p&gt;When the object passed to await is not a Promise, await simply returns the value as-is immediately instead of trying to resolve it. So since you passed await an array (of Promise objects) here instead of a Promise, the value returned by await is simply that array, which is of type Promise[].&lt;/p&gt;

&lt;p&gt;What you probably want to do is call Promise.all on the array returned by map in order to convert it to a single Promise before awaiting 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 arr = [1, 2, 3, 4, 5];

var results: number[] = await Promise.all(arr.map(async (item): Promise&amp;lt;number&amp;gt; =&amp;gt; {
    await callAsynchronousOperation(item);
    return item + 1;
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Javascript how to check object is empty or not</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Mon, 15 Jul 2024 04:09:24 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/javascript-how-to-check-object-is-empty-or-not-1p5a</link>
      <guid>https://forem.com/palchandu_dev/javascript-how-to-check-object-is-empty-or-not-1p5a</guid>
      <description>&lt;p&gt;In JavaScript everything start with object. Object has vital role while working with JavaScript. &lt;/p&gt;

&lt;p&gt;We define object with two curly braces like&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const user={};&lt;br&gt;
later can add key value pairs in it like&lt;br&gt;
user.name="User"&lt;br&gt;
user.email="user@gmail.com"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But mostly we have to check that object is empty or not. So let's check it&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const user={};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;console.log(Object.keys(user).length) // will log 0&lt;/p&gt;

&lt;p&gt;&lt;code&gt;user.name="User"&lt;br&gt;
user.email="user@gmail.com"&lt;br&gt;
console.log(Object.keys(user).length) // will log 2&lt;/code&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Deploy NodeJs Application On Ubuntu and Nginx</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Fri, 21 Jun 2024 09:21:08 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/deploy-nodejs-application-on-ubuntu-and-nginx-a53</link>
      <guid>https://forem.com/palchandu_dev/deploy-nodejs-application-on-ubuntu-and-nginx-a53</guid>
      <description>&lt;p&gt;When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain on a single server.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll discuss how to configure server blocks in Nginx on an Ubuntu.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Login on remote machine by ssh&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First login on remote machine by ssh as follows&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh user@remote_ip&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After login on remote install nodejs on machine, if already installed then ignore it.&lt;/p&gt;

&lt;p&gt;You can check installation guide from &lt;a href="https://nodejs.org/en/download/package-manager" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clone your project from Github&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are a few ways to get your files on to the server, I would suggest using Git&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git clone yourproject.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Install dependencies and test app&lt;/p&gt;

&lt;p&gt;`cd yourproject&lt;br&gt;
npm install&lt;br&gt;
npm start (or whatever your start command)&lt;/p&gt;

&lt;h1&gt;
  
  
  stop app
&lt;/h1&gt;

&lt;p&gt;ctrl+C`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setup ufw firewall&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;sudo ufw enable&lt;br&gt;
sudo ufw status&lt;br&gt;
sudo ufw allow ssh (Port 22)&lt;br&gt;
sudo ufw allow http (Port 80)&lt;br&gt;
sudo ufw allow https (Port 443)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install NGINX and configure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;sudo apt install nginx&lt;/p&gt;

&lt;p&gt;sudo nano /etc/nginx/sites-available/default&lt;/p&gt;

&lt;p&gt;Add the following to the location part of the server block&lt;/p&gt;

&lt;p&gt;`server {&lt;br&gt;
    server_name your_domain &lt;a href="http://www.your_domain" rel="noopener noreferrer"&gt;www.your_domain&lt;/a&gt;;&lt;br&gt;
    location / {&lt;br&gt;
        proxy_pass &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;;&lt;br&gt;
        proxy_http_version 1.1;&lt;br&gt;
        proxy_set_header Upgrade $http_upgrade;&lt;br&gt;
        proxy_set_header Connection 'upgrade';&lt;br&gt;
        proxy_set_header Host $host;&lt;br&gt;
        proxy_cache_bypass $http_upgrade;&lt;br&gt;
    }&lt;/p&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;`# Check NGINX config&lt;br&gt;
sudo nginx -t&lt;/p&gt;

&lt;h1&gt;
  
  
  Restart NGINX
&lt;/h1&gt;

&lt;p&gt;sudo service nginx restart`&lt;/p&gt;

&lt;p&gt;Make the symbolik linking by running following&lt;/p&gt;

&lt;p&gt;sudo ln -s /etc/nginx/sites-available/{your_domain or folder name in sites_availabe}/etc/nginx/sites-enabled&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Secure Nginx with Let's Encrypt&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing Certbot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt install certbot python3-certbot-nginx&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Confirming Nginx’s Configuration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To check, open the configuration file for your domain using nano or your favorite text editor:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/example.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Find the existing server_name line. It should look like this:&lt;/p&gt;

&lt;p&gt;/etc/nginx/sites-available/example.com&lt;br&gt;
...&lt;br&gt;
server_name example.com &lt;a href="http://www.example.com" rel="noopener noreferrer"&gt;www.example.com&lt;/a&gt;;&lt;br&gt;
...&lt;/p&gt;

&lt;p&gt;If it does, exit your editor and move on to the next step.&lt;/p&gt;

&lt;p&gt;verify the syntax of your configuration file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo nginx -t&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once your configuration file’s syntax is correct, reload Nginx to load the new configuration:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo systemctl reload nginx&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Obtaining an SSL Certificate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo certbot --nginx -d example.com -d www.example.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Credits goes to following references&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/hasibdesk/a296fa59240f86421b78e79586adb264" rel="noopener noreferrer"&gt;NodeJS App Deployment On Ubuntu Server&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04" rel="noopener noreferrer"&gt;How To Set Up a Node.js Application for Production on Ubuntu 20.04&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04" rel="noopener noreferrer"&gt;How To Secure Nginx with Let's Encrypt on Ubuntu&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Create virtual host on nginx server(Ubuntu)</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Thu, 20 Jun 2024 08:30:34 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/create-virtual-host-on-nginx-serverubuntu-5gj4</link>
      <guid>https://forem.com/palchandu_dev/create-virtual-host-on-nginx-serverubuntu-5gj4</guid>
      <description>&lt;p&gt;The deployment of application on server is a very tedious task. From installation of nginx to creation of virtual host and linking of virtual host from /etc/nginx/sites-available to /etc/nginx/sites-ebabled. Without the enable of virtual host will not work.&lt;/p&gt;

&lt;p&gt;Following are steps to create virtual host and make it enabled to work.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;create a host file on location /etc/nginx/sites-available/{your_host_file}&lt;br&gt;
Ex. /etc/nginx/sites-available/demo-app&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next add the following code in host file&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;server {&lt;br&gt;
    location / {&lt;br&gt;
        proxy_pass http://localhost:4100;&lt;br&gt;
        proxy_http_version 1.1;&lt;br&gt;
        proxy_set_header Upgrade $http_upgrade;&lt;br&gt;
        proxy_set_header Connection 'upgrade';&lt;br&gt;
        proxy_set_header Host $host;&lt;br&gt;
        proxy_cache_bypass $http_upgrade;&lt;br&gt;
    }&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now enable the host by following commnad&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/demo-app /etc/nginx/sites-enabled/&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>PM2 Add NodeJs Project In Process List</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Fri, 14 Jun 2024 13:44:10 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/pm2-add-nodejs-project-in-process-list-1bk</link>
      <guid>https://forem.com/palchandu_dev/pm2-add-nodejs-project-in-process-list-1bk</guid>
      <description>&lt;p&gt;During the NodeJs project deployment we use the &lt;a href="https://pm2.keymetrics.io/docs/usage/application-declaration/" rel="noopener noreferrer"&gt;PM2&lt;/a&gt; as process manager.&lt;/p&gt;

&lt;p&gt;PM2 monitor the application and restart it automatically while if app is crashing.&lt;/p&gt;

&lt;p&gt;First install PM2 package&lt;/p&gt;

&lt;p&gt;`$ npm install pm2@latest -g&lt;/p&gt;

&lt;h1&gt;
  
  
  or
&lt;/h1&gt;

&lt;p&gt;$ yarn global add pm2`&lt;/p&gt;

&lt;p&gt;When managing multiple applications with PM2, use a JS configuration file to organize them.&lt;/p&gt;

&lt;p&gt;To generate a sample configuration file you can type this command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pm2 init simple&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will generate a sample ecosystem.config.js:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;module.exports = {&lt;br&gt;
  apps : [{&lt;br&gt;
    name   : "app1",&lt;br&gt;
    script : "./app.js"&lt;br&gt;
  }]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the command to add NodeJs project in pm2 list with npm script are following way&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pm2 start npm --name "{project_name}" -- run {script_name}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For Example ,let's i have a project with name HelloWorld and script to run the project are npm run dev the following command will be.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pm2 start npm --name "rStoreFront Backend" -- run dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here are some references for deploy nodejs project&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04" rel="noopener noreferrer"&gt;How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-18-04" rel="noopener noreferrer"&gt;https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-18-04&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-18-04" rel="noopener noreferrer"&gt;How To Set Up a Node.js Application for Production on Ubuntu 20.04&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Javascript expression execution order</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Wed, 12 Jun 2024 15:46:24 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/javascript-expression-execution-order-4fan</link>
      <guid>https://forem.com/palchandu_dev/javascript-expression-execution-order-4fan</guid>
      <description>&lt;p&gt;JavaScript always evaluates expressions in strictly left-to-right order.&lt;/p&gt;

&lt;p&gt;In general, JavaScript expressions are evaluated in the following order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parentheses&lt;/li&gt;
&lt;li&gt;Exponents&lt;/li&gt;
&lt;li&gt;Multiplication and division&lt;/li&gt;
&lt;li&gt;Addition and subtraction&lt;/li&gt;
&lt;li&gt;Relational operators&lt;/li&gt;
&lt;li&gt;Equality operators&lt;/li&gt;
&lt;li&gt;Logical AND&lt;/li&gt;
&lt;li&gt;Logical OR&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is important to understand the order in which expressions are evaluated in order to write code that works correctly.&lt;/p&gt;

&lt;p&gt;Here are the most commonly used operators in JavaScript, ordered by precedence from highest to lowest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Grouping: ()&lt;/li&gt;
&lt;li&gt;Unary: +, -, !&lt;/li&gt;
&lt;li&gt;Multiplicative: *,/, %&lt;/li&gt;
&lt;li&gt;Additive: +, -&lt;/li&gt;
&lt;li&gt;Relational: &amp;lt;, &amp;gt;, &amp;lt;=, &amp;gt;=&lt;/li&gt;
&lt;li&gt;Equality: ==, != , ===, !==&lt;/li&gt;
&lt;li&gt;Logical AND: &amp;amp;&amp;amp;&lt;/li&gt;
&lt;li&gt;Logical OR: ||&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reference taken from &lt;a href="https://www.almabetter.com/bytes/tutorials/javascript/precedence-of-operators" rel="noopener noreferrer"&gt;here&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>javascriptexpression</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Javascript wait inside loop for complete asynchronous task in synchronous way.</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Mon, 03 Jun 2024 08:24:37 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/javascript-wait-inside-loop-for-complete-asynchronous-task-in-synchronous-way-58g4</link>
      <guid>https://forem.com/palchandu_dev/javascript-wait-inside-loop-for-complete-asynchronous-task-in-synchronous-way-58g4</guid>
      <description>&lt;p&gt;&lt;a href="https://madhavpalshikar.medium.com/javascript-how-to-wait-in-for-loop-6a4894d6335d" rel="noopener noreferrer"&gt;Get Reference from here.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>asynch</category>
    </item>
    <item>
      <title>Differences between CSS transitions, transforms, and animations</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Sat, 11 May 2024 13:38:28 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/differences-between-css-transitions-transforms-and-animations-58hi</link>
      <guid>https://forem.com/palchandu_dev/differences-between-css-transitions-transforms-and-animations-58hi</guid>
      <description>&lt;p&gt;Here are some differences between CSS transitions, transforms, and animations:&lt;br&gt;
Transitions&lt;br&gt;
Transitions are state changes that move from one state to another, like a :hover state. They require a trigger to run, and they run forwards when triggered and in reverse when the trigger is removed. Transitions have properties for the name of the property, the duration, any delay, and timing functions.&lt;br&gt;
Transforms&lt;br&gt;
Transforms change the shape and position of content by modifying the coordinate space. They can rotate, move, skew, and scale elements.&lt;br&gt;
Animations&lt;br&gt;
Animations can have multiple stages and are better for more complex movements. They don't require a trigger to run, and they can be played forwards, backwards, or in alternate cycles. Animations have the same properties as transitions, but with the word "animation" instead of "transition".&lt;/p&gt;

</description>
      <category>css</category>
      <category>cssanimatio</category>
    </item>
    <item>
      <title>Random Password Generate</title>
      <dc:creator>Chandra Prakash Pal</dc:creator>
      <pubDate>Wed, 24 Apr 2024 05:55:45 +0000</pubDate>
      <link>https://forem.com/palchandu_dev/random-password-generate-2519</link>
      <guid>https://forem.com/palchandu_dev/random-password-generate-2519</guid>
      <description>&lt;p&gt;When we develop any application, we need a random password for every user.As more as secure password is recommended. The conditions for secure password must follow &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Minimum 8 character long password&lt;/li&gt;
&lt;li&gt;Minimum one uppercase letter&lt;/li&gt;
&lt;li&gt;Minimum one lowercase letter&lt;/li&gt;
&lt;li&gt;Minimum one special character&lt;/li&gt;
&lt;li&gt;Minimum one number.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The combination of above condition will generate a secure password.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function generatePassword(length) {
    // code to generate passowrd
    var charNum = "0123456789";
    var charLower = "abcdefghijklmnopqrstuvwxyz"
    var charUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    var charSpecial = "!@#$%^&amp;amp;*_"
    var charAll = charNum + charLower + charUpper + charSpecial;
    var password = "";
    for (var i = 0; i &amp;lt; length; i++) {
        if (i == 0) {
            password += charUpper.charAt(Math.floor(Math.random() * charUpper.length));
        }
        if(i == 1){
            password += charLower.charAt(Math.floor(Math.random() * charLower.length));
        }
        if(i == 2){
            password += charNum.charAt(Math.floor(Math.random() * charNum.length));
        }
        if(i == 3){
            password += charSpecial.charAt(Math.floor(Math.random() * charSpecial.length));
        }
        var char = charAll[Math.floor(Math.random() * charAll.length)];
        password += char;
    }
    return password;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>password</category>
      <category>random</category>
    </item>
  </channel>
</rss>
