<?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: Bartick Maiti</title>
    <description>The latest articles on DEV Community by Bartick Maiti (@bartick).</description>
    <link>https://dev.to/bartick</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%2F727994%2F36bcde98-c2e5-4adb-ac5f-fa52ea2ff8b0.png</url>
      <title>DEV Community: Bartick Maiti</title>
      <link>https://dev.to/bartick</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bartick"/>
    <language>en</language>
    <item>
      <title>Unnoticed Memory Leaks in Your JS App: A Guide for Future</title>
      <dc:creator>Bartick Maiti</dc:creator>
      <pubDate>Tue, 09 Jan 2024 16:21:25 +0000</pubDate>
      <link>https://dev.to/bartick/unnoticed-memory-leaks-in-your-js-app-a-guide-for-future-k1i</link>
      <guid>https://dev.to/bartick/unnoticed-memory-leaks-in-your-js-app-a-guide-for-future-k1i</guid>
      <description>&lt;p&gt;As I embarked on my coding journey, the intricacies of memory management always intrigued me. Despite reading numerous articles and heeding warnings from my university professors about the perils of memory leaks, I never had the chance to witness a tangible example. In this article, I aim to demystify memory leaks by presenting a straightforward example using JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decoding the Memory Leak
&lt;/h2&gt;

&lt;p&gt;A memory leak is a form of resource mismanagement wherein a computer program fails to appropriately handle memory allocations, leading to the retention of memory that is no longer required. In the realm of object-oriented programming, a memory leak occurs when an object persists in memory but is inaccessible to the executing code. In other words, the memory is not released, even though it is no longer required.&lt;/p&gt;

&lt;h2&gt;
  
  
  Crafting a Memory Leak: A Hands-On Demonstration
&lt;/h2&gt;

&lt;p&gt;To illustrate a memory leak, we'll create a function that generates a closure. A closure, in this context, refers to a function that retains access to its parent scope even after the scope has concluded. The parent scope here is the &lt;code&gt;createLeak&lt;/code&gt; function, and the child scope is the &lt;code&gt;leak&lt;/code&gt; function.&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;createLeak&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;leaks&lt;/span&gt; &lt;span class="o"&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;leak&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;leaks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;leak&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;leak&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;Subsequently, we'll invoke the &lt;code&gt;createLeak&lt;/code&gt; function and store the result in a variable, effectively creating the closure.&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;leak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createLeak&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To induce a memory leak, we'll execute the &lt;code&gt;leak&lt;/code&gt; function within a loop. This action ensures that the &lt;code&gt;leaks&lt;/code&gt; array will never undergo garbage collection.&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;for &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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;leak&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;
  
  
  Rectifying a Memory Leak: A Pragmatic Approach
&lt;/h2&gt;

&lt;p&gt;Addressing a memory leak involves eliminating the reference to the closure. In this case, we must nullify the reference to the &lt;code&gt;leak&lt;/code&gt; function.&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;leak&lt;/span&gt; &lt;span class="o"&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;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;This article has provided insights into both the creation and detection of memory leaks, along with practical solutions for rectification. I trust that this exploration has not only been informative but has also enhanced your comprehension of memory leaks.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Empower Your Project: Hosting Your Own Redis Server</title>
      <dc:creator>Bartick Maiti</dc:creator>
      <pubDate>Thu, 23 Nov 2023 19:00:06 +0000</pubDate>
      <link>https://dev.to/bartick/empower-your-project-hosting-your-own-redis-server-253p</link>
      <guid>https://dev.to/bartick/empower-your-project-hosting-your-own-redis-server-253p</guid>
      <description>&lt;p&gt;As a student, I often find myself in search of free database options, and my go-to choices have always been those that offer a free tier. Many projects, in addition to a primary database, require Redis for streaming fast data. While cloud providers like &lt;a href="https://redis.com/try-free/"&gt;Redis&lt;/a&gt; offer a free tier suitable for small projects or testing purposes, I've always desired more storage and power. This led me to the decision to host my own Redis server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hosting Your Own Redis Server
&lt;/h2&gt;

&lt;p&gt;Assuming you already have a VPS account, if not, you can consider providers such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/"&gt;Digital Ocean&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linode.com/"&gt;Linode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.vultr.com/"&gt;Vultr&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/"&gt;AWS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cloud.google.com/"&gt;Google Cloud&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://azure.microsoft.com/en-us/"&gt;Azure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.oracle.com/cloud/"&gt;Oracle Cloud&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Personally, I use &lt;a href="https://www.oracle.com/cloud/"&gt;Oracle Cloud&lt;/a&gt; with a VPS boasting 2GB RAM and 1 vCPU, running Ubuntu 20.04 LTS. You can choose any VPS provider and OS that suits your preferences.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Redis
&lt;/h3&gt;

&lt;p&gt;To begin, we need to install Redis on our VPS. Execute the following commands:&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;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;redis-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring Redis
&lt;/h3&gt;

&lt;p&gt;Next, we must configure Redis by editing the Redis config file. Run:&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;sudo &lt;/span&gt;nano /etc/redis/redis.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the file, locate the &lt;code&gt;supervised&lt;/code&gt; directive, and change it to &lt;code&gt;systemd&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;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="c"&gt;# If you run Redis from upstart or systemd, Redis can interact with your&lt;/span&gt;
&lt;span class="c"&gt;# supervision tree. Options:&lt;/span&gt;
&lt;span class="c"&gt;#   supervised no      - no supervision interaction&lt;/span&gt;
&lt;span class="c"&gt;#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode&lt;/span&gt;
&lt;span class="c"&gt;#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET&lt;/span&gt;
&lt;span class="c"&gt;#   supervised auto    - detect upstart or systemd method based on&lt;/span&gt;
&lt;span class="c"&gt;#                        UPSTART_JOB or NOTIFY_SOCKET environment variables&lt;/span&gt;
&lt;span class="c"&gt;# Note: these supervision methods only signal "process is ready."&lt;/span&gt;
&lt;span class="c"&gt;#       They do not enable continuous liveness pings back to your supervisor.&lt;/span&gt;
supervised systemd
&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save and close the file, then restart the Redis service:&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;sudo &lt;/span&gt;systemctl restart redis.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Allowing Remote Connections
&lt;/h3&gt;

&lt;p&gt;By default, Redis is only accessible from the localhost. To permit remote connections, bind Redis to your server’s public IP address by editing the Redis configuration file again:&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;sudo &lt;/span&gt;nano /etc/redis/redis.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comment out the &lt;code&gt;bind&lt;/code&gt; directive:&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;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="c"&gt;# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the&lt;/span&gt;
&lt;span class="c"&gt;# internet, binding to all the interfaces is dangerous and will expose the&lt;/span&gt;
&lt;span class="c"&gt;# instance to everybody on the internet. So by default we uncomment the&lt;/span&gt;
&lt;span class="c"&gt;# following bind directive, that will force Redis to listen only on the&lt;/span&gt;
&lt;span class="c"&gt;# IPv4 loopback interface address (this means Redis will only be able to&lt;/span&gt;
&lt;span class="c"&gt;# accept client connections from the same host that it is running on).&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES&lt;/span&gt;
&lt;span class="c"&gt;# JUST COMMENT OUT THE FOLLOWING LINE.&lt;/span&gt;
&lt;span class="c"&gt;# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/span&gt;
&lt;span class="c"&gt;# bind 127.0.0.1 ::1&lt;/span&gt;
&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally, change the &lt;code&gt;protected-mode&lt;/code&gt; directive to &lt;code&gt;no&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;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="c"&gt;# The server only accepts connections from clients connecting from the&lt;/span&gt;
&lt;span class="c"&gt;# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain&lt;/span&gt;
&lt;span class="c"&gt;# sockets.&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# By default protected mode is enabled. You should disable it only if&lt;/span&gt;
&lt;span class="c"&gt;# you are sure you want clients from other hosts to connect to Redis&lt;/span&gt;
&lt;span class="c"&gt;# even if no authentication is configured, nor a specific set of interfaces&lt;/span&gt;
&lt;span class="c"&gt;# are explicitly listed using the "bind" directive.&lt;/span&gt;
protected-mode no
&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart the Redis server:&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;sudo &lt;/span&gt;systemctl restart redis.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adjusting the Firewall
&lt;/h3&gt;

&lt;p&gt;Now, configure the firewall to allow traffic on port 6379, the default Redis port:&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;sudo &lt;/span&gt;firewall-cmd &lt;span class="nt"&gt;--zone&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;public &lt;span class="nt"&gt;--permanent&lt;/span&gt; &lt;span class="nt"&gt;--add-port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;6379/tcp
&lt;span class="nb"&gt;sudo &lt;/span&gt;firewall-cmd &lt;span class="nt"&gt;--reload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing Redis
&lt;/h3&gt;

&lt;p&gt;With Redis configured, test it by connecting to your application. For instance, using &lt;a href="https://github.com/bartick/mini-redis"&gt;mini-redis&lt;/a&gt;, a Rust-based Redis client. The URL for the Redis server is &lt;code&gt;redis://&amp;lt;ip&amp;gt;:6379&lt;/code&gt;, where &lt;code&gt;&amp;lt;ip&amp;gt;&lt;/code&gt; is your VPS's IP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wow, it works but what about the security?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I1VvXZKT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media.tenor.com/3VgYHvcS18MAAAAC/minion-any-questions-question.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I1VvXZKT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media.tenor.com/3VgYHvcS18MAAAAC/minion-any-questions-question.gif" alt="Question" width="440" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although the Redis server is up, it lacks security. Anyone can connect and use it. To enhance security, follow these steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  Securing Redis
&lt;/h3&gt;

&lt;p&gt;Connect to the Redis server using the CLI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;redis-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change &lt;code&gt;protected-mode&lt;/code&gt; from &lt;code&gt;no&lt;/code&gt; to &lt;code&gt;yes&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;127.0.0.1:6379&amp;gt; CONFIG SET protected-mode &lt;span class="nb"&gt;yes
&lt;/span&gt;127.0.0.1:6379&amp;gt; CONFIG REWRITE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we try to connect to the Redis server from our application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo run
  Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.10s
    Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/tiny_redis&lt;span class="sb"&gt;`&lt;/span&gt;
thread &lt;span class="s1"&gt;'main'&lt;/span&gt; panicked at src/main.rs:11:9:
Failed to &lt;span class="nb"&gt;set &lt;/span&gt;key: DENIED: Redis is running &lt;span class="k"&gt;in &lt;/span&gt;protected mode because protected mode is enabled and no password is &lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="k"&gt;for &lt;/span&gt;the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1&lt;span class="o"&gt;)&lt;/span&gt; Just disable protected mode sending the &lt;span class="nb"&gt;command&lt;/span&gt; &lt;span class="s1"&gt;'CONFIG SET protected-mode no'&lt;/span&gt; from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet &lt;span class="k"&gt;if &lt;/span&gt;you &lt;span class="k"&gt;do &lt;/span&gt;so. Use CONFIG REWRITE to make this change permanent. 2&lt;span class="o"&gt;)&lt;/span&gt; Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to &lt;span class="s1"&gt;'no'&lt;/span&gt;, and &lt;span class="k"&gt;then &lt;/span&gt;restarting the server. 3&lt;span class="o"&gt;)&lt;/span&gt; If you started the server manually just &lt;span class="k"&gt;for &lt;/span&gt;testing, restart it with the &lt;span class="s1"&gt;'--protected-mode no'&lt;/span&gt; option. 4&lt;span class="o"&gt;)&lt;/span&gt; Set up an authentication password &lt;span class="k"&gt;for &lt;/span&gt;the default user. NOTE: You only need to &lt;span class="k"&gt;do &lt;/span&gt;one of the above things &lt;span class="k"&gt;in &lt;/span&gt;order &lt;span class="k"&gt;for &lt;/span&gt;the server to start accepting connections from the outside.
note: run with &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nv"&gt;RUST_BACKTRACE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1&lt;span class="sb"&gt;`&lt;/span&gt; environment variable to display a backtrace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, it is throwing an error. Let's add a user and password to the Redis server.&lt;/p&gt;

&lt;p&gt;To add a user and password, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;127.0.0.1:6379&amp;gt; ACL SETUSER blog on &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;mypassword +@all -@dangerous ~&lt;span class="k"&gt;*&lt;/span&gt;
127.0.0.1:6379&amp;gt; CONFIG REWRITE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a user &lt;code&gt;blog&lt;/code&gt; with the password &lt;code&gt;mypassword&lt;/code&gt; and permissions, excluding dangerous commands. Update your URL to &lt;code&gt;redis://blog:mypassword@&amp;lt;ip&amp;gt;:6379&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To know more about the permissions of Redis give it a read &lt;a href="https://docs.redis.com/latest/rc/security/access-control/data-access-control/configure-acls/#predefined-permissions"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, attempt to connect to the Redis server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo run
  Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.10s
    Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/tiny_redis&lt;span class="sb"&gt;`&lt;/span&gt;
thread &lt;span class="s1"&gt;'main'&lt;/span&gt; panicked at src/main.rs:11:9:
Failed to &lt;span class="nb"&gt;set &lt;/span&gt;key: DENIED: Redis is running &lt;span class="k"&gt;in &lt;/span&gt;protected mode because protected mode is enabled and no password is &lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="k"&gt;for &lt;/span&gt;the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1&lt;span class="o"&gt;)&lt;/span&gt; Just disable protected mode sending the &lt;span class="nb"&gt;command&lt;/span&gt; &lt;span class="s1"&gt;'CONFIG SET protected-mode no'&lt;/span&gt; from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet &lt;span class="k"&gt;if &lt;/span&gt;you &lt;span class="k"&gt;do &lt;/span&gt;so. Use CONFIG REWRITE to make this change permanent. 2&lt;span class="o"&gt;)&lt;/span&gt; Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to &lt;span class="s1"&gt;'no'&lt;/span&gt;, and &lt;span class="k"&gt;then &lt;/span&gt;restarting the server. 3&lt;span class="o"&gt;)&lt;/span&gt; If you started the server manually just &lt;span class="k"&gt;for &lt;/span&gt;testing, restart it with the &lt;span class="s1"&gt;'--protected-mode no'&lt;/span&gt; option. 4&lt;span class="o"&gt;)&lt;/span&gt; Set up an authentication password &lt;span class="k"&gt;for &lt;/span&gt;the default user. NOTE: You only need to &lt;span class="k"&gt;do &lt;/span&gt;one of the above things &lt;span class="k"&gt;in &lt;/span&gt;order &lt;span class="k"&gt;for &lt;/span&gt;the server to start accepting connections from the outside.
note: run with &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nv"&gt;RUST_BACKTRACE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1&lt;span class="sb"&gt;`&lt;/span&gt; environment variable to display a backtrace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well it is still throwing an error. And it is because we still have the default user unprotected. So we need to protect the default user.&lt;/p&gt;

&lt;p&gt;Let's protect the default user by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;127.0.0.1:6379&amp;gt; ACL SETUSER default on &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;redis +@all ~&lt;span class="k"&gt;*&lt;/span&gt;
127.0.0.1:6379&amp;gt; CONFIG REWRITE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, attempt to connect to the Redis server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo run
    Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;0.03s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/tiny_redis&lt;span class="sb"&gt;`&lt;/span&gt;
Value: 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay, it works. Now we have a secure Redis server running on our VPS. Now we can use it in our application. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jTytLj-3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media.tenor.com/8zHzYq3eBVwAAAAd/baby-scream-yeah.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jTytLj-3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media.tenor.com/8zHzYq3eBVwAAAAd/baby-scream-yeah.gif" alt="Yeah" width="640" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>redis</category>
      <category>database</category>
      <category>devops</category>
      <category>cloudstorage</category>
    </item>
    <item>
      <title>Understanding JavaScript Concepts: CommonJS vs. ECMAScript Modules</title>
      <dc:creator>Bartick Maiti</dc:creator>
      <pubDate>Fri, 10 Nov 2023 08:57:40 +0000</pubDate>
      <link>https://dev.to/bartick/understanding-javascript-concepts-commonjs-vs-ecmascript-modules-2doe</link>
      <guid>https://dev.to/bartick/understanding-javascript-concepts-commonjs-vs-ecmascript-modules-2doe</guid>
      <description>&lt;p&gt;Embarking on the journey of JavaScript development often confronts us with the dichotomy of &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;import&lt;/code&gt; statements, heralding the realms of CommonJS (cjs) and ECMAScript Modules (mjs) respectively. In this exploration, we aim to unravel the enigma surrounding these module systems and delve into the why and how of their existence.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Prelude to Modules
&lt;/h2&gt;

&lt;p&gt;Before delving into the intricacies, let's grasp the essence of modules. In the JavaScript context, modules are the building blocks that allow developers to compartmentalize code into manageable, reusable units. This not only enhances code organization but also facilitates seamless sharing of functionality across files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unveiling CommonJS
&lt;/h2&gt;

&lt;p&gt;CommonJS, initially devised for Node.js, provides a synchronous module format. It facilitates the division of code into multiple files and the sharing of code between them. The &lt;code&gt;require&lt;/code&gt; statement takes center stage in CommonJS, synchronously loading modules upon invocation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing ECMAScript Modules
&lt;/h2&gt;

&lt;p&gt;On the other hand, ECMAScript Modules (mjs), designed for browsers, introduces an asynchronous module format. This means that code within the module doesn't execute until explicitly imported. The &lt;code&gt;import&lt;/code&gt; statement becomes the gatekeeper for bringing in modules within the ECMAScript Modules paradigm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bridging the Gap: CommonJS vs. ECMAScript Modules
&lt;/h2&gt;

&lt;p&gt;The crux of the distinction lies in the loading mechanism: CommonJS modules load synchronously, while ECMAScript Modules load asynchronously. The former executes code immediately upon loading, while the latter waits for the import trigger.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the Right Path: CommonJS or ECMAScript Modules?
&lt;/h2&gt;

&lt;p&gt;The choice is dictated by the application environment. CommonJS finds its home in Node.js applications, while ECMAScript Modules cater to browser-based applications. Yet, the evolving landscape has blurred these boundaries.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Node.js Dilemma
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_2r7fudc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://blog.bartick.me/images/posts/85json.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_2r7fudc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://blog.bartick.me/images/posts/85json.jpg" alt="Dilema" width="466" height="710"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recent Node.js versions have embraced ECMAScript Modules, causing compatibility headaches for developers reliant on libraries designed for CommonJS. The transition to ECMAScript Modules is not seamless, as older libraries might not offer support, creating a conundrum for developers.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Unresolved Predicament
&lt;/h4&gt;

&lt;p&gt;The persistence of this predicament arises from the historical lack of ECMAScript Module support in Node.js. Libraries built on CommonJS were essential for legacy versions, and the inertia to migrate to ECMAScript Modules persists, hindering resolution.&lt;/p&gt;

&lt;h4&gt;
  
  
  Stranded Between Worlds
&lt;/h4&gt;

&lt;p&gt;In this limbo, developers grapple with libraries exclusively supporting one module system. The result? A forced cohabitation of CommonJS and ECMAScript Modules in the same application, each with its peculiarities in importing and exporting modules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Navigating the Dual Landscape
&lt;/h2&gt;

&lt;p&gt;The dilemma prompts the question: How can one seamlessly utilize both CommonJS and ECMAScript Modules in a single application? Several strategies emerge:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Transpile with Babel:
&lt;/h3&gt;

&lt;p&gt;Leverage Babel to convert between the two syntaxes. A configuration file (&lt;code&gt;.babelrc&lt;/code&gt;) and a build script can facilitate this transformation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install Babel and necessary presets:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; @babel/core @babel/cli @babel/preset-env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create a Babel configuration file (&lt;code&gt;.babelrc&lt;/code&gt;):&lt;/strong&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="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"presets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"@babel/preset-env"&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;"targets"&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;"node"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"current"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add a build script to your &lt;code&gt;package.json&lt;/code&gt;:&lt;/strong&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="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&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;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"babel src --out-dir dist"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Bundle with Webpack:
&lt;/h3&gt;

&lt;p&gt;Webpack emerges as a bundling solution, amalgamating CommonJS and ECMAScript Modules into a cohesive bundle. Configuration involves specifying entry points and output filenames.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install Webpack and necessary loaders:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; webpack webpack-cli babel-loader
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create a Webpack configuration file (&lt;code&gt;webpack.config.js&lt;/code&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="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;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/index.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;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bundle.js&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;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;rules&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;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;js$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/node_modules/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&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;babel-loader&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;p&gt;&lt;strong&gt;Add a build script to your &lt;code&gt;package.json&lt;/code&gt;:&lt;/strong&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="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&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;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Loader Packages like &lt;code&gt;esm&lt;/code&gt;:
&lt;/h3&gt;

&lt;p&gt;Specialized packages like &lt;code&gt;esm&lt;/code&gt; allow the interoperability of CommonJS and ECMAScript Modules. This involves importing modules dynamically with specific syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install the &lt;code&gt;esm&lt;/code&gt; package:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; esm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;esm&lt;/code&gt; to load CommonJS modules in ECMAScript Modules:&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;import&lt;/span&gt; &lt;span class="nx"&gt;esm&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;esm&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;myModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;esm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./my-module.js&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;Use &lt;code&gt;esm&lt;/code&gt; to load ECMAScript Modules in CommonJS:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myModule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;esm&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;./my-module.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;default&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;Run your CommonJS modules using the &lt;code&gt;esm&lt;/code&gt; package:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;-r&lt;/span&gt; esm index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Split Your Codebase:
&lt;/h3&gt;

&lt;p&gt;Consider segregating your project into CommonJS and ECMAScript Modules segments, communicating between them through inter-process communication or other mechanisms.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Native Adoption of ESM:
&lt;/h3&gt;

&lt;p&gt;For projects within your control, consider embracing ECMAScript Modules entirely, either by using the &lt;code&gt;.mjs&lt;/code&gt; extension or setting &lt;code&gt;"type": "module"&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt;. Update your CommonJS modules by using the &lt;code&gt;.cjs&lt;/code&gt; extension.&lt;/p&gt;

&lt;p&gt;These strategies provide a roadmap for navigating the intricate landscape of dual JavaScript module systems, ensuring compatibility and coexistence within your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Resilient JavaScript Journey
&lt;/h2&gt;

&lt;p&gt;As the JavaScript landscape continues to evolve, your mastery of these strategies ensures resilience in the face of change. By embracing the tools and approaches outlined here, you empower yourself to navigate the dual landscape of CommonJS and ECMAScript Modules with confidence.&lt;/p&gt;

&lt;p&gt;In conclusion, may your JavaScript journey be marked by adaptability, innovation, and a mastery of the diverse tools at your disposal. As you embark on projects, let the strategies explored here serve as a compass, guiding you through the intricate terrain of JavaScript modules, ensuring a robust and future-proof development experience.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>node</category>
    </item>
    <item>
      <title>Understanding JavaScript Concepts: prototype vs. __proto__</title>
      <dc:creator>Bartick Maiti</dc:creator>
      <pubDate>Sun, 05 Nov 2023 11:39:07 +0000</pubDate>
      <link>https://dev.to/bartick/understanding-javascript-prototypes-prototype-vs-proto-12p8</link>
      <guid>https://dev.to/bartick/understanding-javascript-prototypes-prototype-vs-proto-12p8</guid>
      <description>&lt;p&gt;When embarking on your JavaScript journey, you may have encountered the terms &lt;code&gt;prototype&lt;/code&gt; and &lt;code&gt;__proto__&lt;/code&gt;. These terms may appear somewhat interchangeable at first glance, leading to some confusion for newcomers. However, they serve distinct purposes in JavaScript. In this article, we will delve into the differences between these two concepts and explore how they interrelate.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Prototype?
&lt;/h2&gt;

&lt;p&gt;In JavaScript, a &lt;strong&gt;prototype&lt;/strong&gt; is an object that is inherently associated with every function and object by default. It is essentially a property attached to a function or object that enables you to augment new properties and methods to that function or object. Furthermore, it plays a pivotal role in implementing inheritance in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Closer Look at Prototypes
&lt;/h3&gt;

&lt;p&gt;In a JavaScript context, prototypes serve as blueprints or templates. They define the shared characteristics and behaviors that can be inherited by multiple objects or instances. By attaching properties and methods to a prototype, you create a sort of common knowledge base that can be efficiently shared among objects of the same type.&lt;/p&gt;

&lt;p&gt;Consider the following 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="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Adding a method to the Animal prototype&lt;/span&gt;
&lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speak&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; makes a sound.`&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;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Whiskers&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fido&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Whiskers makes a sound."&lt;/span&gt;
&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Fido makes a sound."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've defined an &lt;code&gt;Animal&lt;/code&gt; constructor function and added a &lt;code&gt;speak&lt;/code&gt; method to its prototype. As a result, any instance created from the &lt;code&gt;Animal&lt;/code&gt; constructor can access and utilize this &lt;code&gt;speak&lt;/code&gt; method. It illustrates how prototypes facilitate code reusability and object-oriented features in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;__proto__&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;__proto__&lt;/code&gt;, on the other hand, is a property specifically used to access the prototype of an object. It is attached to every object in JavaScript and serves as a getter and setter for the object's prototype. It provides a means to modify the prototype of an existing object, allowing you to change its characteristics dynamically.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Closer Look at &lt;code&gt;__proto__&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;__proto__&lt;/code&gt; property enables you to traverse the prototype chain, moving from an object to its prototype and, if necessary, to its prototype's prototype, and so on. This mechanism is instrumental in achieving inheritance and sharing properties and methods among objects.&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="nx"&gt;Vehicle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;make&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;make&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;make&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;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vehicle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Toyota&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Modifying the prototype dynamically using __proto__&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customFeatures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2023&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;customFeatures&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "blue"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 2023&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've created a &lt;code&gt;Vehicle&lt;/code&gt; object and then used &lt;code&gt;__proto__&lt;/code&gt; to augment it with custom features, such as color and year. This flexibility to modify an object's prototype dynamically allows for robust object manipulation and adaptability.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Are They Related or Different?
&lt;/h2&gt;

&lt;p&gt;To summarize, the key distinctions between &lt;code&gt;prototype&lt;/code&gt; and &lt;code&gt;__proto__&lt;/code&gt; are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Prototype&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is a property attached to a function or object.&lt;/li&gt;
&lt;li&gt;It is used to define shared properties and methods for objects created from that function or constructor.&lt;/li&gt;
&lt;li&gt;It plays a central role in enabling inheritance and code reusability.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;__proto__&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is a property attached to every object in JavaScript.&lt;/li&gt;
&lt;li&gt;It is used to access and modify the prototype of an object.&lt;/li&gt;
&lt;li&gt;It facilitates dynamic changes to an object's characteristics by allowing you to manipulate its prototype chain.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These differences underscore their complementary roles in JavaScript's object-oriented programming paradigm.&lt;/p&gt;

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

&lt;p&gt;Let's explore a practical example that highlights the distinction between &lt;code&gt;prototype&lt;/code&gt; and &lt;code&gt;__proto__&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;side&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;shape&lt;/span&gt; &lt;span class="o"&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;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;area&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;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: 25&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a &lt;code&gt;Square&lt;/code&gt; constructor function and create an empty &lt;code&gt;shape&lt;/code&gt; object. By assigning &lt;code&gt;shape.__proto__&lt;/code&gt; to the &lt;code&gt;square&lt;/code&gt; object, we effectively set the &lt;code&gt;shape&lt;/code&gt; object's prototype to be the same as that of &lt;code&gt;square&lt;/code&gt;. Then, we add an &lt;code&gt;area&lt;/code&gt; method to the &lt;code&gt;shape&lt;/code&gt; object. The &lt;code&gt;console.log&lt;/code&gt; statements illustrate the relationship between &lt;code&gt;shape.__proto__&lt;/code&gt; and &lt;code&gt;Square.prototype&lt;/code&gt;, both of which are true, and we calculate the area of the square.&lt;/p&gt;

&lt;p&gt;Understanding the distinction between &lt;code&gt;prototype&lt;/code&gt; and &lt;code&gt;__proto__&lt;/code&gt; is crucial for mastering JavaScript's object-oriented capabilities and enhancing your ability to design and manipulate objects effectively. These concepts are fundamental to writing efficient and maintainable code in JavaScript.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using Reverse Proxy for multiple backends - Nginx</title>
      <dc:creator>Bartick Maiti</dc:creator>
      <pubDate>Tue, 05 Sep 2023 10:27:24 +0000</pubDate>
      <link>https://dev.to/bartick/using-reverse-proxy-for-multiple-backends-nginx-5ano</link>
      <guid>https://dev.to/bartick/using-reverse-proxy-for-multiple-backends-nginx-5ano</guid>
      <description>&lt;p&gt;I work as a backend developer, and that means I handle various backend projects. When these projects go live, we usually need to open specific ports, like 80 or 443, so that users can access them.&lt;/p&gt;

&lt;p&gt;As a student, I often have multiple projects to manage, but I don't have the financial means to pay for separate Virtual Private Servers (VPS) to host each project. This dilemma has been on my mind for quite some time.&lt;/p&gt;

&lt;p&gt;After doing some research online, I came across a solution: using a reverse proxy. With the VPS I already have, I can now host multiple projects and employ a reverse proxy to guide users to the correct project based on the web address they use. This discovery has opened up a cost-effective way to manage all my projects efficiently.&lt;/p&gt;

&lt;p&gt;In this article, I will show you how I use a reverse proxy to host multiple projects on a single VPS. And I will use Nginx as my reverse proxy.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a reverse proxy?
&lt;/h2&gt;

&lt;p&gt;A reverse proxy is a server or software application that acts as an intermediary between client devices and one or more backend servers. It receives requests from clients (such as web browsers) and forwards those requests to the appropriate backend server. When the backend server responds, the reverse proxy then sends the response back to the client.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l8beqD7j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51v3vvmqd3wzmvn6a4w7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l8beqD7j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51v3vvmqd3wzmvn6a4w7.png" alt="Reverse Proxy" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use a reverse proxy?
&lt;/h2&gt;

&lt;p&gt;There are many reasons why you might want to use a reverse proxy. Here are some of the most common ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Load Balancing:&lt;/strong&gt; They distribute incoming requests across multiple backend servers, helping to distribute traffic evenly and prevent server overload.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Security:&lt;/strong&gt; They can hide the internal architecture of a network by only exposing the reverse proxy to the internet. This adds a layer of security, as the backend servers' details are hidden.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Caching:&lt;/strong&gt; Reverse proxies can cache static content, reducing the load on backend servers and improving performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SSL/TLS Termination:&lt;/strong&gt; They can handle SSL/TLS encryption and decryption, offloading this processing from backend servers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Content Compression:&lt;/strong&gt; They can compress responses before sending them to clients, reducing data transfer times.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's get started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A VPS with a static IP address&lt;/li&gt;
&lt;li&gt;A domain name&lt;/li&gt;
&lt;li&gt;A basic understanding of how to use the command line&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1: Install Nginx
&lt;/h3&gt;

&lt;p&gt;Nginx is a popular web server that can also be used as a reverse proxy. It is free and open-source software, and it is available for most operating systems.&lt;/p&gt;

&lt;p&gt;To install Nginx on Ubuntu, run 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;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Start Nginx
&lt;/h3&gt;

&lt;p&gt;Once Nginx is installed, you can start it using the following command and potentially check if it was installed properly or not:&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;sudo &lt;/span&gt;systemctl start nginx
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go to your browser and type your VPS IP address if Nginx is running properly you should see the following page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HCuJMWOX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wij7suc9khze2d2y7v3z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HCuJMWOX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wij7suc9khze2d2y7v3z.png" alt="Nginx Welcome Page" width="780" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If it's not reachable, you may need to open port 80 on your firewall. You can do this by running 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;&lt;span class="nb"&gt;sudo &lt;/span&gt;ufw allow 80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Configure Nginx for multiple backends
&lt;/h3&gt;

&lt;p&gt;Before we can use Nginx as a reverse proxy, host your projects on your VPS on internal non-exposed ports (e.g. 3000, 5000, 8000, etc.). Then, create a new file in the &lt;code&gt;/etc/nginx/sites-available&lt;/code&gt; directory called &lt;code&gt;yourdomain.com&lt;/code&gt; and add the following content:&lt;/p&gt;

&lt;p&gt;In this example, I will use the domain &lt;code&gt;yourdomain.com&lt;/code&gt; and the port &lt;code&gt;3000&lt;/code&gt; for the backend server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration tells Nginx to listen on port 80 for requests to &lt;code&gt;yourdomain.com&lt;/code&gt;. When it receives a request, it will forward it to the backend server running on port 3000.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optional: Configure SSL/TLS
&lt;/h3&gt;

&lt;p&gt;If you want to use SSL/TLS encryption, you can add the following lines to the configuration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate /path/to/yourdomain.com.crt;
    ssl_certificate_key /path/to/yourdomain.com.key;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To generate an ssl certificate you can use Let's Encrypt. You can find more information about how to do this &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Enable the new configuration
&lt;/h3&gt;

&lt;p&gt;Once you have created the new configuration file, you need to enable it by creating a symbolic link in the &lt;code&gt;/etc/nginx/sites-enabled&lt;/code&gt; directory. You can do this by running 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;&lt;span class="nb"&gt;sudo ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Test the configuration
&lt;/h3&gt;

&lt;p&gt;Before you can use the new configuration, you need to test it to make sure it is valid. You can do this by running 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;&lt;span class="nb"&gt;sudo &lt;/span&gt;nginx &lt;span class="nt"&gt;-t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the configuration is valid, you should see the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf &lt;span class="nb"&gt;test &lt;/span&gt;is successful
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see any errors, you will need to fix them before you can continue.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6: Restart Nginx
&lt;/h3&gt;

&lt;p&gt;Once you have tested the configuration, you can restart Nginx to apply the changes. You can do this by running 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;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl reload nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 7: Configure your domain name
&lt;/h3&gt;

&lt;p&gt;Now that you have configured Nginx, you need to configure your domain name to point to your VPS. You can do this by adding an A record to your domain name's DNS settings. The A record should point to your VPS's IP address.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 8: Test the configuration
&lt;/h3&gt;

&lt;p&gt;Once you have configured your domain name, you can test the configuration by visiting your domain name in your browser. If everything is working correctly, you should see your project running on your VPS.&lt;/p&gt;

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

&lt;p&gt;In this article, I have shown you how to use a reverse proxy to host multiple projects on a single VPS. I hope you have found this article useful. If you have any questions or suggestions, feel free to reach out to me on &lt;a href="https://twitter.com/BartickM"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>backenddevelopment</category>
      <category>nginx</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Autoplay YouTube embed with sound</title>
      <dc:creator>Bartick Maiti</dc:creator>
      <pubDate>Sat, 22 Jul 2023 09:28:16 +0000</pubDate>
      <link>https://dev.to/bartick/autoplay-youtube-embed-with-sound-383g</link>
      <guid>https://dev.to/bartick/autoplay-youtube-embed-with-sound-383g</guid>
      <description>&lt;p&gt;I have always wondered how can I autoplay youtube embed with sound in my website from the start of my software development career. And I am wondering if you have stumbled upon this article, you might be wondering the same. &lt;/p&gt;

&lt;p&gt;So I was fiddling around with HTML and Youtube video embeds to make a CTF challenge for my peers. I was using VSCode's Live Server extension to test my code. &lt;/p&gt;

&lt;p&gt;For some reason, it has become my habit for a long time to add &lt;code&gt;?autoplay=1&lt;/code&gt; whenever I work with YouTube embeds. So I did the same and as usual it didn't work. I was like "What the hell?". I tried to find the solution on the Internet but I couldn't find any. Gave up and went to make the initial challenge. &lt;/p&gt;

&lt;p&gt;While working on the challenge after all that hard work the video suddenly autoplayed with sound. Now my curiosity was at its peak. Now I was determined to find the solution no matter what.&lt;/p&gt;

&lt;p&gt;On further investigation, I found that the video autoplayed with sound when I changed something on the website using Live Server but it didn't autoplayed with sound when I refreshed the page.&lt;/p&gt;

&lt;p&gt;So now lets's google.&lt;/p&gt;

&lt;p&gt;Now did I find the solution on the internet? The simple answer is no. But I stumbled upon this StackOverflow &lt;a href="https://stackoverflow.com/questions/73651599/embed-youtube-video-doesnt-autoplay-with-audio" rel="noopener noreferrer"&gt;question&lt;/a&gt;. Is it a solid start? Maybe. Let's see what the accepted answer says.&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%2Fblog.bartick.me%2Fimages%2Fposts%2Fvideo-autoplay-stackoverflow.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%2Fblog.bartick.me%2Fimages%2Fposts%2Fvideo-autoplay-stackoverflow.png" alt="Browser Dosen't Support Autoplay with Sound"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the answer says that the browser doesn’t support autoplay with sound. But I have seen it working on my browser, I have also seen Marvel use autoplay for their &lt;a href="https://www.theinvasionhasbegun.com/" rel="noopener noreferrer"&gt;Secret Invasion&lt;/a&gt; and according to the answer the browser should block any autoplay with sound. So I did some more googling and found this &lt;a href="https://developers.google.com/web/updates/2017/09/autoplay-policy-changes" rel="noopener noreferrer"&gt;article&lt;/a&gt; on Google.&lt;/p&gt;

&lt;p&gt;Bingo! Found the solution. So I have the solution and now I just need the user to do any action on the website to play the video with sound.&lt;/p&gt;

&lt;p&gt;Now let's implement the solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation: (Let's mimic the secret invasion website)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Create a simple html page
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Try It&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Let us add the text area to add the password
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"autoplay"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"overPassword"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Enter the password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Let's now listen to the keypress event on the input
&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;const&lt;/span&gt; &lt;span class="nx"&gt;passwordForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.overPassword&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;passwordInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;password_input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;keypress&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;e&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="c1"&gt;//TODO: code here&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Up until now we made sure that the user has somehow interacted with the website. Now let's add the youtube embed with autoplay and see if it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Add the youtube embed
&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;passwordInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;keypress&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;e&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;passwordForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&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;iframe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;iframe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`https://www.youtube.com/embed/&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;?autoplay=1&amp;amp;rel=0&amp;amp;controls=0&amp;amp;showinfo=0`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;frameborder&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;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;allow&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;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;allowfullscreen&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="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;width&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;1280&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;height&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;620&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.autoplay&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iframe&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;Boom! It works. Now you can use this trick to make your website more fun to interact with. &lt;/p&gt;

&lt;p&gt;Let's see a live demo of the same here: &lt;a href="https://tryit.bartick.me/" rel="noopener noreferrer"&gt;https://tryit.bartick.me/&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;So we have learned how to autoplay youtube embed with sound in our website. I hope you have enjoyed this article. If you have any questions or suggestions feel free to reach out to me on &lt;a href="https://twitter.com/BartickM" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extra
&lt;/h3&gt;

&lt;p&gt;All mobile browsers don't support &lt;code&gt;keypress&lt;/code&gt; event so you can use the &lt;code&gt;keydown&lt;/code&gt; event instead. &lt;code&gt;Keydown&lt;/code&gt; will work on both mobile and desktop browsers.&lt;/p&gt;

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