<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vaibhav Mehta</title>
    <description>The latest articles on DEV Community by Vaibhav Mehta (@mr_ali3n).</description>
    <link>https://dev.to/mr_ali3n</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%2F388165%2F408f2c22-7b1a-423f-be2d-65ba11deaa6e.jpeg</url>
      <title>DEV Community: Vaibhav Mehta</title>
      <link>https://dev.to/mr_ali3n</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mr_ali3n"/>
    <language>en</language>
    <item>
      <title>Getting Started with JavaScript Fetch API</title>
      <dc:creator>Vaibhav Mehta</dc:creator>
      <pubDate>Mon, 05 Dec 2022 06:07:09 +0000</pubDate>
      <link>https://dev.to/mr_ali3n/getting-started-with-javascript-fetch-api-1272</link>
      <guid>https://dev.to/mr_ali3n/getting-started-with-javascript-fetch-api-1272</guid>
      <description>&lt;h2&gt;
  
  
  Prelude
&lt;/h2&gt;

&lt;p&gt;Have you ever used &lt;code&gt;XMLHttpRequest&lt;/code&gt; object in JavaScript to send a request to the backend to fetch or post some data? If not, then lucky you, as it was pretty chaotic and complicated. &lt;/p&gt;

&lt;p&gt;Here's a quick example of what it takes to fetch a file from a given URL using &lt;code&gt;XMLHttpRequest&lt;/code&gt; object vs &lt;code&gt;$.ajax()&lt;/code&gt; in jQuery vs newly introduced native &lt;code&gt;fetch&lt;/code&gt; API.&lt;/p&gt;




&lt;h3&gt;
  
  
  Fetching Data using &lt;code&gt;XMLHttpRequest&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;xhr&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;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onreadystatechange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readyState&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DONE&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="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;responseText&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="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&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="s1"&gt;https://api.jsonbin.io/v3/b/6360e54a65b57a31e6a94a42&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;xhr&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="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks tedious right? That's not it, the above is mostly a minimal example of how to send a request using &lt;code&gt;XMLHttpRequest()&lt;/code&gt; object. Next, jQuery comes to the rescue by introducing &lt;code&gt;$.ajax()&lt;/code&gt;, a simplified version of the &lt;code&gt;XMLHttpRequest()&lt;/code&gt; as a cherry on top, introducing additional functionalities.&lt;/p&gt;




&lt;h3&gt;
  
  
  Fetching Data using &lt;code&gt;$.get()&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&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;https://api.jsonbin.io/v3/b/6360e54a65b57a31e6a94a42&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;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;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;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;jQuery simplified fetching the data to a great extent, compared to the native &lt;code&gt;XMLHttpRequest()&lt;/code&gt; object. Though it was easy, it came at some cost, as the jQuery lib weighed ~100kb (vague estimation) where even if you built the lib with ajax only module, it still weighed roughly ~50kb which is pretty high to do something as basic as fetching some JSON data.&lt;/p&gt;

&lt;p&gt;Before we dive into using &lt;code&gt;fetch()&lt;/code&gt; API, here's something important to take a note of:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The fetch specification differs from &lt;code&gt;jQuery.ajax()&lt;/code&gt; in the following significant ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The Promise returned from &lt;code&gt;fetch()&lt;/code&gt; won't reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, as soon as the server responds with headers, the Promise will resolve normally (with the ok property of the response set to false if the response isn't in the range 200–299), and it will only reject on network failure or if anything prevented the request from completing. &lt;sup&gt;[1]&lt;/sup&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Unless &lt;code&gt;fetch()&lt;/code&gt; is called with the &lt;code&gt;credentials&lt;/code&gt; option set to &lt;code&gt;include&lt;/code&gt;, &lt;code&gt;fetch()&lt;/code&gt; &lt;sup&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch" rel="noopener noreferrer"&gt;Quoting from MDN&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Won't send cookies in cross-origin requests.&lt;/li&gt;
&lt;li&gt;Won't set any cookies sent back in cross-origin responses.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Introducing the fetch() API
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fetch&lt;/code&gt; API is supported by most of the modern browsers today. It makes it easier for the devs to make asynchronous network requests as it's promise based, thus, avoids callback hell.&lt;/p&gt;

&lt;p&gt;Here's a simple example of making a &lt;code&gt;GET&lt;/code&gt; request using &lt;code&gt;fetch&lt;/code&gt; API.&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://api.jsonbin.io/v3/b/6360e54a65b57a31e6a94a42&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;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="nx"&gt;res&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="p"&gt;)&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;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could also use &lt;code&gt;async ... await&lt;/code&gt; instead of promises, like:&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="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&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://api.jsonbin.io/v3/b/6360e54a65b57a31e6a94a42&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;res&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="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;data&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 the above examples, &lt;code&gt;fetch()&lt;/code&gt; takes one argument which is the path to the resource you wish to fetch. An important thing to note here, the response does not return the JSON response body but it instead a representation of the entire HTTP response where you could use &lt;code&gt;.json()&lt;/code&gt; to fetch the actual response JSON data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch&lt;/code&gt; also provides various options which you could use for the response object, like, &lt;code&gt;text()&lt;/code&gt; which returns raw text, &lt;code&gt;formData()&lt;/code&gt;, &lt;code&gt;blob()&lt;/code&gt;, etc.&lt;/p&gt;



&lt;h2&gt;
  
  
  Error Handling
&lt;/h2&gt;

&lt;p&gt;When I first tried fetch, I used &lt;code&gt;.catch()&lt;/code&gt; assuming that my code would end up in the &lt;code&gt;.catch()&lt;/code&gt; statement if the request was a non 200. To my surprise, &lt;code&gt;.catch()&lt;/code&gt; never ran, even if the request failed with a response code of 4xx or 5xx. As mentioned previously in this post.&lt;/p&gt;

&lt;p&gt;For eg, the following URL would throw a 404 error.&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://api.jsonbin.io/v3/b/6360e54a65b57a31e6a94a4a&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;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="nx"&gt;res&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="p"&gt;)&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;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;err&lt;/span&gt;&lt;span class="p"&gt;)&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;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;Something went wrong&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;Here, my code didn't print &lt;code&gt;Something went wrong&lt;/code&gt;. Later, referring to the official documentation, it turns out that fetch doesn't throw an error if the server returns an error (4xx or 5xx).&lt;/p&gt;

&lt;p&gt;The only time my code would end up in the catch statement is when there's no internet connection, or similar issues. Here's where &lt;code&gt;response.ok&lt;/code&gt; comes to the rescue. Using &lt;code&gt;response.ok&lt;/code&gt; would return true if the response status code is between &lt;code&gt;200-299&lt;/code&gt; else false.&lt;/p&gt;

&lt;p&gt;In the above example, using &lt;code&gt;response.ok&lt;/code&gt; to check the request state would return &lt;code&gt;false&lt;/code&gt; as the response code was &lt;code&gt;404&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="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&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://api.jsonbin.io/v3/b/6360e54a65b57a31e6a94a42&lt;/span&gt;&lt;span class="dl"&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`'Request failed with status code: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;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;res&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="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;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Attaching Request Headers &amp;amp; Payload
&lt;/h2&gt;

&lt;p&gt;In the above examples, we tried to fetch these records using the &lt;strong&gt;default&lt;/strong&gt; &lt;code&gt;GET&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;But what if you wish to send a &lt;code&gt;POST&lt;/code&gt; or a &lt;code&gt;PUT&lt;/code&gt; request along with some &lt;code&gt;Headers&lt;/code&gt; attached to it?&lt;/p&gt;

&lt;p&gt;You could pass &lt;code&gt;headers&lt;/code&gt; and attach a payload to your &lt;code&gt;fetch&lt;/code&gt; request either by using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Headers" rel="noopener noreferrer"&gt;&lt;code&gt;Headers()&lt;/code&gt;&lt;/a&gt; object or you could simply pass these in an object as a second parameter to the &lt;code&gt;fetch&lt;/code&gt; API like:&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="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&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://api.jsonbin.io/v3/b/6360e54a65b57a31e6a94a42&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// *GET, POST, PUT, DELETE, etc.&lt;/span&gt;
    &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// no-cors, *cors, same-origin&lt;/span&gt;
    &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Request headers&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&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="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sample&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;json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="c1"&gt;// Request payload&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`'Request failed with status code: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;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;res&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="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;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;code&gt;fetch()&lt;/code&gt; vs. XHR
&lt;/h2&gt;

&lt;p&gt;There are a few differences between the &lt;code&gt;fetch&lt;/code&gt; API and XHR request. I didn't want to repeat or copy these since I myself got to know about these differences from a &lt;a href="https://stackoverflow.com/q/35549547/1542290" rel="noopener noreferrer"&gt;Stackoverflow answer&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Browser Support
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fetch()&lt;/code&gt; API now has a decent Browser Support. Here's a quick Browser Support overview from MDN:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1av2yhm0ji6l9tfbsnoz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1av2yhm0ji6l9tfbsnoz.png" alt="Browser Support for fetch() JavaScript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You could also use &lt;code&gt;fetch&lt;/code&gt; in NodeJS using &lt;code&gt;--experimental-fetch&lt;/code&gt; flag, or use the &lt;a href="https://www.npmjs.com/package/node-fetch" rel="noopener noreferrer"&gt;node-fetch&lt;/a&gt; package if needed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch" rel="noopener noreferrer"&gt;Using the fetch() API on MDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/35549547/fetch-api-vs-xmlhttprequest" rel="noopener noreferrer"&gt;Fetch API vs. XHR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://caniuse.com/fetch" rel="noopener noreferrer"&gt;Fetch API Browser Support on CanIUse&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://web.dev/introduction-to-fetch/" rel="noopener noreferrer"&gt;Introduction to fetch() API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>api</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Getting started with Deno.js</title>
      <dc:creator>Vaibhav Mehta</dc:creator>
      <pubDate>Thu, 17 Nov 2022 16:30:49 +0000</pubDate>
      <link>https://dev.to/mr_ali3n/getting-started-with-denojs-2hp0</link>
      <guid>https://dev.to/mr_ali3n/getting-started-with-denojs-2hp0</guid>
      <description>&lt;p&gt;I've been spending a lot of time exploring new tech, trying to make out the most of the Lockdown. From Vue &amp;amp; VueX to Service workers &amp;amp; IndexedDB, it has been a great journey so far. For a few days, there has been a lot of buzz in the engineering community around Deno (I pronounce it as Dee-no), a secure JavaScript &amp;amp; Typescript runtime developed by Ryan Dahl, who previously developed Node.js back in May 2009.&lt;/p&gt;

&lt;p&gt;I am working on Node.js for over three years now, and hence, I wanted to explore Deno since it's very similar to Node and wanted to understand how is it different from Node, and is it any better? At the time of writing this article, Deno announced the 1.0.0 release.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is Deno?
&lt;/h2&gt;

&lt;p&gt;Deno is a new runtime for Typescript (and Javascript), a part of it is written in Rust. As Ryan Dahl said in one of his &lt;a href="https://jaxenter.com/deno-interview-ryan-dahl-171425.html" rel="noopener noreferrer"&gt;interviews&lt;/a&gt;, that Deno acts more like a web browser for command-line scripts. Its security model is that to a browser where websites ask for permission to access the camera, location, etc.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Deno aims to replace utility scripts that were historically written with Bash or Python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Similarly, Deno scripts run in a secure sandbox without access to the operating system, where users can selectively grant access to the files to provide further functionality. Furthermore, it explicitly takes on the role of both runtime and the package manager.&lt;/p&gt;




&lt;h2&gt;
  
  
  Features of Deno and how are they different from Node.js
&lt;/h2&gt;

&lt;p&gt;If you've worked with Node.js previously, you'll find Deno quite similar to it but with some significant enhancements around security and the way we import the modules.&lt;/p&gt;

&lt;p&gt;Here are some of the features I tried out using Deno and I am highlighting some of them below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deno is written in Rust as compared to its predecessor, Node.js, which is written in C++.&lt;/li&gt;
&lt;li&gt;Unlike Node.js, it does not use &lt;code&gt;npm&lt;/code&gt; and instead uses modules that are referenced as URLs or file paths. It also embraces the use of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import" rel="noopener noreferrer"&gt;ES6 Modules&lt;/a&gt; over require statements. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//The Node.js way&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mypackage&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//The Deno 🦕 way&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://deno.land/std/SOME_MODULE.ts&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;ul&gt;
&lt;li&gt;Does not use &lt;code&gt;package.json&lt;/code&gt; in its module resolution algorithm.&lt;/li&gt;
&lt;li&gt;All &lt;strong&gt;async&lt;/strong&gt; actions in Deno return a &lt;strong&gt;promise&lt;/strong&gt;. Thus Deno provides different APIs than Node.&lt;/li&gt;
&lt;li&gt;Support for &lt;a href="https://v8.dev/features/top-level-await" rel="noopener noreferrer"&gt;&lt;code&gt;top-level&lt;/code&gt; await&lt;/a&gt; unlike Node.js where you need to define &lt;code&gt;async&lt;/code&gt; before you can use &lt;code&gt;await&lt;/code&gt;. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//The Node.js way&lt;/span&gt;
&lt;span class="k"&gt;async &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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;getData&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;URL&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="c1"&gt;//The Deno 🦕 way&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;getData&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;URL&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;ul&gt;
&lt;li&gt;Requires explicit permissions for files, network, and environment access. Think of a file requesting data from an external resource, and you need to pass a flag like &lt;code&gt;--allow-net&lt;/code&gt; to allow the file to fetch the data from an external resource. We will discuss more on this as we write a basic app.&lt;/li&gt;
&lt;li&gt;It exits if any uncaught errors are encountered. (😨 rip my code, seems like I'll be spending a lot of time on error handling 😂)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Typescript&lt;/strong&gt; by &lt;strong&gt;default&lt;/strong&gt;. It also eliminates the need for other tools like Babel to compile JavaScript and instead has built-in support.&lt;/li&gt;
&lt;li&gt;Deno provides a &lt;u&gt;&lt;a href="https://deno.land/std@0.165.0" rel="noopener noreferrer"&gt;standard library&lt;/a&gt;&lt;/u&gt; that does not have any external dependencies and is reviewed by the Deno core team. This consists of core modules like DateTime, File System handling, HTTP, etc.&lt;/li&gt;
&lt;li&gt;Lastly, it has built-in support for &lt;a href="https://deno.land/std@0.165.0/testing" rel="noopener noreferrer"&gt;necessary testing&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Up &amp;amp; Running
&lt;/h2&gt;

&lt;p&gt;There are numerous ways using which you can install Deno on your system. I'll share a few examples here:\&lt;/p&gt;

&lt;h3&gt;
  
  
  Using CURL (for macOS &amp;amp; Linux)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://deno.land/x/install/install.sh | sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Powershell (for Windows)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;iwr https://deno.land/x/install/install.ps1 &lt;span class="nt"&gt;-useb&lt;/span&gt; | iex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or you can use Homebrew on macOS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;deno
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkt3hkgzf450hwuqql5nc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkt3hkgzf450hwuqql5nc.gif" alt="Installing Deno using Homebrew"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more information, you can refer to the &lt;a href="https://deno.land/#installation" rel="noopener noreferrer"&gt;Deno Installation&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Is there a Deno Versioning Manager?
&lt;/h2&gt;

&lt;p&gt;As of now, there's no official documentation around the version manager. While searching further on this, I came across an open issue on the Deno repo for the same.&lt;/p&gt;

&lt;p&gt;In the above, a couple of developers have suggested version managers like &lt;a href="https://github.com/denoland/deno/issues/5214" rel="noopener noreferrer"&gt;asdf-deno&lt;/a&gt; and &lt;a href="https://github.com/axetroy/dvm" rel="noopener noreferrer"&gt;DVM&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hello World in Deno Land
&lt;/h2&gt;

&lt;p&gt;Let's take a quick look at a simple example that will highlight some critical features of Deno. You can type the commands directly in the REPL(Read-Execute-Print-Loop) mode, or we can write the code in the &lt;code&gt;.ts&lt;/code&gt; or a &lt;code&gt;.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Type deno in the terminal, which will enable the Deno REPL mode.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flqt0s93n2dr9u62a27mx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flqt0s93n2dr9u62a27mx.gif" alt="We are running a Deno code in REPL mode."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can run any JavaScript or TypeScript code (no need of a compiler for TypeScript as Deno natively supports TypeScript).&lt;/p&gt;

&lt;p&gt;In the above example, I've done some simple math operations like adding two integers and assigning an integer to a constant, which is further used to add to another number.&lt;/p&gt;

&lt;p&gt;Lastly, you can exit the REPL mode by typing &lt;code&gt;Deno.exit()&lt;/code&gt; or &lt;code&gt;close()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;REPL mode is not that useful when running large programs or code snippets, and this is where a file comes handy. We can write our code in a &lt;code&gt;.ts&lt;/code&gt; or &lt;code&gt;.js&lt;/code&gt; file and execute it using Deno. Let's create a simple &lt;code&gt;HelloWorld.ts&lt;/code&gt; file which will output a simple string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;HelloWorld.ts //create a file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add some code to it. Just a simple &lt;code&gt;console&lt;/code&gt; statement for now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Hello World'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Writing vanilla JavaScript in the TypeScript file is absolutely fine as mentioned on TypeScript's website: &lt;em&gt;All valid JavaScript code is also TypeScript code.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And now, let's run the above file by typing the following command in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run filepath.ts //outputs Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynilo09j6ntnvjuu08l7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynilo09j6ntnvjuu08l7.gif" alt="We are running a .ts file using Deno runtime."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Deno also allows you to run the remote files directly in the terminal like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run https://deno.land/std/examples/welcome.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz40h824f0znwzoaxjl8d.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz40h824f0znwzoaxjl8d.gif" alt="We are running a remote .ts file using Deno runtime."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above file is one of the examples available in Deno's Standard Library. You can find more examples at &lt;a href="https://deno.land/std/examples" rel="noopener noreferrer"&gt;https://deno.land/std/examples&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Our first Deno app
&lt;/h2&gt;

&lt;p&gt;Now that we have some fair idea on how to execute code using REPL or a .ts file, we will now move to a little more complex program where we will explore additional features of Deno.&lt;/p&gt;

&lt;p&gt;In the below app, we are merely fetching some &lt;a href="https://api.jsonbin.io/v3/b/5ec136d747a2266b1479ecc7" rel="noopener noreferrer"&gt;data from the third-party website&lt;/a&gt; and printing it in the terminal.&lt;/p&gt;

&lt;p&gt;We will create a new file for the program, say &lt;code&gt;Request.ts&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;Request.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the below code to Request.ts&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="c1"&gt;//fetch the data from JSONBin.io - JSON Storage website (public record)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getRecords&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://api.jsonbin.io/v3/b/5ec136d747a2266b1479ecc7&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//proceed once the promise is resolved from the previous statement, fetch the response body and parse it to json&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getData&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;getRecords&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="c1"&gt;//prints the retrieved data&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;getData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//[ 1,2,3,4,5 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we request the JSON data from JSONBin.io (a free JSON hosting website) and later we parse the response using &lt;code&gt;.json()&lt;/code&gt; and then we print the response using &lt;code&gt;console.log()&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;Now, we will run the above code using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run Request.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fair. When we run the file, we expect the file to output the contents we just retrieved, but instead, it errors out.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gm5ts02izj0mww1dm9b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gm5ts02izj0mww1dm9b.png" alt="Fetching data from a third-party website using the fetch API with Deno"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;😖 &lt;strong&gt;Why is it erroring out?&lt;/strong&gt; As we discussed earlier, unlike Node.js, Deno provides a secure runtime for JavaScript, and hence, since your script file is trying to access an external resource, you need to grant the permission to the script to do so. Try rerunning the following command, but this time, we will pass the necessary permission for the script to fetch the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run &lt;span class="nt"&gt;--allow-net&lt;/span&gt; Request.ts

&lt;span class="c"&gt;# script will now output the contents of the file like &lt;/span&gt;
&lt;span class="c"&gt;# [ 1, 2, 3, 4, 5 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ms1kbflmc1toq8g8v62.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ms1kbflmc1toq8g8v62.gif" alt="Run a .ts file to fetch the data from a third-party website"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are some key takeaways from the above app we just created.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We didn't install any third-party modules like &lt;code&gt;axios&lt;/code&gt;, &lt;code&gt;request&lt;/code&gt; or &lt;code&gt;node-fetch&lt;/code&gt; to request the data as Deno provides us with native fetch API support.&lt;/li&gt;
&lt;li&gt;No need to wrap the request with an &lt;strong&gt;async&lt;/strong&gt; function. We can use &lt;code&gt;await&lt;/code&gt; directly as it's available as a &lt;strong&gt;top-level&lt;/strong&gt; function.&lt;/li&gt;
&lt;li&gt;We cannot run the program unless the third-party website is granted explicit permission. Hence, we need to pass --allow-net flag explicitly.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;It's been a couple of days since I started exploring Deno, and I must say, it's too early to use it in production apps. In the near future, security will be a massive concern for the developers as we've already seen &lt;a href="https://www.google.com/search?q=mailcious+npm+package" rel="noopener noreferrer"&gt;malicious packages&lt;/a&gt; sneaking in on the NPM registry. There's also a lack of third-party support, modules that further makes the development difficult as you need to write a lot of modules by yourself instead of focusing on the core functionality of your app.&lt;/p&gt;

&lt;p&gt;Deno is not going to replace Node.js any time soon, but in the coming years, you might see Deno community growing, and you'll see a lot of third-party support like frameworks, modules that will extend the core functionality of the system.&lt;/p&gt;




&lt;h2&gt;
  
  
  Relevant Resources
&lt;/h2&gt;

&lt;p&gt;I'm sharing a few resources which will help you get started with Deno assuming you already know Node.js.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/denoland/deno" rel="noopener noreferrer"&gt;Deno Repository&lt;/a&gt;&lt;br&gt;
&lt;a href="https://doc.deno.land/" rel="noopener noreferrer"&gt;Deno Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://deno.land/std/examples/" rel="noopener noreferrer"&gt;Deno Examples&lt;/a&gt;&lt;br&gt;
&lt;a href="https://deno.land/manual" rel="noopener noreferrer"&gt;Deno Manual&lt;/a&gt;&lt;br&gt;
&lt;a href="https://deno.land/std" rel="noopener noreferrer"&gt;Deno Standard Library&lt;/a&gt;&lt;br&gt;
&lt;a href="https://deno.land/x" rel="noopener noreferrer"&gt;Deno - Third-Party Modules&lt;/a&gt; (more or less like NPM library)&lt;br&gt;
&lt;a href="https://v8.dev/features/top-level-await" rel="noopener noreferrer"&gt;Top Level fetch&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stevedonovan.github.io/rust-gentle-intro/readme.html" rel="noopener noreferrer"&gt;Rust Introduction&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/news/learn-typescript-in-5-minutes-13eda868daeb/" rel="noopener noreferrer"&gt;TypeScript for beginners&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>deno</category>
      <category>node</category>
    </item>
    <item>
      <title>How to Configure PM2 the right way</title>
      <dc:creator>Vaibhav Mehta</dc:creator>
      <pubDate>Tue, 08 Nov 2022 08:25:54 +0000</pubDate>
      <link>https://dev.to/mr_ali3n/how-to-configure-pm2-the-right-way-2m33</link>
      <guid>https://dev.to/mr_ali3n/how-to-configure-pm2-the-right-way-2m33</guid>
      <description>&lt;h3&gt;
  
  
  Prelude
&lt;/h3&gt;

&lt;p&gt;While developing apps on NodeJS, you must have used packages like Nodemon, which monitors your code for changes and restarts your Node App. Though Nodemon is super useful in development, it's not recommended to use it in Production. Nodemon is not something you use if you wish to keep your app alive on Production if it crashes.&lt;/p&gt;

&lt;p&gt;Here's where PM2 comes in handy. I've been hosting my NodeJS apps using PM2 on production as well I use PM2 for Development.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Quoting from PM2&lt;/u&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime, and to facilitate common system admin tasks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we go ahead and configure the PM2, yes, PM2 does support the auto-reload feature like Nodemon using the &lt;code&gt;watch&lt;/code&gt; feature. More on this later in the article.&lt;/p&gt;




&lt;h3&gt;
  
  
  Configuring PM2
&lt;/h3&gt;

&lt;p&gt;PM2 simplifies the way you could define the configuration for your app using PM2 ecosystem configuration file. You could use a PM2 configuration file for configuring multiple envs, logging, etc.&lt;/p&gt;

&lt;p&gt;Here's a snippet for configuring your PM2 envs and I'll brief each of these later in the article.&lt;/p&gt;

&lt;h4&gt;
  
  
  ecosystem.config.js
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;apps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PROJECT_NAME&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./server.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;instances&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;max_memory_restart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;300M&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

      &lt;span class="c1"&gt;// Logging&lt;/span&gt;
      &lt;span class="na"&gt;out_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./out.log&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;error_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./error.log&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;merge_logs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;log_date_format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;DD-MM HH:mm:ss Z&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;log_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

      &lt;span class="c1"&gt;// Env Specific Config&lt;/span&gt;
      &lt;span class="na"&gt;env_production&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;NODE_ENV&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;exec_mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cluster_mode&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="na"&gt;env_development&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;NODE_ENV&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;watch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;watch_delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;ignore_watch&lt;/span&gt;&lt;span class="p"&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;./node_modules&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;./app/views&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;./public&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;./.DS_Store&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;./package.json&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;./yarn.lock&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;./samples&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;./src&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can put the configuration file at the root level of your project.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Basic Configuration
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  name
&lt;/h4&gt;

&lt;p&gt;This could be your project name and PM2 will use this name to name your workers.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  script
&lt;/h4&gt;

&lt;p&gt;The entry point of your app, mostly it's the &lt;code&gt;server.js&lt;/code&gt;. Make sure you set the right path, the above configuration assumes that you've the &lt;code&gt;ecosystem.config.js&lt;/code&gt; and the &lt;code&gt;server.js&lt;/code&gt; file at the root level of your project.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  instances
&lt;/h4&gt;

&lt;p&gt;This is the number of workers you wish to spawn. &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  max_memory_restart
&lt;/h4&gt;

&lt;p&gt;PM2 allows you to reload your application based on the memory limit you specify using the &lt;code&gt;max_memory_restart&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Logging Configuration
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  out_file
&lt;/h4&gt;

&lt;p&gt;Generic logging, all your &lt;code&gt;console.log()&lt;/code&gt; statements are logging in this file along with other information.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  error_file
&lt;/h4&gt;

&lt;p&gt;Similar to &lt;code&gt;out_file&lt;/code&gt; but error files only log the errors of your application.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  merge_logs
&lt;/h4&gt;

&lt;p&gt;This is not needed if you are fine with PM2 storing a unique log file for each of its PID - Process ID). Using this config will merge all your app logs into a single file. &lt;u&gt;&lt;code&gt;merge_logs&lt;/code&gt; will merge your log files of various PIDs and it &lt;strong&gt;doesn't merge&lt;/strong&gt; the output and error logs into one.&lt;/u&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  log_date_format
&lt;/h4&gt;

&lt;p&gt;The above is self-explanatory. You could define a date/time format you wish to print your logs in.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  log_type
&lt;/h4&gt;

&lt;p&gt;Specify the output style, it's raw by default but we are using JSON here.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;strong&gt;Logs Path &amp;amp; Disabling Logs&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
  By default, your Logs are stored under &lt;code&gt;$HOME/.pm2/logs/&amp;lt;app name&amp;gt;-(out||error)-&amp;lt;pid&amp;gt;.log&lt;/code&gt;. You could disable logging by pointing the log path to &lt;code&gt;out_file: "/dev/null"&lt;/code&gt; and the same is applicable for &lt;code&gt;error_file&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Environment specific Configuration
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;h4&gt;env_production &amp;amp;&amp;amp; env_development&lt;/h4&gt;
These are nothing but different environments you wish to run your apps in, using PM2.

You could create config for multiple staging env if your setup requires to do so.
&lt;ul&gt;
&lt;li&gt;
&lt;h5&gt;&lt;u&gt;NODE_ENV&lt;/u&gt;&lt;/h5&gt;
The above set the env variable value to Production / Development for your app.
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;&lt;u&gt;PORT&lt;/u&gt;&lt;/h5&gt;
Port at which you would like to initiate your app at.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;exec_mode&lt;/h4&gt;
You could use cluster or fork, where the latter is the default mode. It is recommended to use the cluster mode on production and you could use a single instance locally. Number specified in the instances creates child processes (workers) and distributes the incoming connections across the worker processes.

You could set the value to 0 or max to specify how many workers you wish the PM2 should launch (max will spawn as many workers as the number of available cores).

&lt;b&gt;From &lt;a href="https://nodejs.org/api/cluster.html#cluster_how_it_works"&gt;PM2 Docs&lt;/a&gt;&lt;/b&gt;
&lt;blockquote&gt;
The worker processes are spawned using the child_process.fork() method, so that they can communicate with the parent via IPC and pass server handles back and forth.
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;watch&lt;/h4&gt;
We need to use this only on the dev envs. The use case is exactly what nodemon does. The watch monitors the file changes in your app and restarts your app as soon as a file change is detected.
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;watch_delay&lt;/h4&gt;
You don't want to end up restarting your app as soon as you save your file. So if you try to save your file multiple times in under three seconds, it will only restart your app once as it will clear the watch timer every time you save your files under three seconds.
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;ignore_watch&lt;/h4&gt;
There will your app will end up restarting even if you install an NPM package, add or move a file in your app dir, or as simple as add an img in your public dir.

Hence, any folder or file paths added to the ignore_watch array will not trigger a restart of your app as they are not relevant and don't require your app to be restarted in the first place.
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Useful PM2 commands
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Starting your App in Dev &amp;amp; Production mode
&lt;/h4&gt;

&lt;p&gt;You could add the following commands to &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start_dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"pm2 start ecosystem.config.js --env development"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start_prod"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"pm2 start ecosystem.config.js --env production"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could execute the above commands like &lt;code&gt;npm run start_dev&lt;/code&gt; and &lt;code&gt;npm run start_prod&lt;/code&gt; which starts your app in dev and production mode with the given config.&lt;/p&gt;




&lt;h4&gt;
  
  
  Start, Stop, Restart &amp;amp; Kill your App
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Kill your App
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pm2 &lt;span class="nb"&gt;kill&lt;/span&gt; &lt;span class="c"&gt;#kills all pm2 workers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Reloading your App
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pm2 reload all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Restarting your App
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pm2 restart all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you could pass the PID instead of using &lt;code&gt;all&lt;/code&gt; keyword which will end up running the same operation on all the processes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Restarting a process will kill and restart it as opposed to reloading which achieves a 0-second-downtime reload.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h4&gt;
  
  
  Logs
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Delete all Logs
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pm2 flush
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Read Logs
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pm2 logs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Processes
&lt;/h4&gt;

&lt;h5&gt;
  
  
  List Processes
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pm2 list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Monitor Processes
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pm2 monit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;You could also configure the deployment process using the PM2 but I've not covered it in the above tutorial as I've never used it personally.&lt;/p&gt;

&lt;p&gt;Here are some resources which will help you understand PM2 configuration in detail.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/pm2"&gt;PM2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pm2.keymetrics.io/docs/usage/application-declaration/"&gt;Configuration Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/34682035/cluster-and-fork-mode-difference-in-pm2"&gt;Cluster vs Fork in PM2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Folder Structure for NodeJS &amp; ExpressJS project</title>
      <dc:creator>Vaibhav Mehta</dc:creator>
      <pubDate>Mon, 31 Oct 2022 09:30:17 +0000</pubDate>
      <link>https://dev.to/mr_ali3n/folder-structure-for-nodejs-expressjs-project-435l</link>
      <guid>https://dev.to/mr_ali3n/folder-structure-for-nodejs-expressjs-project-435l</guid>
      <description>&lt;h2&gt;
  
  
  Prelude
&lt;/h2&gt;

&lt;p&gt;I've worked over several backend technologies, starting from PHP, then moving to RoR before getting my hands on NodeJS. I love how NodeJS simplifies backend development for Frontend developers; and not only this, the NPM ecosystem helps devs get up and running with complex projects with ease, without the need of re-inventing the wheel by developing core packages.&lt;/p&gt;

&lt;p&gt;Over these years, I've tried and tested various folder structures for my projects (confession: I've never referred to any folder structure on the web as I wanted to design something of my own which I find comfortable working with rather than being biased upfront - how ironic as I am drafting this tutorial 🙂) and I've finally reached a stage where I feel confident enough to share something which works for a majority of my NodeJS projects.&lt;/p&gt;

&lt;p&gt;As an example for this article, we will try to structure an App assuming there's a:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Website with a Landing Page&lt;/li&gt;
&lt;li&gt;Basic Authentication&lt;/li&gt;
&lt;li&gt;A Dashboard&lt;/li&gt;
&lt;li&gt;You are using Express like framework&lt;/li&gt;
&lt;li&gt;Build tools for Frontend like Gulp, Webpack.&lt;/li&gt;
&lt;li&gt;Applicable if you are using Frontend frameworks/libs like React, Angular, Vue.js, etc.&lt;/li&gt;
&lt;li&gt;Applicable if you wish to build API only App.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's a pretty standard scenario for any application out there.&lt;/p&gt;

&lt;p&gt;Last but not the least, the structure is more or less a Monorepo. You could take some good parts and apply them to your apps if your Node Apps are intended to serve as an API only or as a Website.&lt;/p&gt;




&lt;h2&gt;
  
  
  NodeJS Project Folder Structure
&lt;/h2&gt;

&lt;p&gt;The folder structure I usually follow is inspired massively by RoR as I was working with Ruby &amp;amp; RoR before switching to NodeJS.&lt;/p&gt;

&lt;p&gt;Here's how I structure my NodeJS App. I'll be explaining some of the reasoning behind the file structure and will also share some snippets on how do I expose some of the configs at a global level or how do I initiate a Database Connection across the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/app
├── config/
│   ├── db.conf.js
│   ├── app.conf.js
│   ├── app.keys.js
│   ├── db.keys.js
│   ├── init.js
├── database/
│   ├── Redis.database.js
│   ├── Mongo.database.js
│   ├── init.js
├── routes/
│   ├── App.routes.js
│   ├── Auth.routes.js
│   ├── Dashboard.routes.js
├── utils/
│   ├── Logger.util.js
├── middleware/
│   ├── App.middleware.js
│   ├── ErrorHandler.middleware.js
│   ├── init.js
├── models/
│   ├── User.model.js
├── controllers/
│   ├── App.controller.js
│   ├── User.controller.js
├── helpers/
│   ├── App.helper.js
├── views/
│   ├── layouts/
│   ├── partials/
│   ├── support/
│   │   ├── index.ejs
│   ├── documentation/
│   │   ├── index.ejs
│   ├── index.ejs
│   ├── about.ejs
│   ├── contact.ejs
/public
├── dist/
├── images/
│   ├── dashboard/
│   ├── auth/
│   ├── documentation/      
├── sitemap.xml
/samples
├── .env.sample
├── db.conf.sample
├── app.conf.sample
├── app.keys.sample
/src
├── javascript/
├── css/
/node_modules
/server.js
/package.json
/.env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Project Structure Brief
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Config &amp;amp; Sample
&lt;/h3&gt;

&lt;p&gt;Configuration could be categorised into three major categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;System Configuration&lt;/li&gt;
&lt;li&gt;App Configuration&lt;/li&gt;
&lt;li&gt;App Keys&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Store the configuration variables in &lt;code&gt;.env&lt;/code&gt; (&lt;a href="https://www.npmjs.com/package/dotenv"&gt;dotenv&lt;/a&gt;) which are necessary for configuring your application like App Port, Environment (Production, Staging, etc.). &lt;code&gt;.env&lt;/code&gt; configuration will be available across your app as they are set globally as ENV variables.&lt;/p&gt;

&lt;p&gt;You could then create multiple app-relevant configuration files like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Database Connection&lt;/strong&gt;: Database-specific configurations like Host, Port &amp;amp; Name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;App Configuration&lt;/strong&gt;: Acceptable Request Payload Size, Blacklisting IPs or Regions, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Auth Configuration&lt;/strong&gt;: OAuth Callback URLs, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And last but not the least, you could create multiple files and store relevant keys, like OAuth Client &amp;amp; Secret Keys, Database Keys, Mailer Keys etc.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Important: Do not commit any of these configuration files to Git. Copy the configuration structure with dummy values and commit to git using sample files under the &lt;code&gt;/sample&lt;/code&gt; folder.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Database
&lt;/h3&gt;

&lt;p&gt;This is where you could create connections to your database(s) like Redis, MySQL, MongoDB etc.&lt;/p&gt;

&lt;p&gt;You could then require the necessary configuration &amp;amp; keys to initiate a connection here. This will also make it easier for you manage everything related to your Database connection in one file, like adding listeners around successful connections, errors and disconnects, etc.&lt;/p&gt;

&lt;p&gt;Later, these connectors could be loaded under initialization files as needed.&lt;/p&gt;




&lt;h3&gt;
  
  
  Routes
&lt;/h3&gt;

&lt;p&gt;Usually, you could create a single Route file here to manage all your app related routes but it is recommended to create multiple route files which could be categorised like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Website Routes&lt;/li&gt;
&lt;li&gt;API Routes&lt;/li&gt;
&lt;li&gt;Authentication Routes&lt;/li&gt;
&lt;li&gt;Documentation Routes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;etc. Above is more scalable as well as manageable. Here's a code sample for the above:&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;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&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;const&lt;/span&gt; &lt;span class="nx"&gt;websiteRoutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@routes/Website.routes&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;apiRoutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@routes/Api.routes&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;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="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;websiteRoutes&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;apiRoutes&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 see, moving the routes to separate files also simplifies how your app would consume these routes.&lt;/p&gt;




&lt;h3&gt;
  
  
  Utils
&lt;/h3&gt;

&lt;p&gt;Your app may have several utility functions, like Logging important information, or something which are static and have no relevance to other classes/files, as opposed to Helpers (defined later in this post) which may help other classes or modules in your app.&lt;/p&gt;




&lt;h3&gt;
  
  
  Middleware
&lt;/h3&gt;

&lt;p&gt;Your app will have several middleware functions which could be separated and initiated in a single file. These helpers could range from critical middleware like Body Parser, Global Error Handlers, Authentication Middleware, enabling CORS, Attaching Custom Headers or setting a View Engine in ExpressJS.&lt;/p&gt;




&lt;h3&gt;
  
  
  Models
&lt;/h3&gt;

&lt;p&gt;Every Collection (if MongoDB) or a Table (if MySQL) will have a standalone model file. For eg: A collection of Users will have it's own &lt;code&gt;User.model.js&lt;/code&gt; file which could be extended further for defining a Schema Structure for the collection, Setting Default Values in the DB or Validating the User Inputs before storing the values to Database.&lt;/p&gt;




&lt;h3&gt;
  
  
  Controllers
&lt;/h3&gt;

&lt;p&gt;Controllers will be responsible to handle all the incoming requests to your application which will either render a page in response, may send a JSON payload or will handle other critical API related actions like POST, PUT, DELETE etc.&lt;/p&gt;

&lt;p&gt;Controllers will further consume Models, say a User model to Create Users while registering on your website or will render the view files if static pages are requested.&lt;/p&gt;




&lt;h3&gt;
  
  
  Helpers
&lt;/h3&gt;

&lt;p&gt;Unlike utility methods, helpers could be dynamic in nature and related to specific controllers when needed. Helpers may contain methods to parse some user posted payload, modify it before storing it to the Database, etc.&lt;/p&gt;




&lt;h3&gt;
  
  
  Views
&lt;/h3&gt;

&lt;p&gt;You may not need this folder if you are developing an API only app or using a separate SSR library like Next or Nuxt. This may be useful when you are using Express View Engine like Pug or EJS.&lt;/p&gt;

&lt;p&gt;You can further divide your views into Layouts, Pages &amp;amp; Partials. Where, Layouts could be shared between similar pages and a website could have multiple layouts. Lastly, partials will hold common components like Header, Footer, Sidebar etc which are needed across your web pages.&lt;/p&gt;




&lt;h3&gt;
  
  
  Public
&lt;/h3&gt;

&lt;p&gt;As the name suggests, anything under this folder will be publicly accessible by your website users. CSS, JavaScript and Images will be a part of this folder.&lt;/p&gt;

&lt;p&gt;Your app may use tools like Webpack and Gulp which will compile(minify, pre-process) your (S)CSS &amp;amp; JS files and move under the public folder which will later be linked on web pages.&lt;/p&gt;

&lt;p&gt;You could also build and output your React (or similar lib/framework) app under the &lt;code&gt;dist/&lt;/code&gt; folder.&lt;/p&gt;




&lt;h3&gt;
  
  
  Src
&lt;/h3&gt;

&lt;p&gt;As &lt;code&gt;App&lt;/code&gt; folder is responsible for handling all of the backend logic, &lt;code&gt;Src&lt;/code&gt; folder will hold your JavaScript &amp;amp; CSS files if you are using SASS, ES 6 / Next which needs to be transpiled, compiled, minified and uglified before these could be served to the end users.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Src&lt;/code&gt; folder will further hold directories like &lt;code&gt;CSS&lt;/code&gt; and &lt;code&gt;JavaScript&lt;/code&gt; which could be customized as per your needs.&lt;/p&gt;




&lt;p&gt;A simple explanation for &lt;code&gt;init.js&lt;/code&gt; files before I close this article, &lt;code&gt;init.js&lt;/code&gt; files will require rest of the files and export them to other files of your application, so that you need not have to require multiple files every time if you wish to consume them all.&lt;/p&gt;




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

&lt;p&gt;I follow the above folder structure more or less in all the projects I build on Node. It works well for me and I would love to hear from you if it helps you in some way.&lt;/p&gt;

&lt;p&gt;Eventually, you should try and modify the folder structure as per your needs and something which you find comfortable managing and scaling it in the long run.&lt;/p&gt;




&lt;h2&gt;
  
  
  Useful Resources
&lt;/h2&gt;

&lt;p&gt;Lastly, I would love to share some packages which I use to manage configuration, simplifying the process of requiring files at various nested levels, as well as some Linting packages to standardise your code to a great extent..&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/dotenv"&gt;DotENV&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/module-alias"&gt;module-alias&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/prettier"&gt;Prettier&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/eslint"&gt;ESLint&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>express</category>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>@use &amp; @forward in SASS</title>
      <dc:creator>Vaibhav Mehta</dc:creator>
      <pubDate>Wed, 12 May 2021 11:50:19 +0000</pubDate>
      <link>https://dev.to/mr_ali3n/use-forward-in-sass-2bab</link>
      <guid>https://dev.to/mr_ali3n/use-forward-in-sass-2bab</guid>
      <description>&lt;p&gt;I have been working with CSS over the years and love it. I have tried various libraries, toolsets like Bourbon (yes, I am that old), and pre-processors like SASS &amp;amp; Less.&lt;/p&gt;

&lt;p&gt;Talking about the CSS pre-processors, lately, I was reading about how &lt;code&gt;node-sass&lt;/code&gt; will be deprecated soon, and hence, I checked out the &lt;code&gt;dart-sass&lt;/code&gt; documentation.&lt;/p&gt;

&lt;p&gt;While reading the documentation, I came across an exciting way of importing the SASS files instead of the traditional way of importing the stylesheets using &lt;code&gt;@import&lt;/code&gt; statements.&lt;/p&gt;

&lt;p&gt;Yes, just like me, you must have thought the same, that what would be better than using &lt;code&gt;@import&lt;/code&gt; statements to import the Stylesheets? Well, &lt;code&gt;dart-sass&lt;/code&gt; has to offer two new ways of importing them, namely &lt;code&gt;@use&lt;/code&gt; and &lt;code&gt;@forward&lt;/code&gt;.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Important: Make sure that you have &lt;a href="https://www.npmjs.com/package/sass"&gt;&lt;code&gt;sass&lt;/code&gt;&lt;/a&gt; installed, and NOT &lt;a href="https://www.npmjs.com/package/node-sass"&gt;&lt;code&gt;node-sass&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we try out the new ways to handle the imports using SCSS, we will first define the Files &amp;amp; the Folder structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/project
|-- /vars
|---- _colors.scss
|---- _fonts.scss
|-- app.scss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Let's try out @use
&lt;/h2&gt;

&lt;p&gt;As you know, we can import the files in SCSS using the &lt;code&gt;@import&lt;/code&gt; statement. &lt;code&gt;@use&lt;/code&gt; pretty much does the same thing, EXCEPT, that it namespaces your variables, thus, preventing the name collisions.&lt;/p&gt;

&lt;p&gt;Starting with some simple code here, we will create a file to hold some variables, and then we will require this in our App file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// _colors.scss
$h1-color: #f00;
$component-color: #0f0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then, we will use &lt;code&gt;@import&lt;/code&gt; first, in &lt;code&gt;app.scss&lt;/code&gt; to test if everything's in place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.scss
@import 'vars/colors';

h1 {
  color: $h1-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above should work as expected. Now, without any further ado, we will go ahead and modify the &lt;code&gt;@import&lt;/code&gt; statement to &lt;code&gt;@use&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;// app.scss
@use 'vars/colors';

h1 {
  color: $h1-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;@use helps you namespace your SCSS files&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we are using &lt;code&gt;@use&lt;/code&gt; it to import the stylesheet will result in an error. Reason? Remember, in the beginning; I mentioned that &lt;code&gt;@use&lt;/code&gt; helps us namespace our variable names, thus preventing collisions if any. Hence, we need to modify the above code like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.scss
@use 'vars/colors';

h1 {
  color: colors.$h1-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Viola, the SCSS is now compiled. All we did was, prefixed the &lt;code&gt;$h1-color&lt;/code&gt; variable using colors that are nothing but the file name.&lt;/p&gt;




&lt;h2&gt;
  
  
  @use(ing) Alias
&lt;/h2&gt;

&lt;p&gt;Now, what if we have a file name of &lt;code&gt;_colors_for_theme_blue.scss&lt;/code&gt; (not pretty at all but assuming some worst-case scenario), and then writing it like &lt;code&gt;colors_for_theme_blue.$h1-color&lt;/code&gt; will be super annoying.&lt;/p&gt;

&lt;p&gt;To cut short the filename, you can use the keyword as that will help you create an alias for the &lt;code&gt;_colors.scss&lt;/code&gt; file. E.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.scss
@use 'vars/colors' as c;

h1 {
  color: c.$h1-color; //note that now we are using only c here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;as&lt;/code&gt; helps you define an alias for the file name you are importing using the &lt;code&gt;@use&lt;/code&gt; keyword.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Alternatively, you could also use an &lt;code&gt;*&lt;/code&gt; as an alias which will result in behavior similar to the one which &lt;code&gt;@import&lt;/code&gt; provides. For, e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.scss
@use 'vars/colors' as *;

h1 {
  color: $h1-color; // note, you need not have to specify
                       colors prefix before you use $h1-
                       color variable.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I would not recommend you doing the above since it misses the point in having name-spaced variables.&lt;/p&gt;




&lt;h2&gt;
  
  
  Scope using @use vs. @import
&lt;/h2&gt;

&lt;p&gt;There is another benefit of using &lt;code&gt;@use&lt;/code&gt; instead of the &lt;code&gt;@import&lt;/code&gt; is the scope. When we use &lt;code&gt;@import&lt;/code&gt; statement to import certain files, then this file has access to the declared variables before importing the component file. For, e.g.: (we will introduce a new folder here, say components, and create a sample component file like &lt;code&gt;_my_component.scss&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;/project
|-- /components
|---- _my_component.scss
|-- /vars
|---- _colors.scss
|---- _fonts.scss
|-- app.scss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we will try to import &lt;strong&gt;&lt;code&gt;_my_component.scss&lt;/code&gt;&lt;/strong&gt; using the &lt;code&gt;@import&lt;/code&gt; statement and then using the &lt;code&gt;@use&lt;/code&gt; and see the difference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.scss
// using @import
@import 'vars/colors';
@import 'components/_my_component';

h1 {
  color: $h1-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here we have some code in the &lt;code&gt;_my_component.scss&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// _my_component.scss
.my-component {
  color: $component-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the code will be compiled as expected, where even the &lt;code&gt;_my_component.scss&lt;/code&gt; has access to the variables declared in the &lt;code&gt;app.scss&lt;/code&gt; file, create issues as the codebase grows.&lt;/p&gt;

&lt;p&gt;The above results in the following compiled CSS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// compiled output
.my-component {
  color: #0f0;
}

h1 {
  color: #aaa;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you notice, the &lt;code&gt;.my-component&lt;/code&gt; can access the colors defined in a separate file imported before importing the component file.&lt;/p&gt;

&lt;p&gt;If we make use of &lt;code&gt;@use&lt;/code&gt; instead of the &lt;code&gt;@import&lt;/code&gt;, here's what will happen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.scss
// using @use
@use 'vars/colors' as c;
@use 'components/_my_component';

h1 {
  color: c.$h1-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tweaking the component file a bit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// _my_component.scss
.my-component {
  color: c.$component-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above will result in an error:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Error: There is no module with the namespace "c".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Say if we remove the namespace, which is c, then the error which we will get is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Error: Undefined variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hence, using &lt;code&gt;@use&lt;/code&gt; helps you control the scope of your variables instead of &lt;code&gt;@import&lt;/code&gt; which makes the variables globally accessible to the files which are imported post the variable file. Still, if you wish to use the same variable file in the &lt;code&gt;_my_components.scss&lt;/code&gt; then you need to require the variables in the components file as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// _my_component.scss
@use '../vars/colors' as c; //note the filepath here,
                              have added ../

.my-component {
  color: c.$component-color;
}

.my-component {
  color: c.$component-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Using @forward
&lt;/h2&gt;

&lt;p&gt;You must be having multiple helper files, say for variables, mixins, fonts, extends, etc. Having to require them in every single file (since using &lt;code&gt;@use&lt;/code&gt;, it will not make these files globally available) required in components and other SASS files will be tedious.&lt;/p&gt;

&lt;p&gt;Hence, we could use &lt;code&gt;@forward&lt;/code&gt; which acts like a pipe. It takes many files and makes the contents of those files available where this file is required. Let us take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/project
|-- /components
|---- _my_component.scss
|-- /vars
|---- _colors.scss
|---- _fonts.scss
|---- helpers.scss // we will add this file here, which 
                      will contain forwards of 
                      _colors.scss &amp;amp; _fonts.scss
|-- app.scss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we will add the &lt;code&gt;@forward&lt;/code&gt; to the &lt;code&gt;helpers.scss&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// helpers.scss
@forward 'colors';
@forward 'fonts';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now instead of requiring every helper file in &lt;code&gt;app.scss&lt;/code&gt; you would instead import the &lt;code&gt;helper.scss&lt;/code&gt; which will make the other files like &lt;code&gt;_colors.scss&lt;/code&gt; &amp;amp; &lt;code&gt;_fonts.scss&lt;/code&gt; available.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.scss
@use "vars/helpers";
@use "components/my_component";

h1 {
  color: helpers.$h1-color; // note that you need to modify
                               the namespace, or use * and 
                               you could simply use $h1- 
                               color
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and subsequently, we can now modify the &lt;code&gt;_my_component.scss&lt;/code&gt; file as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// _my_component.scss
@use "../vars/helpers";

.my-component {
  color: helpers.$component-color;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;@forward&lt;/code&gt; will make it simple for you to import multiple files without specifying them separately and instead use the common file.&lt;/p&gt;




&lt;p&gt;The above is a basic intro for using &lt;code&gt;@use&lt;/code&gt; and &lt;code&gt;@forward&lt;/code&gt;. There is much more to it, like Private Members, Index Files, Visibility Control, etc. I'll be attaching a few references where you could refer to these features in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Documentation for &lt;a href="https://sass-lang.com/documentation/at-rules/use"&gt;@use&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Documentation for &lt;a href="https://sass-lang.com/documentation/at-rules/forward"&gt;@forward&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://sass-lang.com/dart-sass"&gt;Dart SASS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/sass"&gt;SASS NPM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>sass</category>
      <category>scss</category>
    </item>
  </channel>
</rss>
