<?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: Nikhil Kadam</title>
    <description>The latest articles on Forem by Nikhil Kadam (@nickk2305).</description>
    <link>https://forem.com/nickk2305</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%2F478876%2F8bda697b-cb2a-4a4f-941d-47b7294234c7.jpg</url>
      <title>Forem: Nikhil Kadam</title>
      <link>https://forem.com/nickk2305</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nickk2305"/>
    <language>en</language>
    <item>
      <title>WTF is Asynchronous JavaScript: async/await vs .then()</title>
      <dc:creator>Nikhil Kadam</dc:creator>
      <pubDate>Sun, 17 Sep 2023 19:37:41 +0000</pubDate>
      <link>https://forem.com/nickk2305/wtf-is-asynchronous-javascript-asyncawait-vs-then-57kg</link>
      <guid>https://forem.com/nickk2305/wtf-is-asynchronous-javascript-asyncawait-vs-then-57kg</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hey there! Welcome back to the new blog post in the &lt;strong&gt;"&lt;a href="https://nickk2305.hashnode.dev/series/javascript-series"&gt;WTF is JavaScript&lt;/a&gt;"&lt;/strong&gt; series.&lt;/p&gt;

&lt;p&gt;In today's article, we will understand the asynchronous capabilities of JavaScript. Also, we will learn about how to use .then() and async/await in your code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://nickk2305.hashnode.dev/series/javascript-series"&gt;&lt;strong&gt;WTF is JavaScript Series&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;👈🏻&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this series, we are exploring key modern JavaScript concepts, one topic at a time. Check out the series to find more such insightful content and improve your JavaScript skills.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Is JavaScript Synchronous or Asynchronous?
&lt;/h2&gt;

&lt;p&gt;JavaScript is both synchronous and asynchronous, depending on how it's used. This duality arises from JavaScript's single-threaded execution model and its need to handle tasks that may take some time to complete, such as fetching data from a server or reading a file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dV0asAbM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://c.tenor.com/BLo6Zi2lLVAAAAAd/tenor.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dV0asAbM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://c.tenor.com/BLo6Zi2lLVAAAAAd/tenor.gif" alt="image load" width="640" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Synchronous JavaScript&lt;/strong&gt; :&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JavaScript is inherently synchronous by nature, which means it executes code line by line, one after the other.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Synchronous operations block the execution of subsequent code until the current operation is completed. For example, if you have a long-running calculation, it will prevent other tasks from running until it's finished.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous JavaScript&lt;/strong&gt; :&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JavaScript also supports asynchronous operations to prevent blocking. This is essential for tasks like making HTTP requests, reading files, or waiting for user input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asynchronous operations allow JavaScript to start a task, and then move on to other tasks without waiting for the first task to complete. When the asynchronous task finishes, it triggers a callback function or resolves a promise.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It means that JavaScript is synchronous by default to ensure predictable execution but provides mechanisms like callbacks, promises, and async/await to handle asynchronous operations efficiently and maintain responsiveness in web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understand async programming with .then() and async/await
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Vcp0ts90--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://c.tenor.com/5uGJ60CkaCoAAAAd/tenor.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Vcp0ts90--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://c.tenor.com/5uGJ60CkaCoAAAAd/tenor.gif" alt="theres-two-ways-to-look-at-this-gif" width="498" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is where &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;async/await&lt;/code&gt; come into play. They provide structured ways to work with asynchronous operations, allowing developers to write code that's easier to understand, manage, and maintain while ensuring responsiveness.&lt;/p&gt;

&lt;h2&gt;
  
  
  The .then() Approach: Old But Gold
&lt;/h2&gt;

&lt;p&gt;Let's start with the &lt;code&gt;.then()&lt;/code&gt; approach, which is based on Promises JavaScript feature introduced to handle asynchronous operations.&lt;/p&gt;

&lt;p&gt;Suppose you want to fetch and display a list of user names from an API. Here's how you might use &lt;code&gt;.then()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/users&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;usernames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&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;usernames&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Output:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bret&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;Antonette&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;Samantha&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;Karianne&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;Kamren&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;Leopoldo_Corkery&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;Elwyn.Skiles&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;Maxime_Nienow&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;Delphine&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;Moriah.Stanton&lt;/span&gt;&lt;span class="dl"&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;Here's how it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We fetch data from the API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We use &lt;code&gt;.then()&lt;/code&gt; to handle the response.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We convert the response to JSON.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We log the data or handle errors with &lt;code&gt;.catch()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Problems with .then() - Callback Hell
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;.then()&lt;/code&gt; is useful, but it can become problematic when you have multiple asynchronous tasks or deeply nested operations. This situation is often referred to as "callback hell" because the code becomes difficult to read and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you need to fetch user data and their posts, and then log both. Here's how it might look in callback hell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/users/1&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userResponse&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;userResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;userData&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://jsonplaceholder.typicode.com/posts?userId=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;postsResponse&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;postsResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;postsData&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User Data:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userData&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User Posts:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;postsData&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&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;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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the code becomes deeply nested and challenging to follow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9CqZjfI7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://c.tenor.com/vRTTriYBc8wAAAAd/tenor.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9CqZjfI7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://c.tenor.com/vRTTriYBc8wAAAAd/tenor.gif" alt="a-little-complicated-complex-gif" width="498" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to async/await: simpler way
&lt;/h2&gt;

&lt;p&gt;To address the issues of callback hell and simplify asynchronous code, JavaScript introduced &lt;code&gt;async/await&lt;/code&gt;. It allows developers to write asynchronous code in a more linear and structured way.&lt;/p&gt;

&lt;h3&gt;
  
  
  How async/await Helps
&lt;/h3&gt;

&lt;p&gt;Here are a few key benefits of &lt;code&gt;async/await&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability&lt;/strong&gt; : Asynchronous code can be written more like synchronous code, making it easier for developers to follow the flow of the program.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Error Handling&lt;/strong&gt; : Errors are easier to handle with &lt;code&gt;try...catch&lt;/code&gt; blocks, leading to more robust and maintainable code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Structured&lt;/strong&gt; : Code becomes more structured and organized, reducing the need for deeply nested callbacks.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's see how &lt;code&gt;async/await&lt;/code&gt; works with an example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Example with Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchUsernames&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&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;response&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;usernames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&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;usernames&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="p"&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;error&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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;fetchUsernames&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's how async/await works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We declare an &lt;code&gt;async&lt;/code&gt; function &lt;code&gt;fetchUsernames&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the function, we use &lt;code&gt;await&lt;/code&gt; to pause execution until the promise is resolved.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We handle success and errors using a simple &lt;code&gt;try...catch&lt;/code&gt; block.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Output:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bret&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;Antonette&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;Samantha&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;Karianne&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;Kamren&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;Leopoldo_Corkery&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;Elwyn.Skiles&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;Maxime_Nienow&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;Delphine&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;Moriah.Stanton&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Why async/await shines?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As you can see, the code is more structured and easier to understand compared to the &lt;code&gt;.then()&lt;/code&gt; approach. It reads like a series of steps, from fetching the data to logging the usernames.&lt;/p&gt;

&lt;p&gt;It's clear, organized, and easier to handle errors. No callback hell, just smooth, readable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Async/await vs .then(): what to choose
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;When to Choose&lt;/strong&gt; &lt;code&gt;.then()&lt;/code&gt;:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Familiarity&lt;/strong&gt; : If you or your team are already comfortable with using &lt;code&gt;.then()&lt;/code&gt;, and the codebase predominantly uses it, sticking with it may be the simplest choice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple Sequences&lt;/strong&gt; : For straightforward, single asynchronous operations, such as fetching data from an API or making a single HTTP request, &lt;code&gt;.then()&lt;/code&gt; can provide a clear and concise way to handle the task.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chaining&lt;/strong&gt; : &lt;code&gt;.then()&lt;/code&gt; allows for easy chaining of multiple asynchronous operations in a linear manner, which can be effective for simpler scenarios.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;When to Choose&lt;/strong&gt; &lt;code&gt;async/await&lt;/code&gt;:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability&lt;/strong&gt; : &lt;code&gt;async/await&lt;/code&gt; code reads more like synchronous code, making it easier to understand and maintain, especially for complex asynchronous logic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Error Handling&lt;/strong&gt; : &lt;code&gt;async/await&lt;/code&gt; simplifies error handling with &lt;code&gt;try...catch&lt;/code&gt; blocks, leading to cleaner and more robust code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Structured Code&lt;/strong&gt; : If you need to work with multiple asynchronous operations, &lt;code&gt;async/await&lt;/code&gt; provides a structured way to handle them, reducing the risk of callback hell.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Variable Scope&lt;/strong&gt; : Variables declared with &lt;code&gt;await&lt;/code&gt; are limited to the scope of the &lt;code&gt;async&lt;/code&gt; function, preventing accidental variable leakage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Debugging&lt;/strong&gt;: Debugging is typically easier with &lt;code&gt;async/await&lt;/code&gt; because the code execution follows a more predictable, linear flow.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UjUCJIld--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://c.tenor.com/l4_TliXxwj8AAAAd/tenor.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UjUCJIld--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://c.tenor.com/l4_TliXxwj8AAAAd/tenor.gif" alt="the-choice-is-yours-gif" width="498" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Practical Tips for Easier Coding&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For .then():&lt;/strong&gt; Give your functions clear names, so it's like reading a recipe book.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For async/await:&lt;/strong&gt; Use arrow functions to make your code shorter and sweeter.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;In this introductory article, we explored JavaScript's synchronous and asynchronous nature.&lt;/p&gt;

&lt;p&gt;We saw how &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;async/await&lt;/code&gt; are essential tools for managing asynchronous operations. &lt;code&gt;.then()&lt;/code&gt; is useful but can lead to callback hell in complex scenarios.&lt;/p&gt;

&lt;p&gt;In contrast, &lt;code&gt;async/await&lt;/code&gt; simplifies asynchronous code, making it more readable and structured.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading!
&lt;/h2&gt;

&lt;p&gt;That's it for this blog post, and stay tuned for upcoming blogs in the &lt;strong&gt;WTF is Javascript&lt;/strong&gt; series. Thank you for reading this article!&lt;/p&gt;

&lt;p&gt;If you enjoyed this article, be sure to &lt;a href="https://nickk2305.hashnode.dev/newsletter"&gt;subscribe to my newsletter&lt;/a&gt; to stay up-to-date with my content.&lt;/p&gt;

&lt;p&gt;And if you need any help or have any questions, don't hesitate to comment below! I'll be happy to help you out.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>async</category>
      <category>await</category>
      <category>asynchronous</category>
    </item>
    <item>
      <title>Mastering Middleware in Express.js: A Beginner's Guide</title>
      <dc:creator>Nikhil Kadam</dc:creator>
      <pubDate>Fri, 03 Feb 2023 18:53:58 +0000</pubDate>
      <link>https://forem.com/nickk2305/mastering-middleware-in-expressjs-a-beginners-guide-5a4f</link>
      <guid>https://forem.com/nickk2305/mastering-middleware-in-expressjs-a-beginners-guide-5a4f</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Hey there! Welcome to a new blog post about middleware in Express.js!&lt;/p&gt;

&lt;p&gt;If you're new to Express.js, you might not be familiar with the concept of middleware, but it is an important aspect of the framework that is worth understanding.&lt;/p&gt;

&lt;p&gt;In this blog post, we'll be covering everything you need to know about middleware in Express.js, including what it is, how it works, and how to use it in your routes. So, let's get started!&lt;/p&gt;

&lt;h1&gt;
  
  
  What is middleware? 🤔
&lt;/h1&gt;

&lt;p&gt;Middleware in Express.js is a function that is executed between the incoming request and the outgoing response.&lt;/p&gt;

&lt;p&gt;It can be used to modify the request and response objects, perform tasks such as authentication and validation, and even terminate the request-response cycle.&lt;/p&gt;

&lt;h1&gt;
  
  
  How does it work? ⚙️
&lt;/h1&gt;

&lt;p&gt;Middleware is defined using the &lt;code&gt;app.use()&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;__
function, which takes a function as an argument. The function can have three arguments: the request object, the response object, and the next function in the middleware chain.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A request was made!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;next&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;This middleware function will be executed for every incoming request, and it will log a message to the console.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;next()&lt;/code&gt; function is used to move to the next middleware function in the chain, or to the final route handler if there are no more middleware functions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Types of middlewares
&lt;/h1&gt;

&lt;p&gt;There are several different types of middleware available in Express.js, each with its own specific use case.&lt;/p&gt;

&lt;p&gt;Here are a few examples:&lt;/p&gt;

&lt;h2&gt;
  
  
  Application-level middleware
&lt;/h2&gt;

&lt;p&gt;This type of middleware is executed for every incoming request, regardless of the route. It can be used to perform tasks such as logging, parsing request bodies, and setting response headers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Router-level middleware
&lt;/h2&gt;

&lt;p&gt;This type of middleware is specific to a particular router and is executed only for requests that match the router's path. It can be used to perform tasks such as authorization, validation, and error handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error-handling middleware
&lt;/h2&gt;

&lt;p&gt;This type of middleware is used to handle errors that occur in the application. It takes four arguments: the error object, the request object, the response object, and the next function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Third-party middleware
&lt;/h2&gt;

&lt;p&gt;There are many third-party middleware libraries available for Express.js, such as body-parser, cors, and helmet. These libraries provide additional functionality that can be easily integrated into your application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Using middleware in code 💻
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Routing example
&lt;/h2&gt;

&lt;p&gt;Middleware can be used in Express.js routes to perform tasks such as authentication, validation, and error handling.&lt;/p&gt;

&lt;p&gt;For example, consider the following route that requires authentication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/secret&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You have access to the secret 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;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorization&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;secret&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unauthorized&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="nf"&gt;next&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;In this code, &lt;code&gt;authenticate&lt;/code&gt; function is a middleware function that checks the authorization header of the request.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If the header is not set to &lt;code&gt;secret&lt;/code&gt;, the middleware function returns a 401 status code and a message saying "Unauthorized".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the header is set to &lt;code&gt;secret&lt;/code&gt;, the middleware function calls the &lt;code&gt;next()&lt;/code&gt; function, which moves on to the final route handler.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Chained middleware example
&lt;/h2&gt;

&lt;p&gt;Middleware functions can also be chained together, allowing you to perform multiple tasks in a single route.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/secret&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You have access to the secret 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;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorization&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Missing authorization header&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="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorization&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;secret&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unauthorized&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="nf"&gt;next&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;In this example, the &lt;code&gt;validate&lt;/code&gt; function is executed before the &lt;code&gt;authenticate&lt;/code&gt; function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If the authorization header is missing, the &lt;code&gt;validate&lt;/code&gt; function returns a 400 status code and a message saying "Missing authorization header".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the header is present, the &lt;code&gt;validate&lt;/code&gt; function calls the &lt;code&gt;next()&lt;/code&gt; function, which moves on to the &lt;code&gt;authenticate&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Just another example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I see you making that request...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I see you made it here...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This middleware function logs a message to the console and waits for one second before calling the &lt;code&gt;next()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;The resulting delay might be frustrating for users, but it can be a useful technique for simulating slow network conditions during testing. Just don't use it in production! 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom middleware example
&lt;/h2&gt;

&lt;p&gt;It's easy to write your own custom middleware in Express.js. All you need to do is define a function and pass it to the &lt;code&gt;app.use()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; request made to &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;originalUrl&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This middleware function will log a message to the console every time a request is made to the server. You can also specify a path for the middleware function to only be executed for requests matching that path. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This middleware function will only be executed for requests made to the &lt;code&gt;/api&lt;/code&gt; path.&lt;/p&gt;

&lt;h1&gt;
  
  
  Best practices for using middleware ✅
&lt;/h1&gt;

&lt;p&gt;Here are a few best practices for using middleware in Express.js:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Keep middleware functions small and focused on a single task. This will make them easier to understand and reuse.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the &lt;code&gt;next()&lt;/code&gt; function to move to the next middleware function or route handler. If you forget to call &lt;code&gt;next()&lt;/code&gt;, the request-response cycle will be terminated and the client will not receive a response.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't modify the request and response objects in place. Instead, create new objects and assign them to the request and response objects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This will prevent unintended side effects and make your code easier to understand.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use router-level middleware for tasks that are specific to a particular route, and use application-level middleware for tasks that are global to the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consider using third-party middleware libraries to save time and avoid reinventing the wheel.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion ✍🏻
&lt;/h1&gt;

&lt;p&gt;In this blog post, we've covered the basics of middleware in Express.js, including what it is, how it works, and how to use it in your routes.&lt;/p&gt;

&lt;p&gt;We've also looked at different types of middleware and best practices for using middleware in your application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Resources 📑
&lt;/h1&gt;

&lt;p&gt;If you want to learn more about middleware in Express.js, there are many great resources available online.&lt;/p&gt;

&lt;p&gt;Some good places to start include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://expressjs.com/en/guide/using-middleware.html" rel="noopener noreferrer"&gt;The Express.js documentation on middleware&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?t=22251&amp;amp;v=TNV0_7QRDwY&amp;amp;feature=youtu.be" rel="noopener noreferrer"&gt;Middleware in Node and Express Tutorial&lt;/a&gt; by freeCodeCamp&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Thank you for reading! 🎉
&lt;/h1&gt;

&lt;p&gt;That's it for our blog post on middleware in Express.js. Thank you for reading this article!&lt;/p&gt;

&lt;p&gt;If you enjoyed this article, be sure to subscribe to my newsletter to stay up-to-date with my content.&lt;/p&gt;

&lt;p&gt;And if you need any help or have any questions, don't hesitate to reach out – I'm happy to help out. See you on &lt;a href="https://twitter.com/NickK2305" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;! 👋🏻&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>documentation</category>
      <category>api</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Getting Started with Docker - Introduction for Beginners</title>
      <dc:creator>Nikhil Kadam</dc:creator>
      <pubDate>Tue, 17 Jan 2023 13:23:00 +0000</pubDate>
      <link>https://forem.com/nickk2305/getting-started-with-docker-introduction-for-beginners-18h1</link>
      <guid>https://forem.com/nickk2305/getting-started-with-docker-introduction-for-beginners-18h1</guid>
      <description>&lt;h2&gt;
  
  
  What is Docker?
&lt;/h2&gt;

&lt;p&gt;Docker is an open-source containerization platform that allows developers to package and deploy applications in a standardized and isolated environment. It uses containers, which are lightweight, standalone, and executable packages that contain everything an application needs to run, including the application code, libraries, dependencies, and runtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction to Docker
&lt;/h3&gt;

&lt;p&gt;Docker revolutionized the way applications are deployed and run by providing a standard and consistent environment that can be easily shared across development, testing, and production environments. It allows developers to focus on writing code and testing their applications without worrying about the underlying infrastructure and dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  History of Docker
&lt;/h3&gt;

&lt;p&gt;Docker was first released in 2013 by Solomon Hykes, the founder of dotCloud, a Platform as a Service (PaaS) provider. The initial release of Docker was based on the concept of Linux containers (LXC), which had been around for a while but had not gained much popularity. Docker made containers more accessible and easy to use by providing a user-friendly interface and tools for building, sharing, and running containers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we need Docker?
&lt;/h2&gt;

&lt;p&gt;There are several reasons why Docker has become so popular in the software development industry:&lt;/p&gt;

&lt;p&gt;It allows developers to easily package and deploy their applications in a standardized environment, reducing the risk of compatibility issues and errors when moving between different environments.&lt;/p&gt;

&lt;p&gt;It allows developers to easily share their applications and dependencies with other team members, ensuring that everyone is working with the same version of the code and dependencies.&lt;/p&gt;

&lt;p&gt;It allows developers to easily scale and deploy their applications to different environments, such as test, staging, and production, without worrying about the underlying infrastructure.&lt;/p&gt;

&lt;p&gt;It allows developers to easily test their applications in different environments and configurations, ensuring that they are working as expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features of Docker
&lt;/h2&gt;

&lt;p&gt;Some of the key features of Docker are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Containerization:&lt;/strong&gt; Docker allows developers to package their applications and dependencies into lightweight, standalone, and executable containers that can be easily shared and deployed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Portability:&lt;/strong&gt; Docker containers can be easily moved between different environments and platforms, such as local development environments, testing environments, staging environments, and production environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Isolation:&lt;/strong&gt; Docker containers are isolated from each other and the host system, ensuring that they do not interfere with each other or the host system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Standardization:&lt;/strong&gt; Docker provides a standard and consistent environment for developing, testing, and deploying applications, ensuring that applications work as expected in different environments.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Docker Architecture
&lt;/h2&gt;

&lt;p&gt;Docker uses a client-server architecture, with the Docker Engine being the server and the Docker client being the command-line interface (CLI) used to interact with the Docker Engine.&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%2Fjw3e7iqbhoxwbahq3s8u.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%2Fjw3e7iqbhoxwbahq3s8u.png" alt="Image description" width="800" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Docker Engine is responsible for building, running, and managing Docker containers. It consists of three main components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The Docker daemon, which is the background process that manages Docker containers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Docker REST API, which is the interface used by the Docker client to communicate with the Docker daemon.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Docker CLI, which is the command-line interface used to interact with the Docker daemon through the Docker REST API.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Docker client is a command-line interface that allows users to interact with the Docker daemon through the Docker REST API. It is used to build, run, and manage Docker containers.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Docker Works
&lt;/h2&gt;

&lt;p&gt;Docker works by using containers to package and isolate applications and their dependencies. When a developer writes an application, they can use the Docker CLI to build a Docker image, which is a lightweight, standalone package containing the application code, libraries, dependencies, and runtime.&lt;/p&gt;

&lt;p&gt;When a developer is ready to deploy their application, they can use the Docker CLI to create a Docker container from the Docker image. A Docker container is a lightweight, standalone, and executable package that contains everything the application needs to run, including the application code, libraries, dependencies, and runtime.&lt;/p&gt;

&lt;p&gt;To run a Docker container, the developer can use the Docker CLI to send a request to the Docker daemon, which will start the container and run the application inside it. The Docker daemon will also manage the container, ensuring that it has the resources it needs to run and that it is isolated from other containers and the host system.&lt;/p&gt;

&lt;p&gt;Here is a diagram illustrating the process of building, running, and managing Docker containers:&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%2Fnsmkgjvzykyg811atkn5.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%2Fnsmkgjvzykyg811atkn5.png" alt="Image description" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  docker build
&lt;/h3&gt;

&lt;p&gt;docker build is a command that is used to build an image from a Dockerfile. A Dockerfile is a text file that contains instructions for how to build an image. The docker build command reads the instructions in the Dockerfile and creates an image based on those instructions.&lt;/p&gt;

&lt;h3&gt;
  
  
  docker pull
&lt;/h3&gt;

&lt;p&gt;docker pull is a command that is used to pull an image from a Docker registry. A Docker registry is a host that stores Docker images. The docker pull command retrieves an image from the registry and saves it to the host machine.&lt;/p&gt;

&lt;h3&gt;
  
  
  docker run
&lt;/h3&gt;

&lt;p&gt;docker run is a command that is used to run a container based on a particular image. When you run a container, you are creating a new instance of the image that the container is based on.&lt;/p&gt;

&lt;p&gt;In addition to building and running Docker containers, developers can also use the Docker CLI to manage their Docker images and containers, including creating and modifying images, pushing images to Docker Hub (a registry for Docker images), and pulling images from Docker Hub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion (TL;DR)
&lt;/h2&gt;

&lt;p&gt;Docker has revolutionized the way applications are developed, tested, and deployed by providing a standard and consistent environment that can be easily shared and scaled across different environments.&lt;/p&gt;

&lt;p&gt;Its containerization technology allows developers to package and deploy their applications in lightweight, standalone, and executable containers, ensuring that they are portable, isolated, and consistent.&lt;/p&gt;

&lt;p&gt;Its client-server architecture and command-line interface make it easy for developers to build, run, and manage Docker containers, allowing them to focus on writing code and testing their applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading! 🎉
&lt;/h2&gt;

&lt;p&gt;That's it for this blog post. Thank you for reading this article!&lt;/p&gt;

&lt;p&gt;If you enjoyed this article, be sure to subscribe to my newsletter to stay up-to-date with my content.&lt;/p&gt;

&lt;p&gt;And if you need any help or have any questions, don't hesitate to reach out – I'm happy to help out. See you on &lt;a href="//www.twitter.com/NickK2305"&gt;Twitter&lt;/a&gt;! 👋🏻&lt;/p&gt;

</description>
      <category>devmeme</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Getting Started with NodeJS - Introduction for Beginners</title>
      <dc:creator>Nikhil Kadam</dc:creator>
      <pubDate>Fri, 16 Dec 2022 18:12:06 +0000</pubDate>
      <link>https://forem.com/nickk2305/getting-started-with-nodejs-introduction-for-beginners-802</link>
      <guid>https://forem.com/nickk2305/getting-started-with-nodejs-introduction-for-beginners-802</guid>
      <description>&lt;h2&gt;
  
  
  What is NodeJS?
&lt;/h2&gt;

&lt;p&gt;Node.js is a popular JavaScript runtime that allows developers to build fast and scalable server-side applications. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for building real-time web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  History of NodeJS
&lt;/h2&gt;

&lt;p&gt;Node.js was created in 2009 by Ryan Dahl and has become one of the most popular and widely-used JavaScript runtime environments, especially for building web servers and networking applications. It is built on Chrome's V8 JavaScript engine, which allows it to execute JavaScript code at high speeds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should we use NodeJS?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fast and efficient:&lt;/strong&gt; Node.js is built on Chrome's V8 JavaScript engine, which makes it fast and efficient for running JavaScript code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Asynchronous and non-blocking:&lt;/strong&gt; Node.js uses an event-driven, non-blocking I/O model, which makes it well-suited for real-time applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large ecosystem of open-source libraries:&lt;/strong&gt; Node.js has a large ecosystem of open-source libraries, known as "modules", which can be easily installed and used in your applications. These modules provide a wide range of functionality, from parsing HTTP requests to interacting with databases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easy to learn:&lt;/strong&gt; If you are already familiar with JavaScript, then learning Node.js will be relatively easy. This makes it a great choice for developers who want to build web applications using a language they already know.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Great for microservices:&lt;/strong&gt; Node.js is well-suited for building microservices, which are small, independent services that work together to build larger applications. This architecture allows for better scalability and maintainability.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to Install NodeJS?
&lt;/h2&gt;

&lt;p&gt;To get started with Node.js, you will need to have a basic understanding of JavaScript and web development concepts. You will also need to have Node.js installed on your machine. You can download the latest version of Node.js from the official website (&lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;&lt;strong&gt;https://nodejs.org/&lt;/strong&gt;&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction to REPL
&lt;/h3&gt;

&lt;p&gt;Once you have Node.js installed, you can start building your first Node.js application. One of the easiest ways to get started is to use the Node.js REPL (Read-Eval-Print-Loop). The REPL allows you to run JavaScript commands in the terminal and see the results immediately.&lt;/p&gt;

&lt;p&gt;To start the REPL, simply open up your terminal and type &lt;code&gt;node&lt;/code&gt;. You should see a prompt where you can enter JavaScript commands. For example, you can try entering &lt;code&gt;1 + 1&lt;/code&gt; and pressing enter. You should see the result &lt;code&gt;2&lt;/code&gt; printed in the terminal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating your First NodeJS App
&lt;/h3&gt;

&lt;p&gt;To create a more advanced Node.js application, you will need to use a text editor and create a &lt;code&gt;.js&lt;/code&gt; file. The most basic Node.js application looks like this:&lt;br&gt;
&lt;/p&gt;

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

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) =&amp;gt; {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () =&amp;gt; {
  console.log(`Server running at http://${hostname}:${port}/`);
});

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

&lt;/div&gt;



&lt;p&gt;This code creates an HTTP server that listens on port 3000 and returns "Hello World" when someone visits the site. You can run this code by saving it to a file, such as &lt;code&gt;app.js&lt;/code&gt;. Now, you can try running &lt;code&gt;node app.js&lt;/code&gt; in the terminal. If you visit &lt;code&gt;http://localhost:3000&lt;/code&gt; in your web browser, you should see the message "Hello World" displayed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Congrats! You build your first NodeJS app!🎉&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Introduction to In-built Modules
&lt;/h3&gt;

&lt;p&gt;Node.js also has a large ecosystem of open-source libraries, known as "modules", which can be easily installed and used in your Node.js applications. These modules provide a wide range of functionality, from parsing HTTP requests to interacting with databases.&lt;/p&gt;

&lt;p&gt;Node.js also has a built-in module called &lt;code&gt;fs&lt;/code&gt; (File System) that allows you to read and write files on the server. This is useful for storing data or serving static files, such as images or CSS files.&lt;/p&gt;

&lt;p&gt;Here is an example of how to read a file using the &lt;code&gt;fs&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;

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

fs.readFile('/path/to/file.txt', 'utf8', (err, data) =&amp;gt; {
  if (err) throw err;
  console.log(data);
});

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

&lt;/div&gt;



&lt;p&gt;And here is an example of how to write to a file:&lt;br&gt;
&lt;/p&gt;

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

const data = 'Hello World';

fs.writeFile('/path/to/file.txt', data, 'utf8', (err) =&amp;gt; {
  if (err) throw err;
  console.log('The file has been saved!');
});

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

&lt;/div&gt;



&lt;p&gt;This code will write the string "Hello World" to a file called &lt;code&gt;file.txt&lt;/code&gt;. If the file does not exist, it will be created. If the file already exists, it will be overwritten with the new data.&lt;/p&gt;

&lt;p&gt;In addition to its built-in modules, Node.js has a vast ecosystem of open-source libraries that can be easily installed and used in your applications. These libraries provide a wide range of functionality, from parsing HTTP requests to interacting with databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Resources to learn about NodeJS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nodejs.org/en/docs/" rel="noopener noreferrer"&gt;Documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://youtu.be/fBNz5xF-Kx4" rel="noopener noreferrer"&gt;Node.js Crash Course&lt;/a&gt; - By Traversy Media&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://youtube.com/playlist?list=PL4cUxeGkcC9jsz4LDYc6kv3ymONOKxwBU" rel="noopener noreferrer"&gt;Node.js Crash Course Tutorial&lt;/a&gt; - By The Net Ninja&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.theodinproject.com/paths/full-stack-javascript/courses/nodejs" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By taking advantage of these resources, you can become proficient in Node.js and build sophisticated web applications that can handle a large number of concurrent connections and real-time data.&lt;/p&gt;

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

&lt;p&gt;In conclusion, Node.js is a powerful and widely-used JavaScript runtime environment for building scalable network applications. Its event-driven, non-blocking I/O model and a large ecosystem of open-source libraries make it an excellent choice for building a wide range of web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks for reading! 🎉
&lt;/h2&gt;

&lt;p&gt;That's it for this blog post. Thank you for reading this article!&lt;/p&gt;

&lt;p&gt;If you enjoyed this article, be sure to subscribe to my newsletter to stay up-to-date with my content.&lt;/p&gt;

&lt;p&gt;And if you need any help or have any questions, don't hesitate to reach out – I'm happy to help you. See you on &lt;a href="https://www.twitter.com/NickK2305" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;! 👋🏻&lt;/p&gt;

</description>
      <category>node</category>
      <category>introductionforbeginners</category>
      <category>mern</category>
      <category>development</category>
    </item>
  </channel>
</rss>
