<?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: Saurabh Dashora</title>
    <description>The latest articles on DEV Community by Saurabh Dashora (@dashsaurabh).</description>
    <link>https://dev.to/dashsaurabh</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%2F298362%2F608ec4c9-0349-4838-93c2-c14d7a7f2780.jpeg</url>
      <title>DEV Community: Saurabh Dashora</title>
      <link>https://dev.to/dashsaurabh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dashsaurabh"/>
    <language>en</language>
    <item>
      <title>Why the Cloud Revolution is just getting started?</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Mon, 20 Feb 2023 10:59:58 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/why-the-cloud-revolution-is-just-getting-started-45jf</link>
      <guid>https://dev.to/dashsaurabh/why-the-cloud-revolution-is-just-getting-started-45jf</guid>
      <description>&lt;p&gt;When cloud computing burst onto the scene in 2006 with the launch of AWS, it would have been hard to imagine how big of a thing it would eventually become.&lt;/p&gt;

&lt;p&gt;But more than 15 years later, cloud computing has come a long way. And yet, in my view, it is only just getting started towards realizing its true potential.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do I think this way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recently, I came across this Gartner study that contained a couple of mind-boggling facts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;More than 85% of organizations&lt;/strong&gt; will embrace a cloud-first principle by 2025&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over 95% of new digital workloads&lt;/strong&gt; in 2025 will be deployed on cloud-native platforms, up from 30% in 2021&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, numbers can be spurious. But when you talk about 85% of organizations and 95% of all new digital workloads, it is definitely a lot. Even if the figures are off the mark by some points, they are still huge.&lt;/p&gt;

&lt;p&gt;My curiosity about the matter was piqued and I started digging more into the potential trends that might fuel this anticipated growth. Naturally, as a software developer, I’m always interested in knowing where the industry is headed because that’s how we can prepare and hope to keep ourselves relevant.&lt;/p&gt;

&lt;p&gt;After doing some reading up, I have formed an initial idea of the broad trends that are driving cloud computing and will continue to do so in the coming years.&lt;/p&gt;

&lt;p&gt;However, before I share them with you, I wanted to make some points about cloud computing that can help us understand the trends in a much better way.&lt;/p&gt;

&lt;p&gt;You may or may not agree with the points I make. Either way, I’d love it if you share your views in the comments section below.&lt;/p&gt;

&lt;h2&gt;
  
  
  What made the Cloud so popular?
&lt;/h2&gt;

&lt;p&gt;I believe that the seeds of the future are laid in the past. This must hold true for cloud computing as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;But what made the cloud so popular?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my view, cloud computing democratized the ability to build applications with world-class infrastructure.&lt;/p&gt;

&lt;p&gt;No longer do you need to be a multi-billion dollar organization with an army of engineers to create applications used by millions of people. Even a startup working out of a garage can do it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, what was stopping the same thing from happening in the pre-cloud era?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For starters, the pre-cloud era could also be labelled as the on-premise era. This meant that organizations typically managed their own IT infrastructure and resources.&lt;/p&gt;

&lt;p&gt;For example, if you wanted to create an application and make it available to the world in the pre-cloud era, you had to purchase, install and maintain hardware and software in-house.&lt;/p&gt;

&lt;p&gt;This arrangement had a couple of big technical implications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Management of IT infrastructure such as servers, storage and networking lay solely on the shoulders of the organization’s workforce. I still vividly remember the anecdote of one of my seniors about the earlier days when he had to even fix network cables when a broken connection brought their application down in the middle of the night and the network vendor was not available.&lt;/li&gt;
&lt;li&gt;The IT systems were &lt;strong&gt;not scalabl&lt;/strong&gt;e based on demand since organizations were limited by physical resources. If there was an expectation of higher demand, you had to go out and buy more resources. For that to happen, you had to be really good at predicting demand else you would be incurring extra costs for no reason.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, when are higher-ups in organizations worried about tech issues unless there was a threat to the company’s bottom line?&lt;/p&gt;

&lt;p&gt;However, in this case, there were threats.&lt;/p&gt;

&lt;p&gt;For starters, &lt;strong&gt;on-premise computing is a costly business&lt;/strong&gt;. You need a significant up-front investment in hardware and software to build a data centre.&lt;/p&gt;

&lt;p&gt;Initially, big companies loved this situation. It was like a huge barrier to entry for smaller players. However, once the genie was out-of-the-bottle and some cloud offerings came on the scene, the huge cost associated with on-premise computing became a liability.&lt;/p&gt;

&lt;p&gt;Suddenly, the army of engineers hired to just keep the infrastructure running started to look like money-guzzling machines.&lt;/p&gt;

&lt;p&gt;In more disruptive industries, startups with a skeleton crew of software engineers were leap-frogging established players by using the initial cloud tools to drive &lt;strong&gt;faster innovation&lt;/strong&gt; and &lt;strong&gt;reducing time-to-market&lt;/strong&gt;. This meant a loss of market share and growth opportunities for the big companies.&lt;/p&gt;

&lt;p&gt;Of course, all of this didn’t happen in a single day, a month or even a year. But slowly and steadily, large organizations also started to steer their ships in the direction of cloud computing.&lt;/p&gt;

&lt;p&gt;Once that happened, there was no looking back.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F30n74u7xp6iret163sgi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F30n74u7xp6iret163sgi.png" alt="Evolution of Cloud" width="800" height="2000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With more investment coming into the business, cloud computing took off with multiple vendors entering the field. Companies like Google and Microsoft started to compete with Amazon for a pie of this incredibly lucrative business model.&lt;/p&gt;

&lt;p&gt;It was like discovering an untapped oil field right next to your front door.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, where are things headed?
&lt;/h2&gt;

&lt;p&gt;Predicting the direction of a particular technology can be a fool’s errand. Even in 2006, not even the creators of AWS would have predicted the kind of growth they have seen.&lt;/p&gt;

&lt;p&gt;But, of course, it’s important to make an educated guess so that we are better prepared for what’s coming in the next few years.&lt;/p&gt;

&lt;p&gt;Here are a few broad trends I’m tracking:&lt;/p&gt;

&lt;h3&gt;
  
  
  Hybrid Cloud Adoption
&lt;/h3&gt;

&lt;p&gt;This one’s a biggie as it is driven mostly by the large organizations that run the world. Think of big banks, government organizations and mega-corporations.&lt;/p&gt;

&lt;p&gt;The trend is largely driven by the growing regulatory and legal requirements about data and the increase in privacy concerns all across the world.&lt;/p&gt;

&lt;p&gt;In a hybrid cloud setup, companies want to keep a mix of capabilities across external cloud platforms as well as in-house setups. The idea is to use the public platforms for new innovative products but keep the core-business capabilities or data on in-house data centres so that they don’t run foul of government regulations.&lt;/p&gt;

&lt;p&gt;Since it involves big money, I feel hybrid cloud adoption is only going to grow. Already, big cloud providers are rolling out products to support this vision.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Red Hat has been offering its flagship &lt;a href="https://www.redhat.com/en/technologies/cloud-computing/openshift" rel="noopener noreferrer"&gt;OpenShift platform&lt;/a&gt; as an on-premise solution for many years now.&lt;/li&gt;
&lt;li&gt;Microsoft has launched Azure Arc to cater to hybrid and multi-cloud requirements.&lt;/li&gt;
&lt;li&gt;Google has launched Anthos - a platform that promises a single and consistent way of managing Kubernetes workloads across the public and on-premises cloud.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Multi-Cloud Adoption
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;“Don’t keep all your eggs in one basket”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This adage is so wise that organizations are increasingly exploring the use of a multi-cloud approach.&lt;/p&gt;

&lt;p&gt;A large part of the multi-cloud adoption is driven by risk mitigation. For example, fin-tech organization &lt;a href="https://www.cockroachlabs.com/blog/fintech-multi-cloud-architecture/" rel="noopener noreferrer"&gt;Form3 was compelled to go for a multi-cloud setup&lt;/a&gt; when the regulators questioned them about the portability of their platform in case AWS went down.&lt;/p&gt;

&lt;p&gt;However, some of the shift to multi-cloud is also a result of increased competition and service offerings by different cloud vendors. Even beyond the Big 3, there are dozens of other cloud providers providing all manner of cloud services to lure customers with cost or features.&lt;/p&gt;

&lt;p&gt;Organizations have been spoilt for choice and are trying to get the best ROI for every piece of their infrastructure.&lt;/p&gt;

&lt;p&gt;I feel this trend is going to accelerate in the future.&lt;/p&gt;

&lt;p&gt;The difficulties of managing a multi-cloud setup could have curtailed this movement. However, instead of getting bogged down, the demand for multi-cloud and hybrid-cloud setups has spurred a number of new trends such as the rise of infrastructure-as-code tools and the concept of platform engineering. I will discuss more of them in upcoming posts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Serverless Computing
&lt;/h3&gt;

&lt;p&gt;One of the main factors that worked in favour of the cloud in the initial days was cost savings. The idea that you could even launch a product with close-to-zero costs was hard to beat and created a tremendous rush for cloud adoption.&lt;/p&gt;

&lt;p&gt;In my view, serverless has the potential to make even traditional cloud offerings appear costly. Though a few years have passed since serverless options were launched by most major cloud providers, I feel that we are only at the beginning of the serverless revolution.&lt;/p&gt;

&lt;p&gt;Since serverless computing allows companies to run code without even provisioning or managing cloud servers, it is extremely lucrative for organizations that want to save on costs and move faster.&lt;/p&gt;

&lt;p&gt;With the tremendous rise in the number of SaaS startups and the existence of an inflationary environment with rising interest rates, the cost of running your system is a big issue.&lt;/p&gt;

&lt;p&gt;Organizations are looking to achieve product-market fit without burning through too much cash and serverless computing seems like a good deal with its pay-per-use model and little to no maintenance expenditure.&lt;/p&gt;

&lt;h3&gt;
  
  
  AI-Driven Cloud
&lt;/h3&gt;

&lt;p&gt;Apart from cloud computing, the last decade or so has also seen another major trend spread like wildfire - the rise of machine learning and artificial intelligence.&lt;/p&gt;

&lt;p&gt;As AI seeps into more and more areas and supports real requirements, it is already promising to augment cloud services in really interesting ways.&lt;/p&gt;

&lt;p&gt;For example, AI-driven cloud services can potentially make autonomous decisions on when to scale up or down based on an intuitive understanding of demand rather than fixed rules.&lt;/p&gt;

&lt;p&gt;Again, this boils down to monetary benefits with the promise of better cost utilization.&lt;/p&gt;

&lt;p&gt;Of course, we can only hope that one of these services doesn’t turn into Skynet any time soon!&lt;/p&gt;

&lt;p&gt;Either way, I’ll be extremely interested in keeping an eye out for this developing trend.&lt;/p&gt;

&lt;h3&gt;
  
  
  Containers as a Service
&lt;/h3&gt;

&lt;p&gt;Containers on the cloud started off quite early with Amazon launching ECS. Of course, managing a bunch of containers isn’t the easiest thing out there.&lt;/p&gt;

&lt;p&gt;However, the surging popularity of Kubernetes has changed the landscape of container orchestration. And within no time, all major and minor cloud providers are offering managed Kubernetes services.&lt;/p&gt;

&lt;p&gt;This is one area where big and small organizations are lapping up the opportunity. After all, everyone wants to reap the benefits of containerization without acquiring the headache of managing them.&lt;/p&gt;

&lt;p&gt;As developers, it is definitely important to keep abreast of this trend.&lt;/p&gt;

&lt;h2&gt;
  
  
  That’s it for now!
&lt;/h2&gt;

&lt;p&gt;In the end, I feel that we are living in interesting times when it comes to cloud computing.&lt;/p&gt;

&lt;p&gt;The technology is at the right level of maturity where it has become mainstream enough to have a large base of innovation. However, it is also not so dormant that things become boring and static.&lt;/p&gt;

&lt;p&gt;To top it off, cloud computing is also producing other trends in areas such as microservices architecture, DevOps, &lt;strong&gt;infrastructure-as-code&lt;/strong&gt; and &lt;strong&gt;platform engineering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As we learn more about the cloud, we also will be learning a lot about these other secondary trends that have the potential to become even more important. I will definitely be talking about them a lot more in the upcoming days and weeks.&lt;/p&gt;

&lt;p&gt;If you liked today’s post, consider sharing it with friends and colleagues. &lt;/p&gt;




&lt;p&gt;Do you want to stay updated and relevant to the latest trends in technology?&lt;/p&gt;

&lt;p&gt;Do you also want to get actionable advice about cloud and infrastructure topics?&lt;/p&gt;

&lt;p&gt;If yes, subscribe to the &lt;a href="https://progressivecoder.substack.com/" rel="noopener noreferrer"&gt;ProgressiveCoder newsletter&lt;/a&gt; for FREE.&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>5 Must-Ask questions before you implement Caching</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Wed, 08 Feb 2023 08:03:24 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/5-must-ask-questions-before-you-implement-caching-1md2</link>
      <guid>https://dev.to/dashsaurabh/5-must-ask-questions-before-you-implement-caching-1md2</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;There are only two hard things in Computer Science: cache invalidation and naming things.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The above statement by &lt;strong&gt;Phil Karlton&lt;/strong&gt; has acquired some sort of legendary status in software development circles. And not without reason.&lt;/p&gt;

&lt;p&gt;Caching has the potential to be the most important piece in your app development puzzle. It solves a bunch of important problems in modern app development such as :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;performance&lt;/li&gt;
&lt;li&gt;scalability&lt;/li&gt;
&lt;li&gt;cost&lt;/li&gt;
&lt;li&gt;availability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But despite all the benefits, you must &lt;strong&gt;not&lt;/strong&gt; decide to use it blindly.&lt;/p&gt;

&lt;p&gt;Any approach is beneficial only when it provides some value over the current situation.&lt;/p&gt;

&lt;p&gt;Before deciding to implement caching, ask yourself the below questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  1 - Is the operation I’m trying to cache slow?
&lt;/h2&gt;

&lt;p&gt;Caching the result of an operation is beneficial only if the operation is really slow or resource-intensive.&lt;/p&gt;

&lt;p&gt;For example, let’s say you are trying to use an external Weather API to retrieve some information. The Weather API may be slow or it may be expensive to use due to usage limits (in other words, resource-intensive).&lt;/p&gt;

&lt;p&gt;In this case, you would do well to implement a cache to store the results of the Weather API query. When a user makes a query, you can first check if the data is available in the cache and call the slow and costly Weather API only when needed.&lt;/p&gt;

&lt;p&gt;As a rule of thumb, always check whether you are trying to access a slow external API or a database. If yes, only then consider the use of caching. Otherwise, you would end up caching for no benefit and additional complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  2 - Is the cache actually faster?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Don’t cache just for the sake of caching!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The cache must be able to store and retrieve faster than the original source. Alternatively, it should consume fewer resources.&lt;/p&gt;

&lt;p&gt;However, sometimes it is not immediately clear if caching will be advantageous. To make a decision, try to set up a test environment where you can simulate a high volume of traffic. Run tests with and without the cache and compare the results. If the performance improves due to caching, then and only then go for a caching-based solution.&lt;/p&gt;

&lt;p&gt;Ultimately, there should be some quantitative advantage of using cache.&lt;/p&gt;

&lt;h2&gt;
  
  
  3 - Is the data I’m trying to cache dynamic?
&lt;/h2&gt;

&lt;p&gt;Suppose your cache stores the results of a search query. When a user makes a new search query, you retrieve the results and also store them in the cache. On subsequent requests for the same query by other users, you return the cached results.&lt;/p&gt;

&lt;p&gt;While caching such data, &lt;strong&gt;ask yourself how long until the cached result becomes stale&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If the cached data becomes stale very frequently and you have to invalidate it, you might not get sufficient advantage from caching. Items that don’t change from request to request are better candidates for caching.&lt;/p&gt;

&lt;h2&gt;
  
  
  4 - Is the data frequently accessed?
&lt;/h2&gt;

&lt;p&gt;Let’s consider our earlier example of an e-commerce platform.&lt;/p&gt;

&lt;p&gt;If you think there is a popular product in your catalog, its product page may receive a ton of requests. If the details are fetched from the database every single time, your application may perform poorly due to overloading. This is an ideal scenario to explore the use of cache.&lt;/p&gt;

&lt;p&gt;As a rule, always &lt;strong&gt;ask yourself how frequently a piece of data is needed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The more times it is needed, the more effective the use of cache will be.&lt;/p&gt;

&lt;p&gt;To answer this question, you need to have a really good understanding of the statistical distribution of data access from your data source.&lt;/p&gt;

&lt;p&gt;Caching is more likely to be effective for your use case when your data has a normal &lt;strong&gt;bell-curve distribution&lt;/strong&gt; instead of flat distribution in terms of access.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z9_g7i5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnwntbt274vd4v1f3qr3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z9_g7i5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnwntbt274vd4v1f3qr3.png" alt="bell-curve-distribution" width="601" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5 - Does the original operation have side effects?
&lt;/h2&gt;

&lt;p&gt;If you want to cache the result of an operation, it must not have side effects. For example, the operation should not store data, make changes to other systems or control some software or hardware item.&lt;/p&gt;

&lt;p&gt;If the result of such operations is cached, you will end up breaking your application when the requests are served from the cache and the side effects are ignored.&lt;/p&gt;

&lt;h2&gt;
  
  
  That’s it
&lt;/h2&gt;

&lt;p&gt;The decision to use a cache is not an emotional matter.&lt;/p&gt;

&lt;p&gt;Though caching has a very important role in application development, you need to run your use case through the lens of hard questions.&lt;/p&gt;

&lt;p&gt;If you find satisfactory answers that point to the use of a cache, you will be able to reap the benefits in the long run. Otherwise, caching systems can become more of a liability.&lt;/p&gt;




&lt;p&gt;Do you want to stay updated and relevant to the latest trends in technology?&lt;/p&gt;

&lt;p&gt;Do you also want to get actionable advice about web development, cloud computing, distributed systems, platform engineering and software architecture?&lt;/p&gt;

&lt;p&gt;If yes, subscribe to the &lt;a href="https://progressivecoder.substack.com/"&gt;ProgressiveCoder newsletter&lt;/a&gt; for &lt;strong&gt;FREE&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>cloud</category>
    </item>
    <item>
      <title>5 Must-Avoid Pitfalls in Software Development</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Thu, 02 Feb 2023 13:05:07 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/5-must-avoid-pitfalls-in-software-development-9hg</link>
      <guid>https://dev.to/dashsaurabh/5-must-avoid-pitfalls-in-software-development-9hg</guid>
      <description>&lt;p&gt;Software development is the ultimate test of &lt;strong&gt;patience, problem-solving skills and sheer determination&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But even the most seasoned developers can fall into common pitfalls that throw a wrench into their coding magic. &lt;/p&gt;

&lt;p&gt;Yes, I'm talking about those things that seem like a great idea at the time but end up causing chaos and frustration later on.&lt;/p&gt;

&lt;p&gt;In this post, I'm going to take you through &lt;strong&gt;5 software development pitfalls&lt;/strong&gt; that you must avoid like the plague. &lt;/p&gt;

&lt;p&gt;So put on your best &lt;em&gt;'I won't make these mistakes'&lt;/em&gt; grin, and let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfall#1 - Not working on clearly defined requirements
&lt;/h2&gt;

&lt;p&gt;Picture this - You've spent countless hours pouring your heart and soul into a project, only to realize that it's not quite what your client had in mind.&lt;/p&gt;

&lt;p&gt;Maybe, the client didn't define their requirements and you didn't bother to make them do so.&lt;/p&gt;

&lt;p&gt;The problem is that without clear and specific requirements, you're basically coding in the dark. &lt;/p&gt;

&lt;p&gt;And let's be honest, who wants to waste their time building something that nobody wants?&lt;/p&gt;

&lt;p&gt;How do you avoid this pitfall?&lt;/p&gt;

&lt;p&gt;Here are a few tried and tested ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conduct stakeholder interviews&lt;/li&gt;
&lt;li&gt;Create user stories&lt;/li&gt;
&lt;li&gt;Clarify vague or conflicting requirements&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pitfall#2 - Underestimating the Time and Effort Required
&lt;/h2&gt;

&lt;p&gt;This one's my favourite. I have seen it happen so many times that if I give myself a dollar for every time I saw it, I'd be a millionaire by now.&lt;/p&gt;

&lt;p&gt;It's the good old game of &lt;strong&gt;'How quickly can I get this done?'&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For some reason, software developers are guilty folks. When someone asks them to provide an estimate, they always think that they are doing something wrong by giving a reasonable number.&lt;/p&gt;

&lt;p&gt;I really wish developers would cut themselves some slack.&lt;/p&gt;

&lt;p&gt;Creating software is like a puzzle with a million pieces. And if you don't account for each and every one of those pieces and then some more, things can get messy pretty fast.&lt;/p&gt;

&lt;p&gt;Here are some things you can do to avoid falling into the trap of missed deadlines and reduced quality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Be realistic about the time and effort required for a particular requirement.&lt;/li&gt;
&lt;li&gt;Use project management tools to keep yourself in check.&lt;/li&gt;
&lt;li&gt;Regularly revisit your project timeline to make sure you stay on track.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pitfall#3 - Neglecting User Feedback
&lt;/h2&gt;

&lt;p&gt;User feedback is like the voice of the people and the pulse of the product.&lt;/p&gt;

&lt;p&gt;Don't be that developer who shuts their ears to the words of the user. &lt;/p&gt;

&lt;p&gt;Of course, it does &lt;strong&gt;not&lt;/strong&gt; mean that you agree to all their demands even if they are impossible. But your attempt should be to collaborate with them to deliver the best possible product.&lt;/p&gt;

&lt;p&gt;When in doubt, remember that software development is all about solving problems for &lt;strong&gt;real&lt;/strong&gt; people. If you neglect the feedback of the user, you risk everything.&lt;/p&gt;

&lt;p&gt;I suggest that you should always try to understand the user's viewpoint before starting to write code. That will tell you the &lt;strong&gt;WHY&lt;/strong&gt; of the product.&lt;/p&gt;

&lt;p&gt;Conduct user testing, show prototypes and incorporate feedback into your development plans. Take more time if needed. Trust me, your users will thank you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfall#4 - Not Doing Proper Testing and Debugging
&lt;/h2&gt;

&lt;p&gt;"We will figure it out later". &lt;/p&gt;

&lt;p&gt;This is the single biggest lie in software development. Your job as a software developer is to figure things out before they happen.&lt;/p&gt;

&lt;p&gt;Just like a tightrope walker needs a net, your code needs proper testing and debugging to keep it from crashing down. &lt;/p&gt;

&lt;p&gt;Neglecting these steps can lead to all sorts of issues like slow performance and a string of bugs.&lt;/p&gt;

&lt;p&gt;While building your application, &lt;strong&gt;dedicate time for both automated testing and manual testing along with debugging&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Don't be in a rush to deliver software before all the major issues are fixed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfall#5 - Neglecting Security
&lt;/h2&gt;

&lt;p&gt;Don't be that guy! Trust me, there's no fun in that.&lt;/p&gt;

&lt;p&gt;Yes, I'm talking about the developer that ignores security. &lt;/p&gt;

&lt;p&gt;As a developer, it's your job to conduct regular security audits, use secure coding practices and stay up-to-date with the latest security trends.&lt;/p&gt;

&lt;p&gt;However, even if you are not experienced enough in complicated security matters, you should not feign ignorance. &lt;/p&gt;

&lt;p&gt;Try to find someone who knows about it. Ask around and take help. Get your application reviewed by the security department if there's one. Delay the project if you must but don't take security matters for granted, or you'll be sorry!&lt;/p&gt;

&lt;h2&gt;
  
  
  That's it for now!
&lt;/h2&gt;

&lt;p&gt;These are the 5 must-avoid pitfalls that I could think of right now. &lt;/p&gt;

&lt;p&gt;But there may be more. Don't hesitate to write about them in the comments section below. After all, software developers should have each other's back.&lt;/p&gt;

&lt;p&gt;If you found this post useful, do share it with your friends and colleagues.&lt;/p&gt;

&lt;p&gt;You can also connect with me on other platforms:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://progressivecoder.substack.com/" rel="noopener noreferrer"&gt;My Regular Newsletter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/ProgressiveCod2" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/saurabh-dashora-b3601173/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/channel/UCcD92x9BQLvvm-8jtsg12kw" rel="noopener noreferrer"&gt;Youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>crypto</category>
      <category>blockchain</category>
      <category>web3</category>
      <category>offers</category>
    </item>
    <item>
      <title>9 Must-Know Rules for Writing Clean Code</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Mon, 30 Jan 2023 09:40:35 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/9-must-know-rules-for-writing-clean-code-1kp1</link>
      <guid>https://dev.to/dashsaurabh/9-must-know-rules-for-writing-clean-code-1kp1</guid>
      <description>&lt;p&gt;Are you tired of staring at a mess of code that even your cat wouldn’t touch?&lt;/p&gt;

&lt;p&gt;Look no further. In this post, I will tell you 9 rules to turn your spaghetti code into a work of art.&lt;/p&gt;

&lt;p&gt;So grab a cup of coffee, sit back and take charge of your code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule#1 - Keep your code organized and easy to read
&lt;/h2&gt;

&lt;p&gt;Don’t organize your wardrobe but definitely organize your code. By keeping your code organized and easy to read, you can find what you need in a snap.&lt;/p&gt;

&lt;p&gt;Check the below 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="c1"&gt;// Group related functions together&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculateTax&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculateDiscount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Use comments to explain the purpose of the code&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// calculate subtotal&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;subtotal&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="c1"&gt;// add items&lt;/span&gt;
  &lt;span class="nx"&gt;subtotal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;item1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;subtotal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;item2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// return total&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;subtotal&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;As you can see, a little bit of planning and a healthy dose of comments can make it a breeze to navigate your code. &lt;/p&gt;

&lt;p&gt;At one glance, you get a good idea of the overall functionality even after it’s been a while since you worked on the code.&lt;/p&gt;

&lt;p&gt;It’s not every day that you thank your past self for doing something good!&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule#2 - Follow Established Coding Conventions and Standards
&lt;/h2&gt;

&lt;p&gt;I know it is tempting to be a rebel and chart your own course. &lt;/p&gt;

&lt;p&gt;But sometimes, it pays to follow established coding conventions and standards. This includes things like naming conventions, commenting guidelines and more.&lt;/p&gt;

&lt;p&gt;See the below snippet for what I’m trying to say.&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;// Naming conventions&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// camelCase for variables and properties&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MAX_USERS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// UpperCamelCase for constants&lt;/span&gt;

&lt;span class="c1"&gt;// Commenting guidelines&lt;/span&gt;
&lt;span class="c1"&gt;// This function takes in a number and returns its square&lt;/span&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;num&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;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num&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;The idea is to play the game with consistency. By following the code conventions of your team, your code won’t stick out like a sore thumb and your fellow developers will appreciate you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule#3 - Use descriptive variable and function names
&lt;/h2&gt;

&lt;p&gt;I know it’s stylish to name your variables X and Y.&lt;/p&gt;

&lt;p&gt;But trust me, even you won’t be able to understand their true meaning after a few months.&lt;/p&gt;

&lt;p&gt;It’s always safe to give descriptive names to variables. It might even be better for you in the long run.&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;// Descriptive function name&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculateSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Descriptive variable name&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&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="c1"&gt;// Iterate through the array of numbers&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;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="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="c1"&gt;// Add each number to the sum&lt;/span&gt;
    &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Return the sum&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&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;As you can see, even if the function’s code has a loop, the function and variable name makes the objective of the program absolutely clear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule#4 - Avoid Hard-Coding Values and Use Constants
&lt;/h2&gt;

&lt;p&gt;Say goodbye to hard coding and hello to constants!&lt;/p&gt;

&lt;p&gt;By using constants, you’ll be able to store values that you know won’t change throughout your program. This will help you avoid the headache of having to search and replace values throughout your code.&lt;/p&gt;

&lt;p&gt;Check out the below code.&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;TAX_RATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.07&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;subtotal&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;subtotal&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;subtotal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;TAX_RATE&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;If the tax rate changes in the future, you can easily update the value in one single place and have it reflected throughout your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule#5 - Keep functions small and focused
&lt;/h2&gt;

&lt;p&gt;When you are starting off as a developer, it’s always tempting to turn your functions into swiss knives. The feeling of explaining that your code can do so much stuff is so enticing.&lt;/p&gt;

&lt;p&gt;As you become a seasoned developer, this habit wears off. But sometimes it doesn’t.&lt;/p&gt;

&lt;p&gt;Trust me on this. Keep your functions small and your life will be happy. &lt;/p&gt;

&lt;p&gt;If you write small and focused functions that are easy to understand, chances are that you won’t be disturbed on your beach vacation if something goes wrong. &lt;/p&gt;

&lt;p&gt;See this 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;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;subtotal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;discount&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;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;subtotal&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;tax&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;discount&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;total&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;The above function is short and to the point. This makes it easy to understand and maintain. &lt;/p&gt;

&lt;h2&gt;
  
  
  Rule#6 - Use Appropriate Data Structures
&lt;/h2&gt;

&lt;p&gt;Are you always trying to fit a square peg in a round hole? If yes, it’s time to use appropriate data structures.&lt;/p&gt;

&lt;p&gt;Just like a carpenter has a variety of tools for different tasks, a developer should have a variety of data structures for different types of functionality.&lt;/p&gt;

&lt;p&gt;Here’s a cheat sheet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use arrays when you need to store a collection of items that have a specific order.&lt;/li&gt;
&lt;li&gt;Use lists when you need to store a collection of items that can change dynamically.&lt;/li&gt;
&lt;li&gt;Lastly, use maps if you need to store a collection of items that can be accessed by a key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out the below code that demonstrates the use of different data structures.&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;// Using an array to store a collection of items that have a specific order&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shoppingList&lt;/span&gt; &lt;span class="o"&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;milk&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;bread&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;eggs&lt;/span&gt;&lt;span class="dl"&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;shoppingList&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "bread"&lt;/span&gt;

&lt;span class="c1"&gt;// Using a list to store a collection of items that can change dynamically&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoList&lt;/span&gt; &lt;span class="o"&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;write code&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;debug&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;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;todoList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;deploy&lt;/span&gt;&lt;span class="dl"&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;todoList&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: ["write code", "debug", "test", "deploy"]&lt;/span&gt;

&lt;span class="c1"&gt;// Using a dictionary to store a collection of items that can be accessed by a key&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;phonebook&lt;/span&gt; &lt;span class="o"&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;John&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;555-555-5555&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;Mary&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;555-555-5556&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;Bob&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;555-555-5557&lt;/span&gt;&lt;span class="dl"&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;phonebook&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "555-555-5556"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using an appropriate data structure based on the requirement, you’ll find that your code is not only efficient but also easy to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule#7 - Use Version Control
&lt;/h2&gt;

&lt;p&gt;Just like your application is useless if it only runs on your machine, your code is useless if it isn’t committed to a central repository.&lt;/p&gt;

&lt;p&gt;Every developer should get used to version control. Don’t forget to commit your code regularly. If you are doing it, make sure others on your team also do it.&lt;/p&gt;

&lt;p&gt;All it takes is a few commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// Initialize a new Git repository
&lt;span class="nv"&gt;$ &lt;/span&gt;git init

// Add all files to the repository
&lt;span class="nv"&gt;$ &lt;/span&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;

// Commit the changes with a message
&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Initial commit"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A good version control tool allows developers to track changes, collaborate with others and easily revert to previous versions in case of any issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule# 8 - Automate Repetitive Tasks
&lt;/h2&gt;

&lt;p&gt;Don’t be like a hamster on a wheel, constantly running in circles and doing the same boring tasks over and over again.&lt;/p&gt;

&lt;p&gt;You should use tools and scripts to automate repetitive tasks in your code. This will not only save time but also make your code more reliable and efficient.&lt;/p&gt;

&lt;p&gt;Check out this code example of a simple testing automation script.&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;testCases&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;expectedOutput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;expectedOutput&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;expectedOutput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;testCases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;testCase&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;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;addNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;testCase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&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;testCase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;testCase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;expectedOutput&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Test failed: expected &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;testCase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;expectedOutput&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; but got &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;output&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="k"&gt;else&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Test passed&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also automate the building process for compiling your code and creating a final package.&lt;/p&gt;

&lt;p&gt;See the below example where we automate a build process using a tool like Gulp.&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;// Automating a build process using a tool like Grunt or Gulp&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gulp&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;gulp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;gulp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;build&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&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="c1"&gt;// Build process logic&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;gulp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;default&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;build&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;h2&gt;
  
  
  Rule#9 - Keep your code up-to-date
&lt;/h2&gt;

&lt;p&gt;Don’t be a dinosaur. You’re only going to go extinct.&lt;/p&gt;

&lt;p&gt;Keep your code up-to-date. Update your application dependencies whenever possible.&lt;/p&gt;

&lt;p&gt;For example, if you’re working in the Node ecosystem, keep the NPM up-to-date and upgrade your development environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// Keep dependencies up-to-date using package manager
&lt;span class="nv"&gt;$ &lt;/span&gt;npm update

// Keep the development environment up-to-date
&lt;span class="nv"&gt;$ &lt;/span&gt;nvm &lt;span class="nb"&gt;install &lt;/span&gt;12.16.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  That’s it!
&lt;/h2&gt;

&lt;p&gt;Well, there you have it. 9 rules to help you write clean and efficient code.&lt;/p&gt;

&lt;p&gt;Of course, these are not the only things that matter. But by following them, you’ll be able to start on the path to writing code that not only works well but also appears pleasing for others to read, understand and maintain.&lt;/p&gt;

&lt;p&gt;And if you have any other rules, do mention them in the comments section below.&lt;/p&gt;

&lt;p&gt;You can also connect with me on other platforms:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://progressivecoder.substack.com/"&gt;My Regular Newsletter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/ProgressiveCod2"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/saurabh-dashora-b3601173/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/channel/UCcD92x9BQLvvm-8jtsg12kw"&gt;Youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Build a Node.js API Proxy to supercharge your backend</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Thu, 26 Jan 2023 09:28:20 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/build-a-nodejs-api-proxy-to-supercharge-your-backend-42f3</link>
      <guid>https://dev.to/dashsaurabh/build-a-nodejs-api-proxy-to-supercharge-your-backend-42f3</guid>
      <description>&lt;p&gt;Have you ever wondered who's the middleman between you and that fancy API you're using?&lt;/p&gt;

&lt;p&gt;Meet the API proxy. The undercover agent of the tech landscape!&lt;/p&gt;

&lt;p&gt;Just like how your best friend proxied your attendance call during that boring college lecture, &lt;strong&gt;an API proxy acts on behalf of something else in the digital realm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And it's not just for the FBI. Even API developers use it to sneakily redirect incoming requests to the real server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0so99z2lfiuvfvyw5f1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0so99z2lfiuvfvyw5f1.png" alt="api-proxy-concept" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Want to learn how to build a Node.js API Proxy on your own? Keep reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Need for an API Proxy
&lt;/h2&gt;

&lt;p&gt;Several reasons justify the use of an API proxy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maybe the real &lt;strong&gt;API server is unpredictable&lt;/strong&gt;. A proxy can act as a trusty stable hand and provide a more stable interface for your clients.&lt;/li&gt;
&lt;li&gt;Probably the current &lt;strong&gt;API server is like a temporary rental car&lt;/strong&gt;. You have plans for upgrading in the future, but you want to shield the API clients from future changes. You can use an API proxy to make the transition smoother.&lt;/li&gt;
&lt;li&gt;Or maybe, &lt;strong&gt;the response from the API server is like a bad translation&lt;/strong&gt;. A proxy can act as a translator and convert the API response to something that the clients can understand or consume.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All in all, an API Proxy makes your actual backend API more scalable in the long run.&lt;/p&gt;

&lt;p&gt;Bottom line, an API Proxy is like a swiss-army knife. You never know when you might need one. But when you do, you would be happy to have it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Project Setup
&lt;/h2&gt;

&lt;p&gt;First, you need to initialize the project by executing the below command in a project directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate a basic package.json file with meta-data information about the project such as name, version, author and scripts.&lt;/p&gt;

&lt;p&gt;Next, install a couple of packages for developing the Node.js API proxy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install --save express http-proxy-middleware
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;express&lt;/code&gt; is a minimalistic web framework you can use to build API endpoints.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http-proxy-middleware&lt;/code&gt; is a third-party Node.js package that has all the tools to create an API proxy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the package installation, define a start command for the project within the &lt;code&gt;package.json&lt;/code&gt; file. You can use this command to start the application.&lt;/p&gt;

&lt;p&gt;Your project’s &lt;code&gt;package.json&lt;/code&gt; should look similar to the below example.&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;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"express-proxy-demo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Demo Application for Proxy Implementation in Node.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&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;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node index.js"&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;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Saurabh Dashora"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&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;"express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.18.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"http-proxy-middleware"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.0.6"&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;h2&gt;
  
  
  Creating the API Proxy using Node.js
&lt;/h2&gt;

&lt;p&gt;The project setup is done. Now, it’s time to build the API Proxy.&lt;/p&gt;

&lt;p&gt;You are going to create an application that acts like a proxy and redirects incoming requests to an API that’s hosted somewhere else. Think of it like a game of telephone but with requests.&lt;/p&gt;

&lt;p&gt;For the sake of this tutorial, I’m going to use some fake APIs hosted at the wonderful website &lt;strong&gt;JSONPlaceholder&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Check out the below illustration for a visual perspective.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwzaud5j4rb1u3j1g21l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwzaud5j4rb1u3j1g21l.png" alt="nodejs-api-proxy" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s the index.js file where all the magic happens.&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="s1"&gt;express&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="nx"&gt;createProxyMiddleware&lt;/span&gt; &lt;span class="p"&gt;}&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;http-proxy-middleware&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&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;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&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;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;localhost&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;API_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://jsonplaceholder.typicode.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/status&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is a proxy service&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;proxyOptions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;API_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;changeOrigin&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;pathRewrite&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="s2"&gt;`^/api/posts`&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/posts&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;proxy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createProxyMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;proxyOptions&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="s1"&gt;/api/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;proxy&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;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Proxy Started at &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s understand each step in the above program:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: The first segment of the code contains the import statements for &lt;code&gt;express&lt;/code&gt; and &lt;code&gt;http-proxy-middleware&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: The next statement creates an application instance using the call to &lt;code&gt;express()&lt;/code&gt; function. This is followed by declaring a few important constants such as PORT, HOST and API_URL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Implement an endpoint &lt;code&gt;/status&lt;/code&gt; to describe the role of the application. This endpoint has nothing to do with proxying requests. It simply provides a way to test our application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Next, we declare an object &lt;code&gt;proxyOptions&lt;/code&gt;. This is a configuration object for our API proxy. It contains a few important properties&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;target&lt;/code&gt; - It defines the target host where you want to proxy requests. In our case, this is the &lt;a href="https://jsonplaceholder.typicode.com" rel="noopener noreferrer"&gt;https://jsonplaceholder.typicode.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;changeOrigin&lt;/code&gt; - This is set to true since we are proxying to a different origin.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pathRewrite&lt;/code&gt; - This is a very important property where you define the rules for rewriting the path. For example, the expression &lt;code&gt;[^/api/posts]: '/posts'&lt;/code&gt; routes all incoming requests directed at URL &lt;code&gt;/api/posts&lt;/code&gt; to just &lt;code&gt;/posts&lt;/code&gt;. In other words, this will remove the &lt;code&gt;/api&lt;/code&gt; prefix from the path.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: After declaring the configuration object, create the proxy object by calling &lt;code&gt;createProxyMiddleware()&lt;/code&gt; function with the &lt;code&gt;proxyOptions&lt;/code&gt; object as input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: Next, create a request handler for the path &lt;code&gt;/api/posts&lt;/code&gt; and pass the proxy object as handler for the incoming request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7&lt;/strong&gt;: At the very end, start the Node.js API Proxy server to listen on the port and host that were already declared earlier.&lt;/p&gt;

&lt;p&gt;You can start the application using the command &lt;code&gt;npm run start&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="o"&gt;&amp;gt;&lt;/span&gt; express-proxy-demo@1.0.0 start
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; node index.js

&lt;span class="o"&gt;[&lt;/span&gt;HPM] Proxy created: /  -&amp;gt; &amp;lt;https://jsonplaceholder.typicode.com&amp;gt;
&lt;span class="o"&gt;[&lt;/span&gt;HPM] Proxy rewrite rule created: &lt;span class="s2"&gt;"^/api/posts"&lt;/span&gt; ~&amp;gt; &lt;span class="s2"&gt;"/posts"&lt;/span&gt;
Proxy Started at localhost:3000
Messages about the proxy setup indicate that the proxy is configured properly.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you visit the URL &lt;code&gt;http://localhost:3000/api/posts/1&lt;/code&gt; in the browser, you will get the response from the JSONPlaceholder APIs as below:&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;"userId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sunt aut facere repellat provident occaecati excepturi optio reprehenderit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"quia et suscipit&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;nsuscipit recusandae consequuntur expedita et cum&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;nreprehenderit molestiae ut ut quas totam&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;nnostrum rerum est autem sunt rem eveniet architecto"&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;Basically, the Node.js API Proxy is doing its job by proxying requests to the mock APIs hosted by JSONPlaceholder.&lt;/p&gt;

&lt;h2&gt;
  
  
  That’s it
&lt;/h2&gt;

&lt;p&gt;Congratulations! You've just built a basic version of a Node.js API proxy.&lt;/p&gt;

&lt;p&gt;But don't stop there. Just like a birdhouse, you can always make it bigger and better based on specific requirements.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;http-proxy-middleware&lt;/code&gt; is like the toolbox for building a Node.js API proxy server and you can extend it in many ways using path matching and so on.&lt;/p&gt;

&lt;p&gt;The code for this demo is available on &lt;a href="https://github.com/dashsaurabh/nodejs-express-proxy-demo" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you found the post useful, consider sharing it with friends and colleagues. And if you have any questions, feel free to ask in the comments section below.&lt;/p&gt;

&lt;p&gt;In case you want to know more about proxies, you can also head over to this tutorial on building a &lt;a href="https://progressivecoder.com/nodejs-forward-proxy-tutorial/" rel="noopener noreferrer"&gt;Node.js forward proxy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can also connect with me on other platforms:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://progressivecoder.substack.com/" rel="noopener noreferrer"&gt;My Regular Newsletter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/ProgressiveCod2" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/saurabh-dashora-b3601173/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/channel/UCcD92x9BQLvvm-8jtsg12kw" rel="noopener noreferrer"&gt;Youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>community</category>
      <category>devrel</category>
      <category>newproject</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Don't Unleash Zalgo in your Node.js Application!</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Thu, 12 Jan 2023 11:57:12 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/dont-unleash-zalgo-in-your-nodejs-application-3akm</link>
      <guid>https://dev.to/dashsaurabh/dont-unleash-zalgo-in-your-nodejs-application-3akm</guid>
      <description>&lt;p&gt;&lt;strong&gt;Don't Unleash Zalgo in your Node.js application&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;That's my sincere advice.&lt;/p&gt;

&lt;p&gt;And I believe that you can easily avoid the traps of Zalgo while building your next API. But for that, you need to understand how it looks like.&lt;/p&gt;

&lt;p&gt;In the real world, the term Zalgo comes from a meme. It happens to be an Internet legend about an ominous entity believed to cause insanity, death and destruction of the world. Zalgo is often associated with scrambled text on web pages and photos of people whose eyes and mouths have been covered in black.&lt;/p&gt;

&lt;p&gt;In the matrix-world of software development, Zalgo is a piece of code that can destroy your application. &lt;/p&gt;

&lt;p&gt;And you won't even know what's going on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fukc3gq86qb94k8atgbmd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fukc3gq86qb94k8atgbmd.png" alt="meme-on-zalgo" width="525" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The First Signs of Zalgo - Confused Code
&lt;/h2&gt;

&lt;p&gt;Callbacks make it possible for JavaScript to handle concurrency despite being single-threaded (technically).&lt;/p&gt;

&lt;p&gt;This is what essentially drives &lt;a href="https://progressivecoder.com/nodejs-reactor-pattern-event-loop/" rel="noopener noreferrer"&gt;concurrency in Node.js&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;You can build callbacks that behave &lt;strong&gt;synchronously&lt;/strong&gt; or &lt;strong&gt;asynchronously&lt;/strong&gt;. Typically, you choose only one approach.&lt;/p&gt;

&lt;p&gt;The problem shows up when you end up choosing both - &lt;strong&gt;sync&lt;/strong&gt; and &lt;strong&gt;async&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In other words, what if you develop a function that runs synchronously in certain conditions and asynchronously in some other conditions?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's an unpredictable function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unpredictable functions and the APIs built using them unleash Zalgo in your code&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;Here's an 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;let&lt;/span&gt; &lt;span class="nx"&gt;cache&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;getStringLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Despite the innocent appearance, the &lt;code&gt;getStringLength()&lt;/code&gt; function is evil. &lt;/p&gt;

&lt;p&gt;Though it is used to simply calculate the length of a string, it has two faces.&lt;/p&gt;

&lt;p&gt;If the string and its length are available in the &lt;code&gt;cache&lt;/code&gt; object, the function behaves synchronously by returning the data right away from the cache.&lt;/p&gt;

&lt;p&gt;Otherwise, it calculates the length of the string and stores the result in the &lt;code&gt;cache&lt;/code&gt; before triggering the callback. The calculation happens asynchronously using a &lt;code&gt;setTimeout()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Do note that the use of &lt;code&gt;setTimeout()&lt;/code&gt; is to force an asynchronous behaviour. You can replace it with any other asynchronous activity such as reading a file or making an API call. The idea is to demonstrate that a function can have different behaviour in different situations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“But how does it unleash Zalgo?” you may ask.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us write some more logic to actually use this unpredictable function. Check it out below:&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;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;milliseconds&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;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;milliseconds&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cache&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;getStringLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;determineStringLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;listeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="nf"&gt;getStringLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;listeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;onDataReady&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;listeners&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="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;testLogic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;text1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;determineStringLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onDataReady&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="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="s2"&gt;Text1 Length of string: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2000&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;text2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;determineStringLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onDataReady&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="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="s2"&gt;Text2 Length of string: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Pay special attention to the &lt;code&gt;determineStringLength()&lt;/code&gt; function. It is a sort of wrapper around the &lt;code&gt;getStringLength()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;determineStringLength()&lt;/code&gt; function creates a new object that acts as a notifier for the string length calculation. When the string length is calculated by the &lt;code&gt;getStringLength()&lt;/code&gt; function, the listener functions registered within &lt;code&gt;determineStringLength()&lt;/code&gt; get invoked.&lt;/p&gt;

&lt;p&gt;To test this concept, you have the &lt;code&gt;testLogic()&lt;/code&gt; function at the very end end. &lt;/p&gt;

&lt;p&gt;The test function calls &lt;code&gt;determineStringLength()&lt;/code&gt; function twice for the same input string “hello”. Between the two calls, we pause the execution for a couple of seconds using the &lt;code&gt;sleep()&lt;/code&gt; function. This is just to introduce a bit of time lag between the two calls.&lt;/p&gt;

&lt;p&gt;Running the program provides the below result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Text1 Length of string: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only one statement is printed. The callback for the second operation never got invoked.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;text1&lt;/code&gt;, the &lt;code&gt;getStringLength()&lt;/code&gt; function behaves asynchronously since the data is &lt;strong&gt;not&lt;/strong&gt; available in the cache. Therefore, the listener got registered and the output was printed.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;text2&lt;/code&gt;, none of this happens. It gets created in an event loop cycle that already has the data available in the cache. Therefore, &lt;code&gt;getStringLength()&lt;/code&gt; behaves synchronously and the callback that's passed to it gets called immediately. This in turn calls all the registered listeners synchronously. However, registration of new listener happens later and hence it is never invoked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The root of this problem is the unpredictable nature of &lt;code&gt;getStringLength()&lt;/code&gt; function. Instead of providing &lt;strong&gt;consistency&lt;/strong&gt;, it increases the &lt;strong&gt;unpredictability&lt;/strong&gt; of our program.&lt;/p&gt;

&lt;p&gt;Such bugs can turn out to be extremely complicated to identify and reproduce in a real application. More often, they cause nasty bugs and &lt;strong&gt;unleash Zalgo in our application&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, how can we avoid all of this mess?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Deferred Execution
&lt;/h2&gt;

&lt;p&gt;It might appear tricky but preventing such situations can be quite simple. &lt;strong&gt;Just make sure the functions you are writing behave consistently in terms of synchronous vs asynchronous behaviour&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, check the below code:&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;getStringLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextTick&lt;/span&gt;&lt;span class="p"&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="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="c1"&gt;//callback(cache[text])&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of directly triggering the callback, you can wrap it inside the &lt;code&gt;process.nextTick()&lt;/code&gt;. This &lt;strong&gt;defers&lt;/strong&gt; the execution of the function until the beginning of the next &lt;a href="https://progressivecoder.com/a-deeper-look-at-nodejs-event-loop-phases/" rel="noopener noreferrer"&gt;event loop phase&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Subtle reasons can cause nasty bugs. Unleashing Zalgo is one of them and hence, an interesting name was given to this situation.&lt;/p&gt;

&lt;p&gt;As I mentioned in the beginning, the term was first used by &lt;strong&gt;Isaac Z. Schlueter&lt;/strong&gt; who was also inspired by a post on Havoc’s Blog. Below are the links to those blogs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.izs.me/2013/08/designing-apis-for-asynchrony/" rel="noopener noreferrer"&gt;Designing APIs for Asynchrony&lt;/a&gt;&lt;br&gt;
&lt;a href="https://blog.ometer.com/2011/07/24/callbacks-synchronous-and-asynchronous/" rel="noopener noreferrer"&gt;Callbacks, synchronous and asynchronous&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check out those posts to get more background. I hope the example in this post was useful in understanding the issue on a more practical level.&lt;/p&gt;

&lt;p&gt;If you liked this post, consider sharing it with friends and colleagues. You can also connect with me on other platforms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://progressivecoder.substack.com/" rel="noopener noreferrer"&gt;My Regular Newsletter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/ProgressiveCod2" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/saurabh-dashora-b3601173/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/channel/UCcD92x9BQLvvm-8jtsg12kw" rel="noopener noreferrer"&gt;Youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Building a Node.js Webserver with HTTP Module is super easy!</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Thu, 05 Jan 2023 06:13:36 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/building-a-nodejs-webserver-with-http-module-is-super-easy-3mj2</link>
      <guid>https://dev.to/dashsaurabh/building-a-nodejs-webserver-with-http-module-is-super-easy-3mj2</guid>
      <description>&lt;p&gt;&lt;strong&gt;What if your car breaks down on a highway and you have no clue what the mechanic is saying about the problem?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the same as working with your Node.js webserver using frameworks such as Express. You know how to build applications. But you don't know how those applications work.&lt;/p&gt;

&lt;p&gt;And that's usually fine until you have a need to leverage the platform more efficiently.&lt;/p&gt;

&lt;p&gt;There are many easy ways to build a webserver by using frameworks such as Express. However, it is also &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt; that hard to build a basic &lt;strong&gt;Node.js Webserver&lt;/strong&gt; with the &lt;strong&gt;core HTTP module&lt;/strong&gt; and nothing else. &lt;/p&gt;

&lt;p&gt;Even if you might not use it to build real applications, knowing how it's done has benefits if you want to become a real Node.js practitioner. Just like being an automobile enthusiast instead of a simple driver.&lt;/p&gt;

&lt;p&gt;Surely, you don't want to be just a driver!&lt;/p&gt;

&lt;p&gt;All right - so now that you are &lt;em&gt;hopefully&lt;/em&gt; convinced, let's build an extremely simple webserver in Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Node.js Basic Webserver
&lt;/h2&gt;

&lt;p&gt;Create a file named &lt;code&gt;index.mjs&lt;/code&gt; in a project directory and paste the below code into the file.&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt; &lt;span class="p"&gt;}&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;http&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;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is listening to http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;port&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's all the code that you need to create a Node.js webserver.&lt;/p&gt;

&lt;p&gt;But what are you doing over here?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, you import &lt;code&gt;createServer()&lt;/code&gt; function from the &lt;code&gt;http&lt;/code&gt; module. This is a &lt;strong&gt;core module&lt;/strong&gt; that ships with the Node.js installation.&lt;/li&gt;
&lt;li&gt;Next, you call the imported &lt;code&gt;createServer()&lt;/code&gt; function to get a &lt;code&gt;server&lt;/code&gt; object. You store the object in a constant named &lt;code&gt;server&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;server&lt;/code&gt; object has a method named &lt;code&gt;listen()&lt;/code&gt; that takes a couple of arguments. One is the &lt;strong&gt;port number&lt;/strong&gt; and the second is a &lt;strong&gt;callback function&lt;/strong&gt; that prints a message when the server is ready to handle requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To start the server, you can execute the command &lt;code&gt;node index.mjs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Of course, you can say that this server is practically useless. And you won't be exaggerating.&lt;/p&gt;

&lt;p&gt;When you visit &lt;code&gt;http://localhost:8080&lt;/code&gt;, nothing happens. If you are lucky, you might get a timeout.&lt;/p&gt;

&lt;p&gt;Even though the server is up and running, it does not know what to do with an incoming request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling HTTP requests
&lt;/h2&gt;

&lt;p&gt;So, how can you make the Node.js webserver somewhat useful?&lt;/p&gt;

&lt;p&gt;You need to give it the ability to handle requests. &lt;/p&gt;

&lt;p&gt;Check out the below code:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt; &lt;span class="p"&gt;}&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;http&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;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&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="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;text/plain; charset=utf-8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; World&lt;/span&gt;&lt;span class="se"&gt;\n&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;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is listening to http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;port&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;createServer()&lt;/code&gt; function now takes a callback with two input objects - &lt;code&gt;request&lt;/code&gt; and &lt;code&gt;response&lt;/code&gt;. The job of this callback is to handle every incoming request.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;response&lt;/code&gt; object helps us manipulate the output. You can use the &lt;code&gt;writeHead()&lt;/code&gt; method to set the &lt;strong&gt;response status code&lt;/strong&gt; and &lt;strong&gt;response headers&lt;/strong&gt;. For example, you can set the &lt;code&gt;content-type&lt;/code&gt; to &lt;code&gt;text/plain&lt;/code&gt; and &lt;code&gt;charset&lt;/code&gt; to &lt;code&gt;utf-8&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To set the actual response, you can use the &lt;code&gt;response.write()&lt;/code&gt; method to set the response string. You can also have multiple &lt;code&gt;response.write()&lt;/code&gt; statements one after the other.&lt;/p&gt;

&lt;p&gt;Once the response is written, you need to tell the webserver to close the stream. This is done using the &lt;code&gt;response.end()&lt;/code&gt; method. The &lt;code&gt;response.end()&lt;/code&gt; method can also take some part of the response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But who uses a webserver to send some text response?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Of course, there might be a requirement but it does &lt;strong&gt;not&lt;/strong&gt; seem like the best use of a webserver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How about sending some HTML as a response?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sending HTML Response
&lt;/h2&gt;

&lt;p&gt;Check out the below code:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt; &lt;span class="p"&gt;}&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;http&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;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&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="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;text/html; charset=utf-8&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;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;!DOCTYPE html&amp;gt;
        &amp;lt;html&amp;gt;
            &amp;lt;head&amp;gt;
                &amp;lt;meta charset="utf-8"&amp;gt;
                &amp;lt;title&amp;gt;Node.js Webserver Demo&amp;lt;/title&amp;gt;
            &amp;lt;/head&amp;gt;
            &amp;lt;body&amp;gt;
                &amp;lt;h1 style="color: orange"&amp;gt;Hello World&amp;lt;/h1&amp;gt;
            &amp;lt;/body&amp;gt;
        &amp;lt;/html&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is listening to http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;port&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Despite the size of the code, there are only minor modifications.&lt;/p&gt;

&lt;p&gt;One is setting the &lt;code&gt;content-type&lt;/code&gt; value to &lt;code&gt;text/html&lt;/code&gt;. The second is declaring a constant named &lt;code&gt;body&lt;/code&gt; with some HTML content and sending it as part of &lt;code&gt;response.end()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;If you access &lt;code&gt;http://localhost:8080&lt;/code&gt; now, you should see the HTML page displayed in the browser.&lt;/p&gt;

&lt;p&gt;At this point, you might feel somewhat satisfied with the capabilities of the &lt;strong&gt;Node.js webserver&lt;/strong&gt;. But the nature of the content is still pretty static.&lt;/p&gt;

&lt;p&gt;Typical web applications consist of dynamic web pages. Output depends on the request made by the user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can you make your Node.js webserver generate dynamic responses?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sending Dynamic Response
&lt;/h2&gt;

&lt;p&gt;The changes are pretty straightforward. See below:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt; &lt;span class="p"&gt;}&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;http&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;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&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="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;text/html; charset=utf-8&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;url&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;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:8080&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;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;!DOCTYPE html&amp;gt;
        &amp;lt;html&amp;gt;
            &amp;lt;head&amp;gt;
                &amp;lt;meta charset="utf-8"&amp;gt;
                &amp;lt;title&amp;gt;Node.js Webserver Demo&amp;lt;/title&amp;gt;
            &amp;lt;/head&amp;gt;
            &amp;lt;body&amp;gt;
                &amp;lt;h1 style="color: orange"&amp;gt;Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&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;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/h1&amp;gt;
            &amp;lt;/body&amp;gt;
        &amp;lt;/html&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&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="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is listening to http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;port&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The major change in the above snippet is that you are now reading the URL requested by the client using the &lt;code&gt;request.url&lt;/code&gt; property. &lt;/p&gt;

&lt;p&gt;For example, if the user enters the URL &lt;code&gt;http://localhost:8080/?name=adamsmith&lt;/code&gt; in the address bar of the browser, the &lt;code&gt;request.url&lt;/code&gt; will contain the value &lt;code&gt;/?name=adamsmith&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To display the name in the HTML response, I recommend using the &lt;strong&gt;WHATWG URL API&lt;/strong&gt; to create a URL instance. The URL API helps you parse URLs and break them into individual components. It will produce an object that represents the requested URL with all of the components.&lt;/p&gt;

&lt;p&gt;Within the HTML string, you can extract the query parameter &lt;code&gt;name&lt;/code&gt; from the URL object's &lt;code&gt;searchParams&lt;/code&gt; property using the expression &lt;code&gt;url.searchParams.get('name')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you access &lt;code&gt;http://localhost:8080/?name=adamsmith&lt;/code&gt; after making the above change, you will see &lt;strong&gt;Hello adamsmith&lt;/strong&gt; displayed in the browser window. &lt;/p&gt;

&lt;p&gt;This is a &lt;strong&gt;dynamic HTML output&lt;/strong&gt; that depends on what you pass in the request's query parameters.&lt;/p&gt;




&lt;p&gt;There is no doubt using a framework makes life easy for developers. You ultimately write &lt;strong&gt;less code&lt;/strong&gt; and focus more on business logic. Also, you write &lt;strong&gt;less boilerplate code&lt;/strong&gt; to handle basic things when using a framework.&lt;/p&gt;

&lt;p&gt;But knowing how things work under the hood of any framework is also important. Learning how to create a Node.js webserver with just the &lt;strong&gt;core HTTP module&lt;/strong&gt; gives you important lessons on how the frameworks work internally.&lt;/p&gt;

&lt;p&gt;If you enjoyed this post or found it helpful, do share it with friends and colleagues. Also, here are some other articles you might find useful:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://progressivecoder.com/nodejs-reactor-pattern-event-loop/"&gt;How does Node.js Reactor Pattern work?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://progressivecoder.com/a-deeper-look-at-nodejs-event-loop-phases/"&gt;Various Phases of the Node.js Event Loop&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also connect with me on other platforms:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://progressivecoder.substack.com/"&gt;My Regular Newsletter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/ProgressiveCod2"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/saurabh-dashora-b3601173/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/channel/UCcD92x9BQLvvm-8jtsg12kw"&gt;Youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>I misunderstood Scalability in a Distributed System</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Wed, 28 Dec 2022 08:53:48 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/i-misunderstood-scalability-in-a-distributed-system-j4d</link>
      <guid>https://dev.to/dashsaurabh/i-misunderstood-scalability-in-a-distributed-system-j4d</guid>
      <description>&lt;p&gt;"Is the application scalable?" the manager asked. It was almost halfway through the weekly system roadmap meeting. I thought we were done with all the tech parts and would soon move to business-related questions.&lt;/p&gt;

&lt;p&gt;The architect of my team cleared his throat and answered, "It can easily handle double the usual load."&lt;/p&gt;

&lt;p&gt;The manager gave a long look as if he didn't believe the answer. But, in the absence of any better points, he continued with further updates. I implicitly believed that our system is indeed scalable. After all, the architect had announced it in clear terms.&lt;/p&gt;

&lt;p&gt;A year down the line, I learned the truth. One fine day, the system crashed due to excessive load.&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%2Fb0d28quisrgu5k60r54r.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%2Fb0d28quisrgu5k60r54r.png" alt="office meme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The bank had launched a new loan product for the holiday season. The business strategy paid off and there was a surge in demand. Since our application provided the core data to make the credit decision, it was swamped with 5 times more incoming requests from several other applications.&lt;/p&gt;

&lt;p&gt;We added more CPUs to handle the load on our service. The same strategy had worked the last couple of times. However, this time it didn't.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our system was scalable until it no longer was.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Upon analysis, we found that the issue wasn't entirely in our application code. Our application was made up of multiple services that connected with the bank's central database. The connection to the database was made using a connection pool.&lt;/p&gt;

&lt;p&gt;Our fault was that we were careless about the number of open connections. Turns out, the database administrations were not.&lt;/p&gt;

&lt;p&gt;The DBAs had a hard block on the number of open connections for our group of applications. When the incoming requests to our application surged, we ended up acquiring even more connections until we hit the wall. No matter how many additional CPUs we added to increase the application capacity, it didn't make any difference.&lt;/p&gt;

&lt;p&gt;To add insult to injury, the DBAs refused to increase the permissible number of connections. The only option left was a costly change to our application to handle connections properly. For all practical purposes, &lt;strong&gt;our system was no longer scalable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Of course, it was our mistake. We should have accounted for the limited number of connections and used them wisely. Nevertheless, the problem was not unfixable. But the change wasn't cost-effective.&lt;/p&gt;

&lt;p&gt;Though the situation was ultimately handled, it piqued my interest in the subject. We thought the system was scalable. But it wasn't. At least not until we hit the connection limit.&lt;/p&gt;

&lt;p&gt;It was clear we were wrong about the &lt;strong&gt;true meaning of scalability&lt;/strong&gt;. I decided to dig deeper into the whole situation. My initial motivation was to avoid getting into the same situation as my team's architect&lt;/p&gt;

&lt;h2&gt;
  
  
  The Usual Definition of System Scalability
&lt;/h2&gt;

&lt;p&gt;While studying scalability for distributed systems, the most common definition I have come across is as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability is the ability of the system to handle an increased workload.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How does this definition apply in real life?&lt;/p&gt;

&lt;p&gt;Developers of an actively used system monitor its workload levels. They create processes for predicting when the performance of the system will become unsatisfactory. The goal is to increase the system's capacity before the danger level is reached.&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%2Fujrh434k5wtgn8u5d6fj.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%2Fujrh434k5wtgn8u5d6fj.png" alt="throughput vs load"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this approach to scalability, the primary focus is to determine an &lt;strong&gt;interval of demand&lt;/strong&gt; within which the system will perform at an acceptable level. If this interval is sufficiently large, the system is considered scalable.&lt;/p&gt;

&lt;p&gt;In my view, this definition is pretty limited. The focus of this definition is always on the workload.&lt;/p&gt;

&lt;p&gt;The main concern of this definition is that a system is scalable if it continues to perform adequately as the workload grows. No attention is given to &lt;strong&gt;how a system will have to be modified&lt;/strong&gt; to keep the performance at the same level.&lt;/p&gt;

&lt;p&gt;As we found in our case, our system could have continued performing at a good level. But the changes required on the database side were not acceptable. Neither was changing the code cost-effective.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The common scalability definition pays no attention to the approach of capacity increase or its overall impact on the system&lt;/strong&gt;. We don't ask important questions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Will doubling the number of processors allow the system to handle double the workload?&lt;/li&gt;
&lt;li&gt;Is the overhead of coordinating work between processors going to be prohibitively high?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In defence of the definition, it is the most common definition used by system designers and developers. We hear it all the time when someone says that a &lt;strong&gt;system is scalable&lt;/strong&gt; and it can handle double the workload with no problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scalability by Extension
&lt;/h2&gt;

&lt;p&gt;In my quest to understand scalability, I came across another definition of scalability that often gets neglected. However, I have come to believe that it may be far more important in practical situations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability is the ability to handle an increased workload by repeatedly applying a cost-effective strategy for extending a system's capacity.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Straight away, the focus shifts towards the &lt;strong&gt;&lt;em&gt;strategy&lt;/em&gt;&lt;/strong&gt; for adding capacity.&lt;/p&gt;

&lt;p&gt;We are no longer interested in a one-time capacity increase to improve scalability. &lt;strong&gt;We are interested in a strategy for adding capacity and the number of times the strategy can be applied cost-effectively&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This line of thought always makes me think about world-class tennis players like &lt;strong&gt;Rafael Nadal&lt;/strong&gt; or &lt;strong&gt;Roger Federer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Over the years, how many times have players like Nadal or Federer adapted their playing style to the changing demands of the game?&lt;/p&gt;

&lt;p&gt;Many times, I reckon.&lt;/p&gt;

&lt;p&gt;Each change they made prolonged their careers and made them more successful.&lt;/p&gt;

&lt;p&gt;However, making a change is not easy. Many players rise to the top and fizzle out because they could not adapt. Even the best players are bound to face difficulties in trying to adapt. The easier it is for a player to make changes, the higher the probability for that player to maintain his dominance in the game.&lt;/p&gt;

&lt;p&gt;The same goes for scaling a system.&lt;/p&gt;

&lt;p&gt;When we consider the second definition of scalability, we start considering other types of arguments about our system.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If we add more processors to increase capacity, what is the method for coordinating work between the added processors?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Will the coordination method take more processing cycles?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If yes, the full benefit of adding capacity won't be realized. Hence, adding processors beyond a certain point may not be a cost-effective approach to improving scalability.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Focusing on the repeated application of a scaling strategy makes us more cognitive of our choices.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, replacing an &lt;strong&gt;O(n^2)&lt;/strong&gt; with an &lt;strong&gt;O(nlogn)&lt;/strong&gt; algorithm makes it possible to process a larger workload in the same amount of time. In other words, replacing an algorithm with a more efficient one improves the system's scalability.&lt;/p&gt;

&lt;p&gt;But can we use this approach repeatedly?&lt;/p&gt;

&lt;p&gt;I don't think so.&lt;/p&gt;

&lt;p&gt;Once we have the most efficient algorithm in place, the algorithmic replacement strategy stops being viable. You cannot keep applying the strategy again and again to improve the system's scalability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is a System Scalable?
&lt;/h2&gt;

&lt;p&gt;Armed with the two definitions of scalability, I was finally able to make sense of the fundamental question. This was the same question that popped up in our meeting and was answered inadequately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is a system scalable?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To answer the question, we end up labelling a system as scalable or not. In my view, this is an oversimplification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does it mean for a system to be scalable?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most systems scale in some sense. However, no system is infinitely scalable.&lt;/p&gt;

&lt;p&gt;So how should we approach scalability?&lt;/p&gt;

&lt;h3&gt;
  
  
  Theoretical Approach
&lt;/h3&gt;

&lt;p&gt;Rather than labelling systems, it is more fruitful to &lt;strong&gt;compare the scalability of two different systems&lt;/strong&gt;. Consider the below graph that shows the response vs demand curves for two hypothetical systems A and B.&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%2F6bms03189wjqtzs2mk0p.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%2F6bms03189wjqtzs2mk0p.png" alt="Comparing Scalability of two systems"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For any given demand level, the response is &lt;strong&gt;worse&lt;/strong&gt; for System A as compared to System B. If there is a maximum tolerable value for the response, System A will reach it earlier than System B.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;System B is more scalable than System A.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Of course, if both lines continue to rise at the same monotonic rate, they will eventually reach a point where the demand for resources exceeds their availability. At that point, the response times will become unsatisfactory for both systems. Though the points may be different for A and B, they signify the limit of a system's scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember - no system is infinitely scalable.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Systems do not follow a monotonic rate of increase in response vs demand metric. The curve looks more like the below example.&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%2Fxdsw03jxfh1df0tckakz.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%2Fxdsw03jxfh1df0tckakz.png" alt="response vs demand curve"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The hypothetical system tolerates demand increase quite well until it hits a significant design limitation. This limitation forms a knee-like shape in the response demand curve. After crossing a particular demand level, the response metric simply goes out of hand.&lt;/p&gt;

&lt;p&gt;The goal of designers is to keep the knee of the curve as far to the right as possible. Once a system reaches closer to the knee, it is no longer scalable. Any increase in demand will push it down the hill.&lt;/p&gt;

&lt;h3&gt;
  
  
  Practical Approach
&lt;/h3&gt;

&lt;p&gt;The theoretical aspects of scalability were genuine eye-openers for me. But I still thought something was missing.&lt;/p&gt;

&lt;p&gt;I couldn't completely relate the above graphs to our system. We were able to scale our system successfully during the initial days whenever there was a general increase in load. Things went south only when we hit the database limit.&lt;/p&gt;

&lt;p&gt;This behaviour does not fit into either of the two theoretical models to depict scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to classify this behaviour?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most real system exhibit a more hybrid behaviour. The below graph shows this:&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%2Fodg51ahmda6kyyqmqhmo.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%2Fodg51ahmda6kyyqmqhmo.png" alt="real scalability of a system"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a system is running in the bottom green zone, it is responding well. As it begins to linger in the middle zone (shown in yellow), the response starts to get &lt;strong&gt;unacceptable&lt;/strong&gt;. When it finally crosses into the top zone (shown in red), the system becomes &lt;strong&gt;unusable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In my opinion, this is a more accurate representation of the second definition of scalability.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On the first couple of occasions, the system designers were able to take quick action to keep the response within acceptable limits. It was probably a matter of adding additional memory.&lt;/li&gt;
&lt;li&gt;On the third occasion, the designers needed more time and effort to bring the response metric down to acceptable levels. The changes needed to improve the response were probably more complicated.&lt;/li&gt;
&lt;li&gt;In the last case, the system reached a point where no reasonable solution was possible to keep the response times from becoming unusable. &lt;strong&gt;The system was no longer scalable using the cost-effective techniques that worked the initial few times.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The above turn of events also depicts the situation of our system. Even though we were able to keep our system scalable for some time, it ultimately reached a point beyond which any cost-effective solution was not possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, is the system scalable?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer is - &lt;strong&gt;it depends&lt;/strong&gt;! And not in a cop-out sort of way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability is more of a moving target that evolves rather than a fixed state.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the system owner can afford to continue pouring money to meet the higher demand levels, the system is scalable up to a certain point. Beyond this point, no cost-effective action can alleviate the response metric issues.&lt;/p&gt;

&lt;p&gt;If the system owner has no money to purchase additional resources at the first very hiccup, &lt;strong&gt;the system is not scalable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whatever the case, &lt;strong&gt;no system is infinitely scalable&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concluding Thoughts
&lt;/h2&gt;

&lt;p&gt;My journey to understand the true meaning of scalability led to some important conclusions.&lt;/p&gt;

&lt;p&gt;In my view, you cannot simply slap a label of scalability on a system and call it a day.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;All systems are scalable to some extent&lt;/strong&gt;. But it does not mean that you can continue scaling them indefinitely in a cost-effective manner.&lt;/p&gt;

&lt;p&gt;An ideal scalability situation requires a nuanced approach to system design. Only focusing on adding resources to improve scalability is a trap. You also need to consider the cost-effectiveness of the additional resource.&lt;/p&gt;

&lt;p&gt;With this post, my goal was to bring this perspective to the discussion.&lt;/p&gt;

&lt;p&gt;Do share your thoughts in the comments section below.&lt;/p&gt;




&lt;p&gt;If you enjoyed this article or found it helpful, consider sharing it with your friends and colleagues.&lt;/p&gt;

&lt;p&gt;You can also connect with me on other platforms:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/ProgressiveCod2" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/saurabh-dashora-b3601173/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/channel/UCcD92x9BQLvvm-8jtsg12kw" rel="noopener noreferrer"&gt;Youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>distributedsystems</category>
      <category>architecture</category>
      <category>programming</category>
      <category>microservices</category>
    </item>
    <item>
      <title>How to use Svelte Keyed Each Block?</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Tue, 18 Jan 2022 12:28:08 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/how-to-use-svelte-keyed-each-block-pmf</link>
      <guid>https://dev.to/dashsaurabh/how-to-use-svelte-keyed-each-block-pmf</guid>
      <description>&lt;p&gt;In this post, we learn how to use &lt;strong&gt;Svelte Keyed Each Block&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the normal  &lt;a href="https://progressivewebninja.com/how-to-use-svelte-each-block-to-render-arrays/"&gt;Svelte Each Block&lt;/a&gt;, we use the &lt;code&gt;each&lt;/code&gt; keyword to render an array or list. While it works perfectly when we have static lists, there is a serious issue when we delete items from the list. We are going to first look at the issue and then come up with the solution to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  1 – Svelte Each Block Issue
&lt;/h3&gt;

&lt;p&gt;To demonstrate the issue, we will take the example of displaying books and their respective authors.&lt;/p&gt;

&lt;p&gt;Below is our &lt;code&gt;App&lt;/code&gt; component.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;App.svelte&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Book&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;./Book.svelte&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
        &lt;span class="na"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eye of the World&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="na"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Way of Kings&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="na"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Name of the Wind&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}]&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;Fantasy&lt;/span&gt; &lt;span class="nx"&gt;Library&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&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;idx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Book&lt;/span&gt; 
    &lt;span class="nx"&gt;bookName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sr"&gt;/each&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&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;Remove&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;Book&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above piece of code, we have the &lt;code&gt;books&lt;/code&gt; array with three hard-coded books. We use the &lt;code&gt;each&lt;/code&gt; block to render the &lt;code&gt;books&lt;/code&gt; using the &lt;code&gt;Book&lt;/code&gt; component. Finally, we also have a button to remove the first book from the &lt;code&gt;each&lt;/code&gt; block. When the user clicks the button, we invoke the &lt;code&gt;handleClick()&lt;/code&gt; function that slices the &lt;code&gt;books&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;Below is the code for the &lt;code&gt;Book&lt;/code&gt; component.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;Book.svelte&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;authors&lt;/span&gt; &lt;span class="o"&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;Eye of the World&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;Robert Jordan&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;The Way of Kings&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;Brandon Sanderson&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;The Name of the Wind&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;Patrick Rothfuss&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;export&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bookName&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;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;authors&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Book&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="nx"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Author: {author}&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this component, we have the &lt;strong&gt;book&lt;/strong&gt; to &lt;strong&gt;author&lt;/strong&gt; map. Based on the input &lt;code&gt;bookName&lt;/code&gt;, we basically determine the name of the author.&lt;/p&gt;

&lt;p&gt;If we run the app now and then click the button to remove first book once, we will see the below output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DfcT4Tvm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://progressivewebninja.com/wp-content/uploads/2022/01/svelte-each-block-issue.png.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DfcT4Tvm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://progressivewebninja.com/wp-content/uploads/2022/01/svelte-each-block-issue.png.webp" alt="svelte-each-block-issue.png" width="880" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the &lt;code&gt;bookName&lt;/code&gt; and the &lt;code&gt;authorName&lt;/code&gt; does not match. Though the first book was correctly deleted, the &lt;code&gt;authorName&lt;/code&gt; got mixed up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Why does this happen?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The reason is that when we modify the underlying array of an &lt;code&gt;each&lt;/code&gt; block, Svelte adds and removes items from the end of the block. It also updates any values that have changed. This is the default behaviour.&lt;/p&gt;

&lt;p&gt;Due to this, the last item is removed and the items above it are updated. However, the &lt;code&gt;author&lt;/code&gt; value is not updated because it is the local constant of the &lt;code&gt;Book&lt;/code&gt; component. Its value was fixed at the time of component initialization. This leads to the data mismatch.&lt;/p&gt;

&lt;h3&gt;
  
  
  2 – The Svelte Keyed Each Block
&lt;/h3&gt;

&lt;p&gt;To get around this issue, we need to use &lt;strong&gt;keyed each block&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;See below example:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;App.svelte&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Book&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;./Book.svelte&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eye of the World&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Way of Kings&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Name of the Wind&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}]&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;Fantasy&lt;/span&gt; &lt;span class="nx"&gt;Library&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&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;idx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Book&lt;/span&gt; 
    &lt;span class="nx"&gt;bookName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bookName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sr"&gt;/each&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&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;Remove&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;Book&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, here we introduce an &lt;code&gt;id&lt;/code&gt; field in the &lt;code&gt;books&lt;/code&gt; array. Each book has a unique id. In a real life application, this &lt;code&gt;id&lt;/code&gt; could also be the database id. The point is that it should be unique for every item.&lt;/p&gt;

&lt;p&gt;We also utilise this &lt;code&gt;id&lt;/code&gt; in the each block as below.&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="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, the role of this &lt;code&gt;id&lt;/code&gt; field is to help Svelte figure out which DOM node should be changed when the &lt;code&gt;each&lt;/code&gt; block updates. After this modification, if we run the application and click the button, we will see proper data as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wEtOwunZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642385377572/W9TmhfFzW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wEtOwunZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642385377572/W9TmhfFzW.png" alt="svelte-keyed-each-block-fix.png" width="880" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The book name and author name match as expected. The first book was successfully removed. However, Svelte only removed the appropriate DOM node and other nodes were left as they were before the update.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;With this, we have learnt how to use &lt;strong&gt;Svelte Keyed Each Block&lt;/strong&gt;. This is extremely useful when we have a requirement to update elements within our each blocks.&lt;/p&gt;

&lt;p&gt;Hope this little post was useful. If you have any comments or queries about this post, please feel free to write them in the comments section below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>svelte</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to create and run your first Svelte Rollup Application?</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Mon, 17 Jan 2022 06:35:02 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/how-to-create-and-run-your-first-svelte-rollup-application-1k1i</link>
      <guid>https://dev.to/dashsaurabh/how-to-create-and-run-your-first-svelte-rollup-application-1k1i</guid>
      <description>&lt;p&gt;In this post, we will learn how to run our first Svelte Rollup Application.&lt;/p&gt;

&lt;p&gt;To make things easy to understand, we will create a small Svelte app from scratch and write a basic &lt;strong&gt;Rollup&lt;/strong&gt; configuration to build and run the application.&lt;/p&gt;

&lt;p&gt;But before that, let's go through some basics about Svelte.&lt;/p&gt;

&lt;h3&gt;
  
  
  1 – What is Svelte?
&lt;/h3&gt;

&lt;p&gt;Svelte is a new front-end &lt;strong&gt;library&lt;/strong&gt;. It is known for its blazing fast speed. Unlike other UI libraries such as React, &lt;strong&gt;Svelte is more of a compiler&lt;/strong&gt;. While traditional frameworks like  &lt;a href="https://progressivewebninja.com/create-react-app-the-fastest-way-to-start-a-new-react-project/"&gt;React&lt;/a&gt;  or Vue do much of the work in the browser, Svelte shifts that work into the compile step.&lt;/p&gt;

&lt;p&gt;Basically, browsers do not directly execute Svelte code. When we write Svelte code, &lt;strong&gt;we need to have a build step that translates that code into standard HTML, CSS &amp;amp; Javascript&lt;/strong&gt;. This makes it faster while also transferring less code to the browser when the user makes a request.&lt;/p&gt;

&lt;h3&gt;
  
  
  2 – Setting up a new Svelte Project
&lt;/h3&gt;

&lt;p&gt;To demo our &lt;strong&gt;Svelte Rollup application&lt;/strong&gt;, we will create an extremely simple Svelte application. To achieve it, we have to first setup our project.&lt;/p&gt;

&lt;p&gt;First, we will create a new directory to place our application files.&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;my-first-svelte-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we navigate into the directory and then initialize a new &lt;strong&gt;npm&lt;/strong&gt; project.&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;my-first-svelte-app
&lt;span class="nv"&gt;$ &lt;/span&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above commands will generate a &lt;code&gt;package.json&lt;/code&gt; file for our application. Now we can install the required packages. As a first step, we install the &lt;code&gt;svelte&lt;/code&gt; package.&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="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;svelte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the installation is completed, we are ready to write some code for our demo application.&lt;/p&gt;

&lt;h3&gt;
  
  
  3 – Creating the Svelte App
&lt;/h3&gt;

&lt;p&gt;By convention, we put all our code into the &lt;code&gt;src&lt;/code&gt; directory. You can use any editor of your choice or the terminal to create the same. Inside the &lt;code&gt;src&lt;/code&gt; directory, we create two files &lt;code&gt;App.svelte&lt;/code&gt; and &lt;code&gt;main.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;App.svelte&lt;/code&gt; file will contain the actual component code as below:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;App.svelte&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&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;script&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello {name}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A very simple component indeed! We only have a paragraph that displays a hello message. The &lt;code&gt;name&lt;/code&gt; within curly braces is a prop for the &lt;code&gt;App&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;Next, we configure the &lt;code&gt;main.js&lt;/code&gt; file as below:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;main.js&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&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;App&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;./App.svelte&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;app&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;App&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;target&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="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;props&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="s1"&gt;Web Ninja&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we create a new instance of the &lt;code&gt;App&lt;/code&gt; component. We also specify that we want to load the component in the document &lt;code&gt;body&lt;/code&gt;. Lastly, we supply the &lt;code&gt;name&lt;/code&gt; as part of the props object.&lt;/p&gt;

&lt;p&gt;With this our Svelte application is ready. Time to delve into &lt;strong&gt;how to actually run the application&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  4 – Installing Svelte Rollup Packages
&lt;/h3&gt;

&lt;p&gt;As discussed earlier, we will be using Rollup to build and run our application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;But what exactly is Rollup?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rollup&lt;/strong&gt; is basically a &lt;strong&gt;module bundler for Javascript applications&lt;/strong&gt;. It takes our application code and bundles it into files that browsers can easily parse. In other words, &lt;strong&gt;Rollup converts our &lt;code&gt;.svelte&lt;/code&gt; files into browser-understandable HTML, CSS and Javascript&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To get started with Rollup, we first need to install the required package.&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="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; rollup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that this is a &lt;strong&gt;development&lt;/strong&gt; dependency.&lt;/p&gt;

&lt;p&gt;Next, we also need to install two more Rollup development dependencies. See below:&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="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; @rollup/plugin-node-resolve rollup-plugin-svelte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;@rollup/plugin-node-resolve&lt;/code&gt; package &lt;strong&gt;helps Rollup resolve any third party plugins&lt;/strong&gt;. The second dependency &lt;code&gt;rollup-plugin-svelte&lt;/code&gt; is a &lt;strong&gt;third party plugin&lt;/strong&gt; that helps Rollup understand how to handle Svelte applications.&lt;/p&gt;

&lt;p&gt;As with &lt;code&gt;rollup&lt;/code&gt;, both of these dependencies are also development only.&lt;/p&gt;

&lt;h3&gt;
  
  
  5 – Rollup Configuration File
&lt;/h3&gt;

&lt;p&gt;Rollup also requires a configuration file to understand what it needs to do with our source code.&lt;/p&gt;

&lt;p&gt;We will start by creating a very simple Rollup configuration file. It will simply bundle our Svelte application file into the &lt;code&gt;public/build&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;See below:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;rollup.config.js&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&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;svelte&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;rollup-plugin-svelte&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;resolve&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;@rollup/plugin-node-resolve&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;input&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/main.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;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;public/build/bundle.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;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;iife&lt;/span&gt;&lt;span class="dl"&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="s1"&gt;app&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;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nx"&gt;svelte&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;include&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/**/*.svelte&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;resolve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;browser&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="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;Let us understand what we are doing here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In line 1 &amp;amp; 2, we have the necessary imports.&lt;/li&gt;
&lt;li&gt;In line 5, we specify the path to the input file. This is the &lt;code&gt;main.js&lt;/code&gt; file of our application where instantiate our component.&lt;/li&gt;
&lt;li&gt;In line 6, we specify the &lt;code&gt;output&lt;/code&gt;. Basically, this is the destination of our built artifacts. We also specify the format as &lt;code&gt;iife&lt;/code&gt; (&lt;strong&gt;immediately invoked function express&lt;/strong&gt;). Along with this, we specify the name of the variable to which we assign the &lt;code&gt;iife&lt;/code&gt; output value. In this case, we name the variable as &lt;code&gt;app&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Lastly, from line 11 onwards, we specify the other configuration items. Basically, we tell Rollup the path to our &lt;code&gt;.svelte&lt;/code&gt; files. Also, we indicate that we are building our application to work on a browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6 – Adding Rollup to NPM Script
&lt;/h3&gt;

&lt;p&gt;The next step is to add Rollup to our NPM scripts so that we can build and run our application.&lt;/p&gt;

&lt;p&gt;To run Rollup, we add a new script to our &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&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;"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;"rollup -c -w"&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;The &lt;code&gt;-c&lt;/code&gt; flag specifies that we want Rollup to use a configuration file. By default, it checks for a file named &lt;code&gt;rollup.config.js&lt;/code&gt;. We already created the file in the previous section.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-w&lt;/code&gt; flag instructs Rollup to watch our files for changes. In case we make changes to our source code, Rollup will automatically rebuilds the artifacts.&lt;/p&gt;

&lt;p&gt;We can now run &lt;code&gt;npm run dev&lt;/code&gt; to build our Svelte application. The built files will be placed inside &lt;code&gt;/public/build&lt;/code&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  7 – Serving the Content
&lt;/h3&gt;

&lt;p&gt;Though we have successfully compiled our application, we still need a way to serve it. Therefore, we create an &lt;code&gt;index.html&lt;/code&gt; file in the public folder.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;index.html&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&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;html&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;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&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;My First Svelte App&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"build/bundle.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&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;&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;p&gt;Important thing to note is the &lt;code&gt;script&lt;/code&gt; tag where we specify the location to our newly built &lt;code&gt;bundle.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;To serve the application, we will install a simple web server available with the &lt;code&gt;sirv-cli&lt;/code&gt; package.&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="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; sirv-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we add a script to run the application.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&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;"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;"rollup -c -w"&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"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sirv public"&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;We can now start our application by running the command &lt;code&gt;npm run start&lt;/code&gt;. The application will be accessible on &lt;code&gt;http://localhost:5000&lt;/code&gt;. You should see the &lt;em&gt;hello message&lt;/em&gt; in the browser window.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;With this, we have successfully learnt how to create and run a &lt;strong&gt;Svelte Rollup application&lt;/strong&gt;. We created a new &lt;strong&gt;Svelte&lt;/strong&gt; app from scratch and also wrote a basic Rollup configuration.&lt;/p&gt;

&lt;p&gt;The code for this post is available on  &lt;a href="https://github.com/dashsaurabh/svelte-rollup-demo"&gt;Github&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;In the next post, we will look at how to configure  &lt;a href="https://progressivewebninja.com/how-to-configure-svelte-rollup-server-hot-reload/"&gt;Svelte Rollup Hot-Reload feature&lt;/a&gt; so that we can improve the development experience.&lt;/p&gt;

&lt;p&gt;If you have any comments or queries, please feel free to mention them in the comments section below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>svelte</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to implement NestJS Passport Authentication using Local Strategy?</title>
      <dc:creator>Saurabh Dashora</dc:creator>
      <pubDate>Sat, 08 Jan 2022 03:35:29 +0000</pubDate>
      <link>https://dev.to/dashsaurabh/how-to-implement-nestjs-passport-authentication-using-local-strategy-3p24</link>
      <guid>https://dev.to/dashsaurabh/how-to-implement-nestjs-passport-authentication-using-local-strategy-3p24</guid>
      <description>&lt;p&gt;In this post, we will learn how to implement &lt;strong&gt;NestJS Passport Authentication&lt;/strong&gt; using the passport local strategy. Authentication is a key aspect of any production-level application. While there are many ways to handle authentication, one of the more popular ways is to use Passport.&lt;/p&gt;

&lt;p&gt;If you are new to NestJS in general, you can start by going through this post on &lt;a href="https://progressivecoder.com/nestjs-basics-core-fundamentals-learn-nestjs-series-part-1/" rel="noopener noreferrer"&gt;NestJS Basics&lt;/a&gt;. However, if you are mainly interested in learning how to get started with authentication in NestJS, you can continue with this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  1 – What is Passport?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Passport is a popular NodeJS library&lt;/strong&gt;. It is used for authentication purpose and comes along with several strategies. These strategies basically implement different authentication mechanism. For our example, we will use the local strategy. This strategy makes use of username and password.&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%2Fxhxk8sf7e6bkhvztbd8u.jpeg" 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%2Fxhxk8sf7e6bkhvztbd8u.jpeg" alt="Authentication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Passport executes a series of steps as below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authenticating a user by its credentials. These credentials can be username/password, JWT tokens or any other identity token.&lt;/li&gt;
&lt;li&gt;Manage authentication state by issuing a JWT token.&lt;/li&gt;
&lt;li&gt;Attach information about the user to the Request object.&lt;/li&gt;
&lt;li&gt;To make things easy, the &lt;code&gt;@nestjs/passport&lt;/code&gt; module wraps the entire passport usage pattern into familiar NestJS constructs. This makes it extremely easy to implement NestJS Passport Authentication.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2 – Install Packages
&lt;/h2&gt;

&lt;p&gt;To enable the local strategy, we need to install the required packages as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install --save @nestjs/passport passport passport-local
$ npm install --save-dev @types/passport-local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Passport provides the passport-local strategy that implements the username/password authentication. Also, we install the &lt;code&gt;@nestjs/passport&lt;/code&gt; and the passport packages. Finally, the &lt;code&gt;@types/passport-local&lt;/code&gt; package helps us with Typescript types during development.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note - For any strategy, we need to always install the @nestjs/passport and the passport packages. The third package depends on the strategy we are trying to implement. For the local strategy, we install passport-local. Similarly, for the JWT strategy, we use the &lt;code&gt;passport-jwt&lt;/code&gt; strategy. Even the types package depends on the strategy.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With the installation done, let’s now begin building our application.&lt;/p&gt;

&lt;h2&gt;
  
  
  3 – Creating the Modules
&lt;/h2&gt;

&lt;p&gt;As a first step, let’s divide our application into appropriate modules. We will creating an &lt;strong&gt;auth-module&lt;/strong&gt; and &lt;strong&gt;users-module&lt;/strong&gt;. Basically, the &lt;strong&gt;auth-module&lt;/strong&gt; will contain the logic for authenticating the user. On the other hand, the &lt;strong&gt;users-module&lt;/strong&gt; will contain the user information.&lt;/p&gt;

&lt;p&gt;With this view, we will create both the modules and their associated services as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nest g module auth
$ nest g service auth
$ nest g module users
$ nest g service users

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

&lt;/div&gt;



&lt;p&gt;Next step is to build our passport strategy. Configuring a strategy has two typical steps as below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First is a set of options that are specific to a particular strategy. For example, the JWT strategy needs a secret to sign the tokens.&lt;/li&gt;
&lt;li&gt;Second is a verify callback. In other words, we tell passport how to verify whether a user is valid. Passport expects this callback to return the full user object if the validation is successful. Also, it should return null if validation fails. Failure can mean that the user does not exist. For a strategy like &lt;strong&gt;passport-local&lt;/strong&gt;, it can also mean that the password is invalid.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The NestJS passport package helps with the above two steps by providing helper classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  4 – Implementing the User Service
&lt;/h2&gt;

&lt;p&gt;Let’s first create the user service. See below example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@nestjs/common';

export type User = any;

@Injectable()
export class UsersService {

    private readonly users = [
        {
            userId: 1,
            username: 'John Marston',
            password: 'rdr1',
        },
        {
            userId: 2,
            username: 'Arthur Morgan',
            password: 'rdr2',
        },
    ]

    async findOne(username: string): Promise&amp;lt;User | undefined&amp;gt; {
        return this.users.find(user =&amp;gt; user.username === username)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For our demo example, our user service will simply contain a list of hardcoded valid users. However, for a real application, we would probably create a user database and fetch the users from the appropriate table using &lt;a href="https://progressivecoder.com/how-to-configure-nestjs-typeorm-integration-mysql/" rel="noopener noreferrer"&gt;NestJS TypeORM&lt;/a&gt; or &lt;a href="https://progressivecoder.com/how-to-setup-a-nestjs-sequelize-postgresql-integration/" rel="noopener noreferrer"&gt;NestJS Sequelize libraries&lt;/a&gt;. You can also using MongoDB using the &lt;a href="https://progressivecoder.com/how-to-build-a-nestjs-mongodb-crud-application-using-mongoose/" rel="noopener noreferrer"&gt;NestJS Mongoose&lt;/a&gt; library.&lt;/p&gt;

&lt;p&gt;In the above example, the &lt;code&gt;findOne()&lt;/code&gt; method simply fetches the user from the users array. Also, we need to update the exports array for the &lt;strong&gt;UsersModule&lt;/strong&gt; as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { UsersService } from './users.service';

@Module({
  providers: [UsersService],
  exports: [UsersService]
})
export class UsersModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5 – Implementing the Auth Service
&lt;/h2&gt;

&lt;p&gt;As the next step, we implement the Auth Service in the Auth Module.&lt;/p&gt;

&lt;p&gt;See below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@nestjs/common';
import { UsersService } from 'src/users/users.service';

@Injectable()
export class AuthService {
    constructor(private usersService: UsersService){}

    async validateUser(username: string, password: string): Promise&amp;lt;any&amp;gt; {
        const user = await this.usersService.findOne(username);

        if (user &amp;amp;&amp;amp; user.password === password) {
            const {password, ...result} = user
            return result
        }
        return null
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, the auth service has the job of retrieving the user and verifying the password. As you can see, we create the &lt;code&gt;validateUser()&lt;/code&gt; method. We fetch the user by using the user service and return the user object as output. However, we strip the password property out of the object before returning it.&lt;/p&gt;

&lt;p&gt;Finally, we update the &lt;strong&gt;AuthModule&lt;/strong&gt; to import the &lt;strong&gt;UsersModule&lt;/strong&gt;. Without this step, we will not be able to use the user service in the auth module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { UsersModule } from 'src/users/users.module';
import { AuthService } from './auth.service';

@Module({
  imports: [UsersModule],
  providers: [AuthService]
})
export class AuthModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6 – Implement Passport Local Strategy
&lt;/h2&gt;

&lt;p&gt;This is a key step in implementing the NestJS Passport Authentication.&lt;/p&gt;

&lt;p&gt;Basically, we need to implement the passport local strategy. To do so, we create a file named &lt;strong&gt;local.strategy.ts&lt;/strong&gt; in the auth folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable, UnauthorizedException } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { Strategy } from "passport-local";
import { AuthService } from "./auth.service";

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
    constructor(private authService: AuthService) {
        super()
    }

    async validate(username: string, password: string): Promise&amp;lt;any&amp;gt; {
        const user = await this.authService.validateUser(username, password);

        if (!user) {
            throw new UnauthorizedException();
        }

        return user;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, we create a class &lt;strong&gt;LocalStrategy&lt;/strong&gt; that extends the &lt;strong&gt;PassportStrategy&lt;/strong&gt; class. We also pass the attribute Strategy in the class definition. Note here that the &lt;strong&gt;Strategy&lt;/strong&gt; is imported from &lt;strong&gt;passport-local&lt;/strong&gt; and not passport package.&lt;/p&gt;

&lt;p&gt;In the constructor, we simply call the &lt;code&gt;super()&lt;/code&gt; method. The local strategy only expects the username and password fields and therefore, configuration options are not needed. However, if needed, we can pass extra properties while calling super().&lt;/p&gt;

&lt;p&gt;Next, we implement the &lt;code&gt;validate()&lt;/code&gt; method as part of the &lt;strong&gt;PassportStrategy&lt;/strong&gt; class. For every strategy, Passport will call the verify function. In NestJS, this function is implemented with the &lt;code&gt;validate()&lt;/code&gt; method. Based on the strategy, it expects some arguments. For example, in the local strategy, it expects the username and password attributes.&lt;/p&gt;

&lt;p&gt;From a logical point of view, this method is quite simple. It simply calls the &lt;code&gt;validateUser()&lt;/code&gt; method from the Auth service. If a valid user is found, it returns the same. Otherwise, it throws an exception. Most of the heavy work is done in the Auth Service.&lt;/p&gt;

&lt;p&gt;Lastly, we need to update the Auth Module as below to use the passport module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { UsersModule } from 'src/users/users.module';
import { AuthService } from './auth.service';
import { LocalStrategy } from './local.strategy';

@Module({
  imports: [UsersModule, PassportModule],
  providers: [AuthService, LocalStrategy]
})
export class AuthModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7 – Creating the Login Route
&lt;/h2&gt;

&lt;p&gt;Now, we can create the login route. Basically, this route will be used to authenticate a user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller, Post, Request, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Controller()
export class AppController {

  @UseGuards(AuthGuard('local'))
  @Post('login')
  async login(@Request() req) {
    return req.user;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we use the standard &lt;code&gt;@Controller()&lt;/code&gt; decorator. Also, we use &lt;code&gt;@Post()&lt;/code&gt; for the &lt;code&gt;login()&lt;/code&gt; request handler. If you wish to know more about creating controllers, you can refer to our detailed post on &lt;a href="https://progressivecoder.com/creating-your-first-nestjs-controller-learn-nestjs-series-part-2/" rel="noopener noreferrer"&gt;NestJS Controllers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Important thing to note here is the @UseGuards(AuthGuard(‘local’)) decorator. Basically, the AuthGuard is a special guard provided by the @nestjs/passport package. This guard was provisioned when we extended the &lt;strong&gt;passport-local&lt;/strong&gt; strategy.&lt;/p&gt;

&lt;p&gt;This built-in guard invokes the Passport strategy and starts the entire process. In other words, based on the strategy in the &lt;strong&gt;AuthGuard&lt;/strong&gt; (in this case, local), this guard will retrieve the credentials, run the verify function and create the user property.&lt;/p&gt;

&lt;p&gt;The user can simply authenticate by using the &lt;code&gt;/login&lt;/code&gt; route as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl -X POST http://localhost:3000/auth/login -d '{"username": "John Marston", "password": "rdr1"}' -H "Content-Type: application/json"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case the user is valid, we receive the user object in response. Else, we receive the HTTP Status 401 or &lt;strong&gt;Unauthorized&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;With this, we have successfully learnt how to implement &lt;strong&gt;NestJS Passport Authentication using the passport local strategy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We started by installing the required packages and then building the modules for retrieving users and authenticating them. Lastly, we implemented the login route to trigger the passport authentication process.&lt;/p&gt;

&lt;p&gt;The code for this post is available on &lt;a href="https://github.com/dashsaurabh/nestjs-authentication-demo" rel="noopener noreferrer"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the next post we will be implementing the &lt;a href="https://progressivecoder.com/how-to-implement-nestjs-jwt-authentication-using-jwt-strategy/" rel="noopener noreferrer"&gt;NestJS Passport JWT Strategy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have any comments or queries, please feel free to mention them in the comments section below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>passport</category>
      <category>nestjs</category>
    </item>
  </channel>
</rss>
