<?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: Dhruvang Gajjar</title>
    <description>The latest articles on Forem by Dhruvang Gajjar (@dhruvangg).</description>
    <link>https://forem.com/dhruvangg</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%2F350504%2F1f392b9b-9c81-402d-9c63-4fb7e60ca235.jpg</url>
      <title>Forem: Dhruvang Gajjar</title>
      <link>https://forem.com/dhruvangg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dhruvangg"/>
    <language>en</language>
    <item>
      <title>A routing system in JavaScript for Single Page Application</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Thu, 12 Dec 2024 09:52:59 +0000</pubDate>
      <link>https://forem.com/dhruvangg/a-routing-system-in-javascript-for-single-page-application-1k42</link>
      <guid>https://forem.com/dhruvangg/a-routing-system-in-javascript-for-single-page-application-1k42</guid>
      <description>&lt;h3&gt;
  
  
  🌐 Building a Simple Routing System in Vanilla JavaScript for SPAs
&lt;/h3&gt;

&lt;p&gt;In the world of Single Page Applications (SPAs), managing routes without a framework can seem daunting. But with Vanilla JavaScript, you can create a simple yet powerful routing system! 🚀&lt;/p&gt;




&lt;p&gt;Here’s a quick guide to get you started:&lt;/p&gt;

&lt;h3&gt;
  
  
  1️⃣ &lt;strong&gt;Understand the Basics of Routing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Routing in an SPA involves mapping a URL to a specific view or content. Instead of reloading the page, the app dynamically updates the content based on the URL.&lt;/p&gt;

&lt;h3&gt;
  
  
  2️⃣ &lt;strong&gt;Core Concepts&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hash-based Routing&lt;/strong&gt;: Uses &lt;code&gt;window.location.hash&lt;/code&gt; (e.g., &lt;code&gt;/#/home&lt;/code&gt;). It's simple and doesn't require server configuration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;History API&lt;/strong&gt;: Leverages &lt;code&gt;pushState&lt;/code&gt; and &lt;code&gt;popstate&lt;/code&gt; for cleaner URLs (e.g., &lt;code&gt;/home&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3️⃣ &lt;strong&gt;Steps to Build&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 1: Set Up Your HTML Structure&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;nav&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#/about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;About&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#/contact"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"view"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Step 2: Create the Router&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Router&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hashchange&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;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handleRouteChange&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handleRouteChange&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;handleRouteChange&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;currentPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;route&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentPath&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;route&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/404&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;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&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;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Home Page&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/about&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;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;About Page&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/404&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;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;404 Page&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;h3&gt;
  
  
  4️⃣ &lt;strong&gt;Enhancements&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use the &lt;strong&gt;History API&lt;/strong&gt; for cleaner URLs.&lt;/li&gt;
&lt;li&gt;Lazy-load content or templates for better performance.&lt;/li&gt;
&lt;li&gt;Add 404 and fallback routes for improved user experience.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Build This Yourself?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deep Understanding&lt;/strong&gt;: Learn the mechanics behind routing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: Customize it to your needs without the constraints of a library.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt;: Avoid the overhead of external dependencies.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;💡 &lt;strong&gt;Pro Tip&lt;/strong&gt;: If you're building a larger app, consider modularizing your code and handling edge cases like query parameters or nested routes.&lt;/p&gt;

&lt;p&gt;Have you ever built your own routing system? Share your experience in comments! 👇&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>softwareengineering</category>
      <category>learning</category>
    </item>
    <item>
      <title>Automatic batching for better performance</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Tue, 02 May 2023 13:20:08 +0000</pubDate>
      <link>https://forem.com/dhruvangg/automatic-batching-for-better-performance-19f1</link>
      <guid>https://forem.com/dhruvangg/automatic-batching-for-better-performance-19f1</guid>
      <description>&lt;h3&gt;
  
  
  What is batching?
&lt;/h3&gt;

&lt;p&gt;Batching is when React groups multiple state updates into a single re-render for better performance.&lt;/p&gt;

&lt;p&gt;React has always combined two state updates that occur inside the same click event into a single re-render. You can notice that React only renders once for each click even though the state was set twice if you execute the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  const [count, setCount] = useState(1);
  const [flag, setFlag] = useState(true);

  function handleClick() {
    setCount((count) =&amp;gt; count + 1);
    setFlag((flag) =&amp;gt; !flag);
  }

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;h1&amp;gt;
        {count} is {flag ? "odd" : "even"}
      &amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Click&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is automatic batching?
&lt;/h3&gt;

&lt;p&gt;Starting in React 18 with createRoot, No matter where they come from, all changes will be automatically batched.&lt;/p&gt;

&lt;p&gt;This means that updates inside of timeouts, promises, native event handlers or any other event will batch the same way as updates inside of React events&lt;/p&gt;

&lt;p&gt;Also If you need to re-render the component, you can disable automatic batching by using &lt;code&gt;flushSync&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fq1lj6cjtpfb26fj6xkcp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fq1lj6cjtpfb26fj6xkcp.png" alt="Use flushsync to opt-out automatic batching" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all about React batching. &lt;/p&gt;

&lt;p&gt;If you want to discuss things related to Web Technology or want to explore more, You can always reach me out at &lt;a href="mailto:dhruvangg@gmail.com"&gt;dhruvangg@gmail.com&lt;/a&gt; OR &lt;a href="https://twitter.com/champdecay" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;br&gt;
&lt;a href="https://www.linkedin.com/in/dhruvangg/" rel="noopener noreferrer"&gt;LinkedIN&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>performance</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Boost User Satisfaction with a Dynamic Progress Bar for Your JavaScript Fetch Calls</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Thu, 09 Feb 2023 10:14:25 +0000</pubDate>
      <link>https://forem.com/dhruvangg/boost-user-satisfaction-with-a-dynamic-progress-bar-for-your-javascript-fetch-calls-1oa4</link>
      <guid>https://forem.com/dhruvangg/boost-user-satisfaction-with-a-dynamic-progress-bar-for-your-javascript-fetch-calls-1oa4</guid>
      <description>&lt;p&gt;Creating a dynamic progress bar for your JavaScript fetch calls can greatly improve the user experience and boost satisfaction. Here are a few steps to get you started:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create an HTML structure: First, create an HTML structure for your progress bar. You can use a &lt;code&gt;div&lt;/code&gt; element with a width of 0% and a background color. This &lt;code&gt;div&lt;/code&gt; will represent the progress bar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use JavaScript fetch method: Next, use the JavaScript &lt;code&gt;fetch&lt;/code&gt; method to make the network request. The fetch method returns a &lt;code&gt;Promise&lt;/code&gt; that resolves to the Response object representing the response to your request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Track the progress of the fetch call: You can use the &lt;code&gt;Response.body&lt;/code&gt; property to access a &lt;code&gt;ReadableStream object&lt;/code&gt;, which represents the body of the response. You can then use the &lt;code&gt;ReadableStream.getReader()&lt;/code&gt; method to get a reader and track the progress of the fetch call by reading chunks of data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Update the progress bar: As you receive chunks of data, you can use JavaScript to update the width of the progress bar. For example, you can divide the total number of bytes received by the total number of bytes to be received, and multiply the result by 100 to get the percentage. You can then use this percentage to update the width of the progress bar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Show the completion of the fetch call: Finally, you can use JavaScript to show the completion of the fetch call. For example, you can change the background color of the progress bar to green when the fetch call is complete, or display a message to indicate that the data has been loaded.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these steps, you can create a dynamic progress bar for your JavaScript fetch calls that provides a better user experience and improves satisfaction.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/dhruvangg/embed/NWBVQLM?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>twitter</category>
    </item>
    <item>
      <title>Setup React App with esbuild</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Mon, 31 Oct 2022 08:28:32 +0000</pubDate>
      <link>https://forem.com/dhruvangg/setup-react-app-with-esbuild-3m8o</link>
      <guid>https://forem.com/dhruvangg/setup-react-app-with-esbuild-3m8o</guid>
      <description>&lt;p&gt;Usally we create our react application using "create-react-app" which takes too much time to setup and installing packages. Also It takes more time start the server. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://esbuild.github.io/" rel="noopener noreferrer"&gt;ESBuild&lt;/a&gt; is the solution of that frustrating issue. &lt;/p&gt;

&lt;p&gt;When we setup react application with "create-react-app", it will install many packages which takes more time. Also webpack is slow compared to esbuild.&lt;/p&gt;

&lt;p&gt;Let's start with initiating project by &lt;code&gt;npm init&lt;/code&gt;. Thereafter install &lt;code&gt;react&lt;/code&gt; and &lt;code&gt;react-dom&lt;/code&gt; using 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;npm i react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once react dependencies have been added, install dev dependencies &lt;code&gt;esbuild&lt;/code&gt; and &lt;code&gt;live-server&lt;/code&gt;. Here live-server will be used 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;npm i esbuild live-server --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once all the dependencies have been installed, create &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;src&lt;/code&gt; folder. All the react files should be placed inside &lt;code&gt;src&lt;/code&gt; folder and index.html file should be placed inside &lt;code&gt;public&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Folder Structure should be look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F3fck88xmavik531fnhnw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3fck88xmavik531fnhnw.png" alt="Image description" width="361" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now create a &lt;code&gt;server.js&lt;/code&gt; file in which we will define build related code. &lt;/p&gt;

&lt;p&gt;Code of &lt;code&gt;server.js&lt;/code&gt; should be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { build } = require("esbuild"),
    liveServer = require("live-server");

build({
    entryPoints: ['./src/index.js'],
    outfile: './public/bundle.js',
    bundle: true,
    loader: {
        '.js': 'jsx'
    },
    watch: {
        onRebuild(error, result) {
            if (error) console.error('watch build failed:', error)
            else console.log('watch build succeeded:', result)
        },
    },
}).then(result =&amp;gt; {
    liveServer.start({
        port: 8000,
        host: 'localhost',
        root: "public",
        open: true,
        watch: "everything",
        ignore: 'scss,my/templates',
        file: "index.html",
        wait: 1000,
        mount: [['/components', './node_modules']],
        logLevel: 2,
        middleware: [function (req, res, next) { next(); }]
    });
    console.log('watching...', result)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And That's all. Now just add your react code inside &lt;code&gt;src&lt;/code&gt; folder and us the &lt;code&gt;bundle.js&lt;/code&gt; in &lt;code&gt;public&lt;/code&gt; folder. &lt;/p&gt;

&lt;p&gt;Sharing &lt;a href="https://github.com/dhruvangg/react-esbuild-boilerplate" rel="noopener noreferrer"&gt;Github&lt;/a&gt; link for reference.&lt;/p&gt;

</description>
      <category>esbuild</category>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Handle common login for API and static page in node js</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Fri, 16 Jul 2021 02:12:02 +0000</pubDate>
      <link>https://forem.com/dhruvangg/handle-login-for-api-and-static-page-in-node-js-1in4</link>
      <guid>https://forem.com/dhruvangg/handle-login-for-api-and-static-page-in-node-js-1in4</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;div class="ltag__stackexchange--header"&gt;
          &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
          &lt;a href="https://stackoverflow.com/questions/68361204/handle-login-for-api-and-static-page-in-node-js" rel="noopener noreferrer"&gt;
            Handle login for API and static page in node js
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Jul 13 '21&lt;/span&gt;
            &lt;span&gt;Comments: 1&lt;/span&gt;
            &lt;span&gt;Answers: 1&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/68361204/handle-login-for-api-and-static-page-in-node-js" rel="noopener noreferrer"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          0
        &lt;/div&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;I'm working on MERN stack application. There are two types of endpoints in my applications. One for API and another for static routes (using handlebars in NodeJS). I have used the basic JWT token to secure the API endpoints which would be accessible by React App. But JWT is not…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    &lt;a href="https://stackoverflow.com/questions/68361204/handle-login-for-api-and-static-page-in-node-js" class="ltag__stackexchange--btn" rel="noopener noreferrer"&gt;Open Full Question&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>node</category>
      <category>api</category>
      <category>authentication</category>
      <category>jwt</category>
    </item>
    <item>
      <title>Ways to get user's geo location</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Mon, 19 Apr 2021 02:54:14 +0000</pubDate>
      <link>https://forem.com/dhruvangg/get-user-s-location-1h13</link>
      <guid>https://forem.com/dhruvangg/get-user-s-location-1h13</guid>
      <description>&lt;p&gt;There are a lot of functionalities where you have to get the user's location. &lt;/p&gt;

&lt;p&gt;Let's consider a simple scenario that you have to show the popup based on user's location. so you have to get the user's location to achieve the requirement. &lt;/p&gt;

&lt;p&gt;There are 2 ways to get the user's location. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using Geo IP API
&lt;/h3&gt;

&lt;p&gt;With this approach, You will have to use IP database. Get User's IP address and match that IP address in the IP database, and get related address details. Here If you don't have a backend or database you can use 3rd party API. There are many APIs available. &lt;br&gt;
Here is a list of Few IP API Providers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://ip-api.com/" rel="noopener noreferrer"&gt;https://ip-api.com/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ipapi.co/" rel="noopener noreferrer"&gt;https://ipapi.co/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ipapi.com/" rel="noopener noreferrer"&gt;https://ipapi.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All have their own limitation and different pricing so check documentation, terms and conditions before use. &lt;/p&gt;

&lt;h5&gt;
  
  
  Usage
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;axios.get(`http://ip-api.com/json/`).then(res =&amp;gt; {
    console.log(res.data)
}).catch(err =&amp;gt; {
    console.error(err)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Pros
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;User don't need user permission to get their location.&lt;/li&gt;
&lt;li&gt;Instant and simple user's geo location place.&lt;/li&gt;
&lt;li&gt;Free for limited requests&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Cons
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Less Accuracy of user's location&lt;/li&gt;
&lt;li&gt;Some 3rd party APIs are paid for commercial use.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using Geo Location API
&lt;/h3&gt;

&lt;p&gt;The Geolocation API allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information. It returns only geographical coordinates of user's location once they allow.&lt;/p&gt;

&lt;h5&gt;
  
  
  Usage
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(position =&amp;gt; {
        console.log(position.coords)
    });
} else {
    x.innerHTML = "Geolocation is not supported by this browser.";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By above code, User would get the popup as per below screenshot. &lt;br&gt;
&lt;a href="https://media2.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%2F5bltuvwrx3qdtq2eccdm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5bltuvwrx3qdtq2eccdm.png" alt="Geo Location API" width="477" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will return geolocation data only if user allow the popup.&lt;/p&gt;

&lt;h5&gt;
  
  
  Pros
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;100% Accurate Geo location &lt;/li&gt;
&lt;li&gt;Instant &lt;/li&gt;
&lt;li&gt;Available for free as It is native browser API.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Cons
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Only return coordinates&lt;/li&gt;
&lt;li&gt;Require 3rd party tool to convert coordinates to geo location&lt;/li&gt;
&lt;li&gt;Available only in secure contexts (https)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can use &lt;a href="https://www.openstreetmap.org" rel="noopener noreferrer"&gt;openstreetmap&lt;/a&gt; as tool to convert coordinates to geo location. It's an open source platform.&lt;/p&gt;

&lt;p&gt;So that's all about user's Geo location. Let me know your views by commenting on this post. &lt;/p&gt;

&lt;p&gt;I hope you found the article helpful. Feedbacks are greatly appreciated! 🙌&lt;/p&gt;

&lt;p&gt;Have an amazing day!&lt;/p&gt;

&lt;p&gt;🌎 Lets connect&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/dhruvangg" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/dhruvangg/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.instagram.com/champdecay/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>geolocation</category>
    </item>
    <item>
      <title>Relative Time Format</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Wed, 31 Mar 2021 11:45:35 +0000</pubDate>
      <link>https://forem.com/dhruvangg/relative-time-format-53dg</link>
      <guid>https://forem.com/dhruvangg/relative-time-format-53dg</guid>
      <description>&lt;p&gt;The relative Time format is often used in social media, blogs, and chat applications. Earlier we have to make some functions to get relative time. Now It can be achievable by Vanilla JavaScript. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Intl.RelativeTimeFormat()&lt;/code&gt; enables localized formatting of relative times without sacrificing performance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  A Quick Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rtf = new Intl.RelativeTimeFormat('en');

rtf.format(10, 'second');
// 'in 10 seconds'

rtf.format(-10, 'minute');
// '10 minutes ago'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Intl.RelativeTimeFormat()&lt;/code&gt; constructor creates &lt;code&gt;Intl.RelativeTimeFormat&lt;/code&gt; objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;new Intl.RelativeTimeFormat([locales[, options]])&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Parameters
&lt;/h3&gt;

&lt;h5&gt;
  
  
  locales [Optional]
&lt;/h5&gt;

&lt;p&gt;A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the locales argument, see the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation" rel="noopener noreferrer"&gt;Intl page&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  options [Optional]
&lt;/h5&gt;

&lt;p&gt;An object with some or all of the following properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;localeMatcher&lt;/strong&gt;
The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". For information about this option, see the Intl page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;numeric&lt;/strong&gt;
The format of output message. Possible values are:
"always" (default, e.g., 1 day ago),
or "auto" (e.g., yesterday). The "auto" value allows to not always have to use numeric values in the output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;style&lt;/strong&gt;
The length of the internationalized message. Possible values are:
"long" (default, e.g., in 1 month)
"short" (e.g., in 1 mo.),
or "narrow" (e.g., in 1 mo.). The narrow style could be similar to the short style for some locales.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example with some option properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });

rtf.format(-1, 'day');
// 'yesterday'

rtf.format(0, 'day');
// 'today'

rtf.format(1, 'day');
// 'tomorrow'

rtf.format(-1, 'week');
// 'last week'

rtf.format(0, 'week');
// 'this week'

rtf.format(1, 'week');
// 'next week'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, This is supported by all modern browsers. You can check compatibility &lt;a href="https://caniuse.com/?search=RelativeTimeFormat" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;References: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://v8.dev/features/intl-relativetimeformat" rel="noopener noreferrer"&gt;https://v8.dev/features/intl-relativetimeformat&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>How to detect Caps lock in JavaScript</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Sat, 27 Mar 2021 10:57:30 +0000</pubDate>
      <link>https://forem.com/dhruvangg/how-to-detect-caps-lock-in-javascript-5f08</link>
      <guid>https://forem.com/dhruvangg/how-to-detect-caps-lock-in-javascript-5f08</guid>
      <description>&lt;p&gt;To check the caps lock status, you use the &lt;code&gt;getModifierState()&lt;/code&gt; method of the &lt;code&gt;KeyboardEvent&lt;/code&gt; object:&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;getModifierState()&lt;/code&gt; method returns boolean. If modifier is active it returns &lt;code&gt;true&lt;/code&gt; otherwise &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is a sample example of &lt;code&gt;getModifierState()&lt;/code&gt; for password inputs where you can check the status of caps lock and return appropriate alert.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const password = document.querySelector("input[name=password]")

password.addEventListener('keyup', (event) =&amp;gt; {
    if (event.getModifierState('CapsLock')) {
        alert('CapsLock is on')
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Sticky Sidebar in Vanilla JavaScript</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Thu, 11 Mar 2021 07:44:18 +0000</pubDate>
      <link>https://forem.com/dhruvangg/sticky-sidebar-in-vanilla-javascript-nom</link>
      <guid>https://forem.com/dhruvangg/sticky-sidebar-in-vanilla-javascript-nom</guid>
      <description>&lt;p&gt;This code is built with Pure JavaScript without any dependencies.  It just count top and bottom edges of the element in which the element needs to be sticky. &lt;/p&gt;

&lt;p&gt;You can also check the &lt;a href="https://codepen.io/dhruvangg/full/WNoLRZq" rel="noopener noreferrer"&gt;Demo&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function offset(elt) {
  var rect = elt.getBoundingClientRect(), bodyElt = document.body;
  return {
    top: rect.top + bodyElt .scrollTop,
    left: rect.left + bodyElt .scrollLeft
  }
}

window.addEventListener("load", function(){
  if(document.querySelector("#sidebar")){
    const sidebar = document.querySelector("#sidebar");
    const footer = document.querySelector("section");
    const top = offset(sidebar).top;
    const footTop = offset(footer).top;
    const maxY = footTop - sidebar.offsetHeight

    window.addEventListener("scroll", function(){
      let y = document.scrollingElement.scrollTop;
      if (y &amp;gt; top) {
        if (y &amp;lt; maxY) {
          sidebar.classList.add("fixed")
          sidebar.removeAttribute('style');
        } else {
          sidebar.classList.remove("fixed")
          sidebar.setAttribute('style', 'position: absolute; top: '+(maxY - top)+'px');
        }
      } else {
        sidebar.classList.remove("fixed")
      }
    })
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>Equal Height in Vanilla JavaScript</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Mon, 08 Mar 2021 09:54:35 +0000</pubDate>
      <link>https://forem.com/dhruvangg/equal-height-in-vanilla-javascript-49ch</link>
      <guid>https://forem.com/dhruvangg/equal-height-in-vanilla-javascript-49ch</guid>
      <description>&lt;p&gt;If you want to add &lt;strong&gt;equal height&lt;/strong&gt; for the elements, you don't need any jQuery or JavaScript Plugins. Just add following script and use it for any elements. &lt;/p&gt;

&lt;p&gt;You can also check the &lt;a href="https://codepen.io/dhruvangg/full/ZEBqBKz" rel="noopener noreferrer"&gt;Demo&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setHeight(el, val) {
  if (typeof val === "function") val = val();
  if (typeof val === "string") el.style.height = val;
  else el.style.height = val + "px";
}

var equalheight = function(container){
  var currentTallest = 0,
      currentRowStart = 0,
      rowDivs = new Array(),
      $el,
      topPosition = 0;

  Array.from(document.querySelectorAll(container)).forEach((el,i) =&amp;gt; {
    el.style.height = "auto";
    topPostion = el.offsetTop;
    if(currentRowStart != topPostion){
      for (currentDiv = 0 ; currentDiv &amp;lt; rowDivs.length ; currentDiv++) {
        setHeight(rowDivs[currentDiv], currentTallest)
      }
      rowDivs.length = 0;
      currentRowStart = topPostion;
      currentTallest = parseFloat(getComputedStyle(el, null).height.replace("px", ""))
      rowDivs.push(el);
    } else {
      rowDivs.push(el);
      currentTallest = (currentTallest &amp;lt; parseFloat(getComputedStyle(el, null).height.replace("px", ""))) ? (parseFloat(getComputedStyle(el, null).height.replace("px", ""))) : (currentTallest);
    }
    for (currentDiv = 0 ; currentDiv &amp;lt; rowDivs.length ; currentDiv++) {
      setHeight(rowDivs[currentDiv], currentTallest)
    }
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't need to change above code. Just put following code below main code and repeat it as many time as you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.addEventListener("load", function(){
  equalheight('.blog-title')
})
window.addEventListener("resize", function(){
  setTimeout(function(){
    equalheight('.blog-title')
  })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code does not have any dependency. It is &lt;strong&gt;built in pure JavaScript&lt;/strong&gt;. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>Authentication in REST APIs</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Tue, 19 Jan 2021 11:00:17 +0000</pubDate>
      <link>https://forem.com/dhruvangg/authentication-in-rest-apis-20am</link>
      <guid>https://forem.com/dhruvangg/authentication-in-rest-apis-20am</guid>
      <description>&lt;p&gt;Built Authentication REST APIs using expressJS, MySQL, Sequelize and JWT. We will use following dependencies to built authentication APIs&lt;/p&gt;

&lt;h3&gt;
  
  
  Required Tool
&lt;/h3&gt;

&lt;p&gt;Let’s take a moment to review the tools we’re going to be using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;NodeJS&lt;/a&gt; : We’re going to use this to run JavaScript code on the server. I’ve decided to use the latest version of Node, v6.3.0 at the time of writing, so that we’ll have access to most of the new features introduced in ES6.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express&lt;/a&gt; : As per their website, Express is a “Fast, unopinionated, minimalist web framework for Node.js”, that we’re going to be building our Todo list application on.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;NPM&lt;/a&gt; : for the package management (both server, frontend, and development packages). It’ll be easier to maintain one package management system, than using NPM and Bower together.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.mysql.com/" rel="noopener noreferrer"&gt;MySQL&lt;/a&gt; : This is a powerful open-source database that we’re going to use to store our Todos.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://sequelize.org/" rel="noopener noreferrer"&gt;Sequelize&lt;/a&gt; : In addition, we’re going to use Sequelize, which is a database ORM that will interface with the Mysql database for us.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; : A Chrome app that we’ll use to practically test our API.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Create Project
&lt;/h3&gt;

&lt;p&gt;Let’s begin by setting up our workspace. &lt;br&gt;
You all are familiar with NPM. Before setup the project, open the terminal and check node and npm version. If version is displaying its means node and npm installed. If not then you must have to install the node and npm.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open CLI and go to the project directory&lt;/li&gt;
&lt;li&gt;Now type &lt;code&gt;npm init&lt;/code&gt; to initialize the node project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;entry point: (index.js)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Enter &lt;code&gt;app.js&lt;/code&gt;, or whatever you want the name of the main file to be. If you want it to be index.js, hit RETURN to accept the suggested default file name.&lt;br&gt;
This command will generate &lt;code&gt;package.json&lt;/code&gt; file in the project folder. &lt;/p&gt;
&lt;h3&gt;
  
  
  Setup Express
&lt;/h3&gt;

&lt;p&gt;Initially I will make routes for the project. Install Express and a few of it’s dependencies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open CLI and go to the project directory&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;npm i --save express cors body-parser dotenv&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;--save&lt;/code&gt; flag will save these packages to the dependencies section of your package.json file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a file in the root folder and call it &lt;code&gt;app.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In this file, let’s create our Express application.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express"),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    PORT = 8080;
require('dotenv').config()    

const app = express()
app.use(cors())
app.use(bodyParser.json())

app.get("/", (req, res) =&amp;gt; {
    res.json({ "message": "Hello ChampDecay" })
})
app.listen(PORT, async () =&amp;gt; {
    console.log(`App is running on http://localhost:${PORT}`);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The application will be run scuccessfully on &lt;code&gt;http://localhost:8080&lt;/code&gt;&lt;br&gt;&lt;br&gt;
We’ll need a way to restart the server every time we change something in our code. For that, we’ll use nodemon npm package.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;npm i nodemon&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then, open up your &lt;code&gt;package.json&lt;/code&gt; file and create a command to run the server. That command will be created under the scripts section. Edit your package.jsonin the scripts section as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
"scripts": {
    "dev": "nodemon app.js"
},
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try running the application by executing following command in cli. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;npm run dev&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and visiting &lt;code&gt;http://localhost:8080&lt;/code&gt;. You should see&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "message": "Hello ChampDecay"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point in time, your project structure should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root
├── app.js
├── package.json
└── node_modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sequelize Setup
&lt;/h3&gt;

&lt;p&gt;For this part, we are going to install MySQL.&lt;br&gt;
Next, we will require Sequelize. This is an ORM that will interface with the MYSQL database for us.&lt;br&gt;
We will use of the Sequelize CLI package to bootstrap the project for us. It will also help us generate database migrations.&lt;/p&gt;

&lt;p&gt;So Let’s install Sequelize CLI package.following command will install sequelize-cli globally&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;npm install -g sequelize-cli&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Now we will install Sequelize package, alongside its dependencies. Sequelize is an easy-to-use multi SQL dialect ORM for Node.js. We gonna use MySQL as our database. So let install Sequelize ORM and mysql2 dialect.
&amp;gt; &lt;code&gt;npm i sequelize mysql2&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Initialize sequelize
&lt;/h3&gt;

&lt;p&gt;Let's generate generate migrations, seeders, config and models directories and config file using sequelize cli. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;sequelize init&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Above command will generate boilerplate code in your project, Now the project structure should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root
├── app.js
├── package.json
├── config
│   └── config.json
├── migrations
├── models
│   └── index.js
└── seeders
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;models/index.js&lt;/code&gt; file, It establishes database connection by using &lt;code&gt;config/config.json&lt;/code&gt;. So let's configure &lt;code&gt;config.json&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;{
  "development": {
    "username": "root", // Database Username
    "password": null,   // Database Password
    "database": "blog", // Database Name
    "host": "127.0.0.1",// Database Host
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now as we want to create a authentication, we have to create a table for &lt;code&gt;Users&lt;/code&gt;. So we would generate migration and model for Users. Let's create model and migration by sequelize cli command as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;sequelize model:create --name User --attributes username:string,email:string,password:string&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This will generate &lt;code&gt;user.js&lt;/code&gt; file in model directory and &lt;code&gt;&amp;lt;timestamp&amp;gt;-create-user.js&lt;/code&gt; migration in migration directory. &lt;/p&gt;

&lt;p&gt;Generated &lt;code&gt;user.js&lt;/code&gt; model file will be look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) =&amp;gt; {
  class User extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  User.init({
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and generated &lt;code&gt;&amp;lt;timestamp&amp;gt;-create-user.js&lt;/code&gt; migration file will be look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict';
module.exports = {
  up: async (queryInterface, Sequelize) =&amp;gt; {
    await queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      username: {
        type: Sequelize.STRING
      },
      email: {
        type: Sequelize.STRING
      },
      password: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: async (queryInterface, Sequelize) =&amp;gt; {
    await queryInterface.dropTable('Users');
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have to add some settings in model and migration files like unique key and allow/disallow null values. So let's modify &lt;code&gt;user.js&lt;/code&gt; model file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
User.init({
    username: {
      type: DataTypes.STRING,
      unique: true,
      allowNull: false
    },
    email: {
      type: DataTypes.STRING,
      unique: true,
      allowNull: false
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, We have added &lt;code&gt;unique&lt;/code&gt; and &lt;code&gt;allowNull&lt;/code&gt; in fields and same as model, add these parameters in migration file as well. &lt;/p&gt;

&lt;p&gt;Finally, &lt;code&gt;models/user.js&lt;/code&gt; will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) =&amp;gt; {
  class User extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  User.init({
    username: {
      type: DataTypes.STRING,
      unique: true,
      allowNull: false
    },
    email: {
      type: DataTypes.STRING,
      unique: true,
      allowNull: false
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and &lt;code&gt;migrations/&amp;lt;timestamp&amp;gt;-create-user.js&lt;/code&gt; file will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict';
module.exports = {
  up: async (queryInterface, Sequelize) =&amp;gt; {
    await queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      username: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false
      },
      email: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false
      },
      password: {
        type: Sequelize.STRING,
        allowNull: false
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: async (queryInterface, Sequelize) =&amp;gt; {
    await queryInterface.dropTable('Users');
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After setting up model and migrations, we will setup routes and controllers for user.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup Route
&lt;/h3&gt;

&lt;p&gt;Create a file &lt;code&gt;routes/user.js&lt;/code&gt; at root directory of the project. &lt;br&gt;
It will be look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express'),
    router = express.Router();

router.post('/signup', (req, res) =&amp;gt; {
    res.json({ "msg": "Signup Route" })
});
router.post('/signin', (req, res) =&amp;gt; {
    res.json({ "msg": "Signin Route" })
});

module.exports = router
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have create two routes, one for signup and another for signin. now this file should be called in &lt;code&gt;app.js&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Add following code in &lt;code&gt;app.js&lt;/code&gt; before app.listen command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use('/api', require('./routes/user'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! We have setup routes for signup and signin. Our next step will be settingup the controller for user.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup Controller
&lt;/h3&gt;

&lt;p&gt;Here, we will use jsonwebtokens for api authentications, So let's install &lt;code&gt;bcryptjs&lt;/code&gt; and &lt;code&gt;jsonwebtoken&lt;/code&gt; packages. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;npm i bcryptjs jsonwebtoken&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After adding two packages, create a &lt;code&gt;controllers/user.js&lt;/code&gt; file and add following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const bcrypt = require("bcryptjs"),
    jwt = require('jsonwebtoken'),
    db = require("../models/index"),
    JWT_SECRET = process.env.JWT_SECRET

exports.signUp = async (req, res) =&amp;gt; {
    const { username, email, password: plainTextPassword } = req.body;
    const password = await bcrypt.hash(plainTextPassword, 10)
    try {
        const user = await db.User.create({
            username,
            email,
            password
        })
        res.status(201).json({ "status": "ok", "message": "User registered", user })
    } catch (error) {
        res.status(401).json({ "status": "error", "message": error.errors[0].message })
    }
}

exports.signIn = async (req, res) =&amp;gt; {
    const { email, password } = req.body;
    const user = await db.User.findOne({ where: { email: email } })
    if (!user) {
        return res.status(401).json({ status: 'error', error: 'Invalid username/password' })
    }
    if (await bcrypt.compare(password, user.password)) {
        const payload = { id: user.id, username: user.username };
        const options = { expiresIn: '2d', issuer: 'http://localhost:8080' };
        const secret = JWT_SECRET;
        const token = jwt.sign(payload, secret, options)
        return res.status(200).json({ status: 'ok', "message": "User signin successful", token })
    }
    return res.status(401).json({ "status": "error", "message": "Invalid Username/Password" })
}
exports.getUsers = async (req, res) =&amp;gt; {
    try {
        const users = await db.User.findAll()
        return res.status(200).json({ status: 'ok', users })
    } catch (error) {
        return res.status(401).json({ "status": "error", error })
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this file, we have imported &lt;code&gt;bcryptjs&lt;/code&gt;, &lt;code&gt;jsonwebtoken&lt;/code&gt; and &lt;code&gt;index&lt;/code&gt; model. We have define &lt;code&gt;JWT_SECRET&lt;/code&gt; variable which should be hidden and ideally fetch from &lt;code&gt;.env&lt;/code&gt; file. Thereafter we have exported two functions. &lt;/p&gt;

&lt;h5&gt;
  
  
  Signup function
&lt;/h5&gt;

&lt;p&gt;Here we get all request parameters and define it by &lt;code&gt;const { username, email, password: plainTextPassword } = req.body;&lt;/code&gt; Then we have to hashed the password so no one can see it from database. To hash the password, we have used &lt;strong&gt;bcrypt's hash&lt;/strong&gt; function. we have used 2 paramaters in hash function, first is plaintext password which should be encoded and second is salt. &lt;/p&gt;

&lt;p&gt;After it, we have to store the values including new hashed password to the database. so using sequelize's &lt;strong&gt;create&lt;/strong&gt; function we have stored it in databse. &lt;/p&gt;

&lt;h5&gt;
  
  
  Signin function
&lt;/h5&gt;

&lt;p&gt;Here we get all request parameters same as signup function. Thereafter we fetch row from users table of the database using &lt;strong&gt;findOne&lt;/strong&gt; function.&lt;br&gt;
If it doesn't return any row that means a user enters wrong email so we have responsed invalid message wirh 401 status.&lt;br&gt;
If It returns a row, we have to compare the password from database and user input. So again bcrypt will be used. the &lt;strong&gt;compare&lt;/strong&gt; function of bcrypt will do the comparisation. If comparisation is true, we will generate access token using jwt otherwise returns error message with 401 status. &lt;br&gt;
We have used username and id to create jwt token. Also we have set expire time and issuer of the token. &lt;/p&gt;
&lt;h5&gt;
  
  
  Get Users Function
&lt;/h5&gt;

&lt;p&gt;This function simply fetch users from database. But to access this route, user should pass valid access token with the request and to validate the access token we have to make a middle ware. &lt;br&gt;
Create a new file &lt;code&gt;middleware/auth.js&lt;/code&gt; and that file should have following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const jwt = require('jsonwebtoken');

module.exports = {
    validateToken: async (req, res, next) =&amp;gt; {
        const authHeader = req.headers.authorization;
        if (authHeader) {
            const token = authHeader.split(' ')[1];
            try {
                const result = await jwt.verify(token, process.env.JWT_SECRET)
                req.decoded = result;
                next()
            } catch (error) {
                return res.status(401).json({ "status": "error", "message": "Invalid Authentication.", error })
            }
        } else {
            return res.status(401).json({ "status": "error", "message": "Authentication error. Token required." })
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have imported only jsonwebtoken and create a function called &lt;code&gt;validateToken&lt;/code&gt;. It will take access token from the authorization headers and verify it using &lt;code&gt;jwt.verify()&lt;/code&gt; function. If it is verfied successfully, it will go for the next process by &lt;code&gt;next()&lt;/code&gt; function, otherwise it returns a error message of invalide access token with status code 401. &lt;/p&gt;

&lt;p&gt;So finally we have created controller and route. Let's connect route with the controller. Open &lt;code&gt;routes/user.js&lt;/code&gt; file and replace the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express'),
    router = express.Router(),
    User = require("../controllers/user")
router.post('/signup', User.signUp);
router.post('/signin', User.signIn);
router.get('/users', Auth.validateToken, User.getUsers);
module.exports = router
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in &lt;code&gt;/users&lt;/code&gt; route, we have used middleware as second argument and That's all. &lt;/p&gt;

&lt;p&gt;Finally, The code is ready..!! &lt;/p&gt;

&lt;p&gt;&lt;code&gt;This is my first post. Let me know your views by commenting on it.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://god.postman.co/run-collection/59d725a55a2ea7083b2a" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frun.pstmn.io%2Fbutton.svg" alt="Run in Postman" width="128" height="32"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dhruvangg/api-auth" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fforthebadge.com%2Fimages%2Fbadges%2Fcheck-it-out.svg" alt="forthebadge" width="154" height="35"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="mailto:dhruvangg@gmail.com"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimg.shields.io%2Fbadge%2FGmail-D14836%3Fstyle%3Dfor-the-badge%26logo%3Dgmail%26logoColor%3Dwhite" alt="Gmail" width="87" height="28"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>mysql</category>
      <category>api</category>
      <category>auth</category>
    </item>
    <item>
      <title>Match arrays in JavaScript</title>
      <dc:creator>Dhruvang Gajjar</dc:creator>
      <pubDate>Tue, 12 Jan 2021 11:07:46 +0000</pubDate>
      <link>https://forem.com/dhruvangg/match-arrays-in-javascript-121g</link>
      <guid>https://forem.com/dhruvangg/match-arrays-in-javascript-121g</guid>
      <description>&lt;h3&gt;
  
  
  Check if array A has all elements of array B
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Check if array A has any elements of array B
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


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