<?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: Hal Friday</title>
    <description>The latest articles on DEV Community by Hal Friday (@halented).</description>
    <link>https://dev.to/halented</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%2F143198%2Fe347dd6a-9a71-4a5b-88dd-7891c2b6ee70.png</url>
      <title>DEV Community: Hal Friday</title>
      <link>https://dev.to/halented</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/halented"/>
    <language>en</language>
    <item>
      <title>In the Gut: Reaching a Better Understanding of Recursion</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Fri, 09 Feb 2024 22:32:06 +0000</pubDate>
      <link>https://dev.to/halented/in-the-gut-reaching-a-better-understanding-of-recursion-3bk4</link>
      <guid>https://dev.to/halented/in-the-gut-reaching-a-better-understanding-of-recursion-3bk4</guid>
      <description>&lt;p&gt;Often times people (me) get swung into the habit of solving recursion style algorithms in these neat little scenarios that you find on popular sites like LeetCode and HackerRank. And it will sometimes come in handy if you run into such a problem during a live coding interview, because the scenarios share a lot of qualities and you can carry over the specific skillset you learned on those sandbox sites into whatever IDE is being used for the interview. &lt;/p&gt;

&lt;p&gt;That's got a limited helpfulness, but I'm not as interested in learning a cleansed version of the skill that mostly makes sense in the vacuum of performing interview tricks — I want to learn recursion "in the gut," to the point of it occurring to me naturally for solution building in a real-world setting, on a production-level job. &lt;/p&gt;

&lt;p&gt;With that goal in mind, I set out to revisit recursion and develop and understanding of it that was more versatile and less pattern-based. I wanted to record some of my experience with exploring that here in this blog post. &lt;/p&gt;

&lt;p&gt;It makes sense to start where you are — in my case, a review of popular problems I've solved with recursion. I'd like to go over each of the following three and capture some conceptual, overarching information from each before moving forward into a more unknown, creative space and apply these concepts. &lt;/p&gt;

&lt;h2&gt;
  
  
  toThePowerOf
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Spoiler alert! Each section will show code solutions. If you'd like to try toThePowerOf yourself before reading, &lt;a href="https://www.geeksforgeeks.org/write-a-c-program-to-calculate-powxn/" rel="noopener noreferrer"&gt;check it out here.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the simplest recursion problems which can be used to ground one's understanding of the whole thing, is the &lt;strong&gt;toThePowerOf&lt;/strong&gt; function. Your function receives a base number (&lt;em&gt;b&lt;/em&gt;) and power (&lt;em&gt;p&lt;/em&gt;). It must return an integer, which is the result of taking &lt;em&gt;b&lt;/em&gt; to the power of &lt;em&gt;p&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;For me, the important concept which we can carry forward into our creative space from this problem is that recursion is easier to think about in an upside down way. By that I mean, the first thing you need to know about your recursive problem is: what is the end like? At what point does your algorithm know when to stop recurring? That's called the "base case" which I honestly think it's kind of a confusing name for it, because it's the case at the very tippy top of the recursion stack. In my opinion it should be called the peak case. Doesn't quite have the same ring to it, maybe crest case is better. &lt;/p&gt;

&lt;p&gt;Anyway. &lt;/p&gt;

&lt;p&gt;How can we determine the crest case for an algorithm which returns &lt;em&gt;b&lt;/em&gt; to the power of &lt;em&gt;p&lt;/em&gt;? The function should continue to recur until the power variable (&lt;em&gt;p&lt;/em&gt;) is down to 1. At that point, we know what can start climbing back down the stack which has been built, multiplying each result by the base variable (&lt;em&gt;b&lt;/em&gt;). It's difficult to understand this without looking at it, so take a peek at the code:&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;toThePowerOf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;b&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;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;toThePowerOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p&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="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;Taking it chunk by chunk, you have the crest case, when the power becomes one. Any number to the power of one is itself, so we just return that. After that, we start heading back down the stack — the return value of the most recent function call is multiplied by &lt;em&gt;b&lt;/em&gt;, and so on. Once the stack completes, it will spit out &lt;em&gt;b&lt;/em&gt; to the power of &lt;em&gt;p&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;To make sure the mental model is complete, let's take a look at this visualization of the function doing its work when given the parameters 2 and 3 as &lt;em&gt;b&lt;/em&gt; and &lt;em&gt;p&lt;/em&gt;, respectively.&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%2Flx1nl03jodj43mi3hgwq.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%2Flx1nl03jodj43mi3hgwq.png" alt="Call stack and description of the flow of calls for the toThePowerOf function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(You can look at this on Google Drawings directly if you would like, from &lt;a href="https://docs.google.com/drawings/d/1O7tW22kXFcV57PfBTuxK-drZQNlQWyi9pL9_jNlEpXU/edit" rel="noopener noreferrer"&gt;this link&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;As you can see, call #3 is at the top of the call stack, and it ends the recursion and completes first. It is the shortest running function in the bunch. Call #1 is at the bottom of the call stack, and when all is said and done, call #1 will be the one to return the correct answer. &lt;/p&gt;

&lt;p&gt;If that's not quite hitting, I recommend popping the function into &lt;a href="https://recursion.vercel.app/" rel="noopener noreferrer"&gt;this visualizer&lt;/a&gt; and watching it / stepping through the functions as they run. I will give a warning about that site, it isn't perfect and asking it to visualize denser recursion problems has produced bugs and/or incorrect results for me. But it works well for this powers one. &lt;/p&gt;

&lt;p&gt;This type of recursion problem is what I'd like to call linear. It steps up the call stack; it steps back down the call stack. This isn't the common use-case for recursion as seen in the real world, because such problems can also be solved in an iterative way without sacrificing much speed or space. You could just use a while-loop. &lt;/p&gt;

&lt;p&gt;A case where recursion is a little more useful might be..&lt;/p&gt;

&lt;h2&gt;
  
  
  nthFibonacciNumber
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Check out the problem &lt;a href="https://leetcode.com/problems/fibonacci-number/" rel="noopener noreferrer"&gt;here&lt;/a&gt; if you'd like to attempt it before seeing any solution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When visualized, the recursive solution for the &lt;strong&gt;nthFibonacciNumber&lt;/strong&gt; algorithm does not have one single line of calls, so it takes a more branched shape. The call flow steps all the way up the call stack, then on its way back down, it must make separate branches off of the main stack which it completes along the way. Calling &lt;strong&gt;nthFibonacciNumber(5)&lt;/strong&gt; has a result that looks something like 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%2Fpb1mg9aln39rkc0m9ihf.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%2Fpb1mg9aln39rkc0m9ihf.png" alt="visualized tree for nthFibonacciNumber algorithm given the parameter 5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Image generated on &lt;a href="https://recursion.vercel.app/" rel="noopener noreferrer"&gt;Recursion Tree Visualizer&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;While studying the algorithm this go around, the concept that I focused on retaining revolved around the use of returns. &lt;/p&gt;

&lt;p&gt;Recursive algorithms all have at least 2 options for returning:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The return that happens during the crest case; the return that ceases any further recursion, which should never change, and &lt;/li&gt;
&lt;li&gt;The return that receives the bulk of the work and is sent on down the line, which changes with each completed call&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using these carefully and understanding what is needed from each before beginning to write out a solution will make the actual programming part a lot easier. &lt;/p&gt;

&lt;p&gt;So, applying the first concept to the nthFibo problem, our crest case (and first return value) should be the point at which we stop adding recursions to the stack. Since the algorithm's argument is the number of times that we need to perform the fibo operation, then we will count backward from that to the end of the line and our crest case should be 1 and/or 0 -- it just needs to stop before anything negative happens.&lt;/p&gt;

&lt;p&gt;As for the second return value, it needs to be the result of the operations to actually find a Fib number: the last two Fib numbers added together. &lt;/p&gt;

&lt;p&gt;Here's what the whole thing looks like. &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;fib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&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="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;+&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&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;Structurally, you can really see the similarities between this one and the &lt;strong&gt;toThePowerOf&lt;/strong&gt; algorithm. The big difference is that Fib is calling itself twice at the end, instead of just once. This creates the branching pattern that you saw from the visualizer. Aside from the crest case, each instance of the call must resolve two other instances before it can complete its function and produce an actual value. As difficult as it may be to follow the function calls and their resolutions in one's head, the final line of the algorithm winds up looking pretty intuitive once it's on the page. &lt;/p&gt;

&lt;p&gt;With that we'll move on to an algorithm which has a lot more branching.&lt;/p&gt;

&lt;h2&gt;
  
  
  mergeSort
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Check out the problem &lt;a href="https://www.geeksforgeeks.org/problems/merge-sort/1?itm_source=geeksforgeeks&amp;amp;itm_medium=article&amp;amp;itm_campaign=bottom_sticky_on_article" rel="noopener noreferrer"&gt;here&lt;/a&gt; on GeeksForGeeks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this algorithm, the factors are less simplistic and the application is a little more involved. But the two concepts mentioned previously, understanding the crest case and conceptualizing possible return values, are still super useful to start out with. &lt;/p&gt;

&lt;p&gt;The third concept I'd like to anchor myself to and use in this solution and others is also a little more involved. This concept is the thoughtful headspace that one should be in when considering implementing recursion: what types patterns signify that recursion could be a useful solution? When is it powerful, and how can you look at a set of circumstances and transform that into something that fits the bill for a recursive function? &lt;/p&gt;

&lt;p&gt;With all three problems so far, recursion &lt;em&gt;can&lt;/em&gt; be used because one or two operations need to take place over and over again. But for the first two, an argument can be made that in terms of space and time complexity, it doesn't really give you anything to select a recursive solution over an iterative one (except bragging rights). &lt;/p&gt;

&lt;p&gt;With &lt;strong&gt;mergeSort&lt;/strong&gt;, a recursive solution makes a bit more sense. The algorithm has a lot of branches to resolve, and it's important that those branches be resolved in order. This type of data movement would be complicated and annoying to program an iterative approach, but it works pretty gracefully in a recursive approach. Take a look at the code and the tree of calls:&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;mergeSort&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&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;arr&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;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&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;leftArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mid&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;rightArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leftArr&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rightArr&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;merge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sorted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;right&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&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="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;right&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="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;sorted&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;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&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;sorted&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;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&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="nx"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;&lt;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%2F08xad0x2m7fjo8g9ltmj.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%2F08xad0x2m7fjo8g9ltmj.png" alt="Call tree visualization for mergeSort"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using mergeSort recursively like this still does have limited viability in real world programming, since this solution is &lt;em&gt;not&lt;/em&gt; ideal for large data sets like you might see in a production level database, but there are many other circumstances where it can be applied and actually satisfy the requirements in the most elegant way, as long as you have a smaller data set. &lt;/p&gt;

&lt;h2&gt;
  
  
  To the Real World
&lt;/h2&gt;

&lt;p&gt;With these three items in mind, one can start getting creative in the application space. I spoke with a mentor of mine recently with more than a decade of experience in the field, who told me that recursion is definitely a tool used in his regular engineering life, albeit sort of rarely. An example he gave fit the profile perfectly for the type of use case I had been imagining: he had recently made a solution to dynamically build URLs by recursing into an Angular object and collecting relevant breadcrumbs along the way. This is the ideal situation; the dataset is reasonably small and the flow of the data is relatively complex and follows a branching pattern.&lt;/p&gt;

&lt;p&gt;By framing the circumstance and visualizing it before beginning to program anything, one would be able to see recursion as a satisfying solution for situations like building a dynamic URL, and I'm sure many others. This post is mostly just a journal entry for my own adventures in becoming more comfortable with recursion, but I hope it's able to help anyone else who is in the position of wanting to "level up" in this particular category. Please feel free to leave a comment with any questions or thoughts, I'd love to chat about others' experiences!&lt;/p&gt;

</description>
      <category>recursion</category>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Are BigInt Math Calculations More Expensive Than Using Numbers in JavaScript?</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Mon, 29 Mar 2021 16:50:15 +0000</pubDate>
      <link>https://dev.to/halented/are-bigint-math-calculations-more-expensive-than-using-numbers-in-javascript-19of</link>
      <guid>https://dev.to/halented/are-bigint-math-calculations-more-expensive-than-using-numbers-in-javascript-19of</guid>
      <description>&lt;h1&gt;
  
  YES.
  
&lt;/h1&gt;

&lt;p&gt;Had to dig through quite a few articles to get a simple answer to this one, so I figured I'd put it right at the beginning. &lt;strong&gt;Yes, regular math calculations using &lt;code&gt;BigInt&lt;/code&gt; cost more for the computer to execute than if you were just using Numbers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a little more detail on that, please read on. &lt;/p&gt;

&lt;p&gt;JavaScript, like any programming language, is made up of a bunch of different types -- I went into a little depth on the topic of types in &lt;a href="https://dev.to/halented/javascript-stuff-i-thought-was-weird-which-just-turned-out-to-make-sense-after-all-fgo"&gt;this blog post over here&lt;/a&gt;. Two of those types are "BigInt" and "Number". So first off, we'll give a clear picture of both:&lt;/p&gt;

&lt;h3&gt;
  
  
  NUMBER
&lt;/h3&gt;

&lt;p&gt;Ol' fashioned integers. 0-10. Your grandad's math. Numbers are....&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive"&gt;Primitive&lt;/a&gt;&lt;/strong&gt; (meaning they &lt;em&gt;cannot be changed no matter what&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;64-bit&lt;/strong&gt; (meaning there is a size limit)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Imprecise&lt;/strong&gt; at a point (meaning if a calculation gets past 17 decimal points, JavaScript will stop math-ing and round the number)&lt;/li&gt;
&lt;li&gt;Is &lt;em&gt;always&lt;/em&gt; a float (meaning &lt;code&gt;17.0 === 17&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And just for fun, here are some examples of numbers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;17&lt;/li&gt;
&lt;li&gt;1234.123123123123&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NaN&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Infinity&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are those who think it's weird that &lt;code&gt;NaN&lt;/code&gt;, standing for Not-A-Number, is of type &lt;code&gt;Number&lt;/code&gt; -- but honestly, it provides more detail about itself as an error if it's still associated with &lt;code&gt;Number&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  BIGINT
&lt;/h3&gt;

&lt;p&gt;Large, hefty numbers. Could eat your grandad's numbers for breakfast. Here are some &lt;code&gt;BigInt&lt;/code&gt; facts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Also &lt;strong&gt;Primitive&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Represents anything beyond the 64-bit limit (which can be checked for using JavaScript's method &lt;code&gt;Number.MAX_SAFE_INTEGER&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Does not&lt;/em&gt; like to mix with numbers (&lt;code&gt;10 * BigInt(10)&lt;/code&gt; will result in a type error)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Does&lt;/em&gt; somewhat mix with &lt;code&gt;String&lt;/code&gt;s&lt;/li&gt;
&lt;li&gt;Puts a fancy little &lt;code&gt;n&lt;/code&gt; at the end of the number&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's helpful to play around with &lt;code&gt;BigInt&lt;/code&gt;s to get a feel for them. In whatever JavaScript environment you're used to, you can get your computer to print out some BigInts with the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;BigInt("32787835783895638958923489124891234")&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;note that the &lt;code&gt;BigInt&lt;/code&gt; method here will accept a string&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;code&gt;BigInt(5)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;1n&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  So, &lt;strong&gt;why&lt;/strong&gt; is &lt;code&gt;BigInt&lt;/code&gt; math more expensive than &lt;code&gt;Number&lt;/code&gt; math?
&lt;/h2&gt;

&lt;p&gt;Your intuition probably led you to this already, but there is the one obvious thing: &lt;code&gt;BigInt&lt;/code&gt;s are bigger. Not in the way that 100 is bigger than 10, but in the way that your computer receives and handles the &lt;code&gt;BigInt&lt;/code&gt; data type. The memory set aside for a &lt;code&gt;BigInt&lt;/code&gt; is &lt;em&gt;always&lt;/em&gt; larger than a regular &lt;code&gt;Number&lt;/code&gt;, regardless of the value of the &lt;code&gt;BigInt&lt;/code&gt; itself. So, if you have two variables:&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;optionA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;optionB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are 15, but optionB is clunkier. Thicker. &lt;code&gt;BigInt&lt;/code&gt;er. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A side note on equality:&lt;br&gt;
JavaScript does recognize that optionA and optionB are equal to 15, but a strict equality operator will return false because of their differing types. If you need to compare between &lt;code&gt;BigInt&lt;/code&gt;s and &lt;code&gt;Number&lt;/code&gt;s, you should use loose equality (&lt;code&gt;==&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The other reason &lt;code&gt;BigInt&lt;/code&gt; calculations take more time &amp;amp; space is that the instructions for handling these calculations are &lt;em&gt;in the software&lt;/em&gt; of JavaScript. When you do a Math calculation like &lt;code&gt;10*81&lt;/code&gt;, your computer uses its hardware, which is easily accessible and requires almost no searching or computational power. When you try something special like &lt;code&gt;10n * 81n&lt;/code&gt;, it has to go and find the &lt;code&gt;BigInt&lt;/code&gt; JavaScript functions, feed your input in, and calculate the result at a high level. You can read more about that from &lt;a href="https://www.tektutorialshub.com/typescript/typescript-bigint-vs-number/"&gt;this TekTutorialsHub&lt;/a&gt; post. &lt;/p&gt;

&lt;p&gt;You can prove this by benchmarking JavaScript methods using both types of math. Check out &lt;a href="https://replit.com/@halented/BigInt-vs-Number-benchmarking#index.js"&gt;this Repl.it&lt;/a&gt; and hit &lt;code&gt;Run&lt;/code&gt; to see it in action. &lt;/p&gt;

&lt;h2&gt;
  
  
  What's the practical use of knowing this?
&lt;/h2&gt;

&lt;p&gt;The only reason I went down this rabbit hole was for interview prep: practicing algorithms. There is an interesting LeetCode problem (&lt;a href="https://leetcode.com/problems/add-two-numbers/submissions/"&gt;found here&lt;/a&gt;) which asks you to add two linked lists together and return their sum as a linked list. After solving the initial prompt, my code was still failing because some of the test cases resulted in massive....some might say &lt;code&gt;BigInt&lt;/code&gt; numbers. &lt;/p&gt;

&lt;p&gt;To resolve this problem, my first instinct was, &lt;em&gt;"Well, what if we treat every single number like it's gonna be a &lt;code&gt;BigInt&lt;/code&gt;?"&lt;/em&gt; And the answer to that question is that the solution &lt;em&gt;will&lt;/em&gt; pass, but it will be slower than a...car that's slow...or...whatever, it'll make your algorithm super slow. So you really need to allow for and handle &lt;code&gt;BigInt&lt;/code&gt;s separately, giving them the attention they deserve as unique circumstances. &lt;/p&gt;

&lt;p&gt;Interview prep is a &lt;em&gt;huge&lt;/em&gt; practical reality for software engineers of all levels, but there are also real-world applications for &lt;code&gt;BigInt&lt;/code&gt;s, such as massive database management, precise math with large numbers for financial applications, high-accuracy timestamps, and anything else people do with fancy and elaborate math. Who knows, maybe rocket science. &lt;/p&gt;

&lt;p&gt;There is a lot more to the story with the &lt;code&gt;BigInt&lt;/code&gt; datatype, but hopefully this gave a useful and concise introduction. Have fun &amp;amp; feel free to drop a comment with any additions, questions, or thoughts!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Other Resources...&lt;br&gt;
&lt;a href="https://v8.dev/features/bigint"&gt;V8&lt;/a&gt;&lt;br&gt;
&lt;a href="https://betterprogramming.pub/the-downsides-of-bigints-in-javascript-6350fd807d"&gt;BetterProgramming.Pub&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"&gt;BigInts on MDN&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number"&gt;Numbers on MDN&lt;/a&gt;&lt;br&gt;
&lt;a href="https://levelup.gitconnected.com/how-to-check-if-a-number-is-a-bigint-in-javascript-6d658be6e89c"&gt;GitConnected&lt;/a&gt;&lt;br&gt;
&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Writing Your First React Test</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Wed, 17 Mar 2021 20:35:32 +0000</pubDate>
      <link>https://dev.to/halented/writing-your-first-react-test-jc8</link>
      <guid>https://dev.to/halented/writing-your-first-react-test-jc8</guid>
      <description>&lt;p&gt;Cover image unrelated -- nice to look at though, right? Discovery Park, Seattle, WA. &lt;/p&gt;

&lt;p&gt;This post will assume that the reader has a good understanding of the React basics. It will also involve some coding, which you are welcome to code along with. The repository for the starter code can be found &lt;a href="https://github.com/halented/testing-tutorial" rel="noopener noreferrer"&gt;HERE&lt;/a&gt;. To view the finished product with tests, use the same repository, but switch to the &lt;code&gt;with-tests&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;Before we make any changes, take a moment to poke around the code. It's fairly simple -- just two components &amp;amp; a little bit of state to swap the image between a dog and not-a-dog. &lt;/p&gt;

&lt;p&gt;Since this repository was created with &lt;a href="https://create-react-app.dev/docs/getting-started/" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt;, there are some testing tools which are already in the code that we don't have to build or add ourselves.&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%2Fj66ucvyxsq34w2nymv6x.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%2Fj66ucvyxsq34w2nymv6x.png" alt="Screenshot of file tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Firstly, there is a file called &lt;code&gt;setupTests.js&lt;/code&gt;, which contains the basic import for the &lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Jest&lt;/a&gt; test runner. This file can be edited for fancier, more complicated tests in the future, but we won't need to do much with it right now. &lt;br&gt;
Secondly, you'll see a file called &lt;code&gt;App.test.js&lt;/code&gt;. Now, I know this is crazy, but that's where we'll write the tests for the App component. The file extension matters, as when we input the command to run the tests (either &lt;code&gt;npm test&lt;/code&gt; or &lt;code&gt;yarn test&lt;/code&gt; depending on your package manager), anything with a &lt;code&gt;.test.js&lt;/code&gt; file extension will get read &amp;amp; executed.&lt;br&gt;
There is also some code which already lives inside of the &lt;code&gt;App.test.js&lt;/code&gt; file, which we can use to make some basic assumptions about how tests look. We might as well check it out.&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;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;screen&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="s1"&gt;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;renders learn react link&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;App&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;linkElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/learn react/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;linkElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&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 break down what we are looking at before changing it. &lt;/p&gt;

&lt;p&gt;I think the most confusing and, potentially most important part to recognize with this small snippet is that there are two different testing packages being used. The first package we are using is the &lt;a href="https://testing-library.com/docs/react-testing-library/intro/" rel="noopener noreferrer"&gt;React Testing Library&lt;/a&gt;. It's the more obvious package, because the import is right at the top, like normal. We are importing &lt;code&gt;render&lt;/code&gt;, which will allow us to access an instance of any component, and &lt;code&gt;screen&lt;/code&gt;, which will allow us to make queries off the DOM similarly to vanilla JavaScript, after said component is rendered.&lt;/p&gt;

&lt;p&gt;The second package is Jest, which is a "test runner". Jest ships out of the box when you make a project using &lt;code&gt;create-react-app&lt;/code&gt;, but it &lt;strong&gt;is&lt;/strong&gt; a third party library. You don't have to use Jest if you discover that an alternate test runner offers more applicable functionality, but it's the most widely used and a great place to start. &lt;/p&gt;

&lt;h2&gt;
  
  
  What's a test runner?
&lt;/h2&gt;

&lt;p&gt;Test runners are not all the same, but their overall purpose is to read the test files and print some output based on whether or not the &lt;em&gt;expectations&lt;/em&gt;, otherwise known as &lt;em&gt;assertions&lt;/em&gt;, are met for each test. &lt;/p&gt;

&lt;p&gt;Jest itself is a &lt;strong&gt;runner&lt;/strong&gt; (meaning you can read tests with it), &lt;strong&gt;assertion library&lt;/strong&gt; (meaning you can write expectations with it), and a &lt;strong&gt;mocker&lt;/strong&gt; (meaning you can create a fake replica of outside functionality to mess around with in the testing space). Now let's look at another picture of landscape really quick.&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%2Fveef036yo868cbl9xjsb.jpg" 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%2Fveef036yo868cbl9xjsb.jpg" alt="Discovery Park, WA"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Discovery Park again!! What a nice damn place to be.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Okay, back to the code snippet:&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;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;screen&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="s1"&gt;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;renders learn react link&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;App&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;linkElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/learn react/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;linkElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&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;We can now determine which parts are Jest: &lt;code&gt;test&lt;/code&gt; and &lt;code&gt;expect&lt;/code&gt; (assertions) &lt;/p&gt;

&lt;p&gt;and which parts are the React Testing Library: &lt;code&gt;render&lt;/code&gt; and &lt;code&gt;screen&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One last thing you may be wondering...&lt;/strong&gt; why don't we have to import &lt;code&gt;test&lt;/code&gt; and &lt;code&gt;expect&lt;/code&gt; from Jest?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And that the answer to that would be...&lt;/strong&gt; that Jest sneakily adds its methods into the global scope of your &lt;code&gt;.test.js&lt;/code&gt; files. If you pop a &lt;code&gt;console.log(global)&lt;/code&gt; into one of your test files and then run it, you can physically see every single method available to you in that space. Be warned, that is a huge console.log you are about to see. But &lt;code&gt;expect&lt;/code&gt; and &lt;code&gt;test&lt;/code&gt; are in there. &lt;/p&gt;

&lt;p&gt;At this point, go ahead and run &lt;code&gt;yarn test&lt;/code&gt; if you haven't already. Our app doesn't have the learn react link, so of course the original test won't pass. &lt;/p&gt;

&lt;h2&gt;
  
  
  Determining What to Test
&lt;/h2&gt;

&lt;p&gt;Now that we've got a taste of what tools &lt;code&gt;create-react-app&lt;/code&gt; grants us, we can begin to think about what tests to write. There are three main types of tests: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Unit&lt;/strong&gt; - Tests one single piece of functionailty, like a method or a piece of state, in a vaccuum. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integrations&lt;/strong&gt; - Tests a group of methods or components together, to make sure they work properly in combination. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;End-to-End&lt;/strong&gt; - Begins where the site user would begin and tests the entirety of the available app.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;⚠ Which type of test do you think is currently being rendered in &lt;code&gt;App.test.js&lt;/code&gt;?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I've read differing opinions on which style you should &lt;em&gt;begin&lt;/em&gt; your testing with, but the one that makes the most sense to me is to write integrations tests. Overall, the prevailing philosophy is to test your app the way that it might be used by a user. So let's take that and look at what our app does. Go ahead and spin up the app from a second terminal using &lt;code&gt;yarn start&lt;/code&gt;. &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%2Fkem6g4eve1vqk7k9k03i.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%2Fkem6g4eve1vqk7k9k03i.png" alt="Screenshot of the pre-built app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wow, it's a dog. &lt;/p&gt;

&lt;p&gt;While running the app, you should be able to click on the image to flip it back and forth between a dog, and not-a-dog. So there you have it: seems like we should write a test to make sure that clicking on the image switches it back &amp;amp; forth. After all, that's what our users are doing. &lt;/p&gt;

&lt;h2&gt;
  
  
  Writing the Test
&lt;/h2&gt;

&lt;p&gt;We'll start by rendering the app. That part we don't have to change.&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;render&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="s1"&gt;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Switches image upon clicking&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;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;Next, we need to grab the image from the DOM, so we can simulate a click. The &lt;code&gt;screen&lt;/code&gt; import from React Testing Library is pre-bound to the &lt;code&gt;document.body&lt;/code&gt;, so conceptually, you can query &lt;code&gt;screen&lt;/code&gt; like you would the &lt;code&gt;document.body&lt;/code&gt; in vanilla JS. The methods are a little different, but the idea is the same. If you are using VS Code, there is a quick way to check out all the methods available on the &lt;code&gt;screen&lt;/code&gt; object. Head over to your &lt;code&gt;App.test.js&lt;/code&gt; file and type &lt;code&gt;screen.&lt;/code&gt;, and you should see some options pop up in a drop down that you can scroll through with the arrow keys. &lt;/p&gt;

&lt;p&gt;You may notice that "getByTagName" is not available -- so how do we grab an image? Well, images have alt texts.&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;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;screen&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="s1"&gt;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Switches image upon clicking&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;App&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;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByAltText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/My dog, Beany/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;img&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;Since this is our first time poking around with tests, I recommend &lt;code&gt;console.log&lt;/code&gt;ging as much as you need to in order to prove that your ideas are working. If you run the above code, you should see a log in the test server that looks like a React Node. &lt;/p&gt;

&lt;p&gt;Now we've rendered the App and we have ahold of the image, so it's time to simulate a click.&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;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fireEvent&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="s1"&gt;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Switches image upon clicking&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;App&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;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByAltText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/My dog, Beany/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;img&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;We can import the &lt;code&gt;fireEvent&lt;/code&gt; object from the React Testing Library and use its &lt;code&gt;click&lt;/code&gt; method. By passing it the image, we would expect that the image on the screen is now changed. So for the final part of the test, we're going to write out that expectation.&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;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fireEvent&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="s1"&gt;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Switches image upon clicking&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;App&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;dogImg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByAltText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/My dog, Beany/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dogImg&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;notDogImg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByAltText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Rainbow frowny face/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notDogImg&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&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 there you have it: you have written your first test in React. Hopefully you have also gained some tools for understanding the testing libraries and the ability to write more!&lt;/p&gt;

&lt;p&gt;Might as well sign off with a nice picture of some landscape. &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%2F6s0snup0znniqn8gk0wv.jpg" 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%2F6s0snup0znniqn8gk0wv.jpg" alt="Double Bluff Beach on Whidbey Island"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another one of beautiful, sunny Discov- just kidding! This one is from Whidbey Island. &lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>testing</category>
    </item>
    <item>
      <title>Part 2: Creating Models for MongoDB with Mongoose</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Tue, 26 Jan 2021 18:55:01 +0000</pubDate>
      <link>https://dev.to/halented/part-2-creating-models-for-mongodb-with-mongoose-575d</link>
      <guid>https://dev.to/halented/part-2-creating-models-for-mongodb-with-mongoose-575d</guid>
      <description>&lt;p&gt;&lt;em&gt;The fully complete codebase for this project is public at &lt;a href="https://github.com/halented/men-backend-codebase" rel="noopener noreferrer"&gt;THIS&lt;/a&gt; github repo, if you would rather just poke around than reading this whole walkthrough.&lt;/em&gt;&lt;br&gt;
__&lt;/p&gt;

&lt;p&gt;Here's the overview of the two part series. The steps in bold will be covered in this second installment: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initializing a folder with a package manager&lt;/li&gt;
&lt;li&gt;Adding necessary dependencies (and discussing the purposes of each)&lt;/li&gt;
&lt;li&gt;Establishing a connection to MongoDB through &lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;Atlas&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Establishing an Express application &amp;amp; selecting the local port on which to run it&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Creating A Model&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Creating CRUD routes for that model&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing your code out with an API tester like Postman or Insomnia&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Where we left off in the last post, you should at this point have a successful connection with a MongoDB database hosted on the Atlas website, and a running local server on port 5000. What a time to be alive. &lt;/p&gt;
&lt;h2&gt;
  
  
  Creating A Model
&lt;/h2&gt;

&lt;p&gt;Organizationally, it is up to you how you arrange your files inside of this app. This sort of backend is not as opinionated as something like Ruby on Rails, so you can exercise a little more freedom with the structure. &lt;/p&gt;

&lt;p&gt;To keep my files project organized, I like to keep related items in separate folders, so I'll firstly create a new folder named &lt;code&gt;models&lt;/code&gt;, and nest a file called &lt;code&gt;user.model.js&lt;/code&gt; inside of that. &lt;/p&gt;

&lt;p&gt;This is where we will define the requirements of the data model that we intend to map to our database on MongoDB. Remember that we are using the Mongoose library as the messenger between our Express app and our database, so the first thing to do inside of this file is require Mongoose, and grab the Schema class out of the Mongoose library.&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;mongoose&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;mongoose&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;Schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Schema&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can begin to write the format for our User model, by creating a new instance of the Schema class.&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;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first argument for the schema is an object containing the attributes for your data model. Our user model will have 3 attributes: a username, an email, and an age (which is optional). Each attribute will be defined using a key:value pair, with the key being the name of the attribute, and the value being its type. You can check out all available schema types for Mongoose from &lt;a href="https://mongoosejs.com/docs/schematypes.html" rel="noopener noreferrer"&gt;THIS&lt;/a&gt; page. We will just need the String and Number types.&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;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This by itself would work, but remember that we wanted the age to be optional. Well, good news, it already is. The default for each model attribute sets &lt;code&gt;required&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;. So rather than specify that the age is optional, we should specify that the username and email are not.&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;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;required&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;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;required&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;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are allowed to open the value of any attribute out into a full object which specifies further detail for the attribute. For fun let's say you can't use our app unless you're 18 years or older, as well.&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;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;required&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;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;required&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;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;18&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;Validations: easy as that. Any time you make an instance of the user model, it'll run through this Schema to make sure your input attributes meet the requirements. &lt;/p&gt;

&lt;p&gt;The last step is to solidify this Schema as a data model with mongoose, and export it from this file for use in other areas of our project.&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;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can move onto making some CRUD routes for the User. &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating CRUD routes
&lt;/h2&gt;

&lt;p&gt;In keeping with my organizational choices, I'll now create a folder called &lt;code&gt;controllers&lt;/code&gt; which houses the file &lt;code&gt;user.controller.js&lt;/code&gt;. This will be the central hub for making any of the activity involving the User model; predictably, controlling what happens when you try to Create, Read, Update, or Delete a model instance. &lt;/p&gt;

&lt;p&gt;There are two necessary items to import into this file. Since the User model is going to be needed repeatedly in here, we'll need that one. In addition, we will be using express to define some routes that will live on the local port.&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;User&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;../models/user.model&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;router&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="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take a look at that router import -- notice that every time you require the Router, you are calling on a function, which creates a totally new instance of the router within this file. So if we had more than one model, we would have the same code inside of its controller file, but that other router object would be totally different. We are going to define some changes to this instance of the router to make it specific to our User model, then export this instance of the router for the rest of our Express app to use. &lt;/p&gt;

&lt;p&gt;With that said, let's lay down the boilerplate for the routes we need, and export it at the bottom.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/new&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/delete/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/update/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can get to customizing those routes. Notice that you must first pass the path to the router's &lt;code&gt;route&lt;/code&gt; method, then clarify what type of HTTP method will be made to that path. The method, in the first case a &lt;code&gt;post&lt;/code&gt;, will accept one argument: a function to run when this path is hit. We will use anonymous functions so that we can keep each route's functionality encapsulated in this one router.route call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/new&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function is automatically handed a few arguments: the request (&lt;code&gt;req&lt;/code&gt;) information, and the response (&lt;code&gt;res&lt;/code&gt;) information. The request comes from your frontend (or Postman/Insomnia), and the response is what your backend should send in response. Each argument has some built-in functionality that we will make use of here. If you have made post requests before, you should be familiar with the format. Your frontend will make a request to your backend and send with it a &lt;code&gt;body&lt;/code&gt; attribute which contains the information that is to be posted to the database. The &lt;code&gt;body&lt;/code&gt; attribute should look something like this: &lt;code&gt;{ username: "Hal", email: "Halrulez@halgoogle.com", age: 247 }&lt;/code&gt;. Using that information, we will make a new instance of our user model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/new&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&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;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line will use our Mongoose model to create something that should be acceptable to our MongoDB database. The next step is to actually contact the database and try to save the new instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/new&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&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;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;Assuming that the database entry is successful, MongoDB will send us back the newly minted User. There is one key difference between this user instance and the one we created called &lt;code&gt;newUser&lt;/code&gt; -- the User that is sent back from MongoDB will have an ID, which we need to use to do all other sorts of operations on this User instance in the future. Once we receive this verified User instance, we use the line &lt;code&gt;res.json(user)&lt;/code&gt; to complete the cycle by filling our response's json with the user itself.&lt;/p&gt;

&lt;p&gt;The code we wrote &lt;em&gt;should&lt;/em&gt; work, but it is a little frail. We did not handle for the case that our new user gets rejected by the database, which could happen for a myriad of reasons. So let's add in some error handling before we move on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/new&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&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;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error! &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&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;Now that we have that written out, there's one more step before we can test it. As of right now, the Express app that we created inside &lt;code&gt;server.js&lt;/code&gt; doesn't know anything about these model or controller files we've made. So we need to head back over to the server and tell it about our new 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="c1"&gt;// inside server.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userRoutes&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;./controllers/user.controller&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;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;/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userRoutes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By specifying that the app should use '/users', we are able to nest any routes that are defined in the user controller under the '/users' resource first. So in the case of creating a new user, our frontend should make a post request to '&lt;a href="http://localhost:5000/users/new" rel="noopener noreferrer"&gt;http://localhost:5000/users/new&lt;/a&gt;'. &lt;/p&gt;

&lt;p&gt;And now, we can test it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing your code out with an API tester like Postman or Insomnia
&lt;/h2&gt;

&lt;p&gt;I've used both of these apps and enjoy them each equally. No endorsement either way. They do things. &lt;br&gt;
&lt;a href="https://www.postman.com/downloads/" rel="noopener noreferrer"&gt;HERE&lt;/a&gt; is the link to the Postman tester, and &lt;a href="https://insomnia.rest/" rel="noopener noreferrer"&gt;HERE&lt;/a&gt; is the link to the Insomnia one. &lt;/p&gt;

&lt;p&gt;Right now I happen to be using Insomnia, because the name's cool. Once you've gotten all logged in to your tester, you should create a new request, specify that it is a POST request, copy &lt;code&gt;http://localhost:5000/users/new&lt;/code&gt; into the resource section, and select JSON for the body type. Then you can add some raw JSON into the body -- this will match up with what you expect to see from the &lt;code&gt;body&lt;/code&gt; portion that your frontend sends. So again something like &lt;code&gt;{ username: "Hal", email: "Halrulez@halgoogle.com", age: 247 }&lt;/code&gt;. Then send the request! If you've got it all set up properly, you should see a response like 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%2Fraw.githubusercontent.com%2Fhalented%2Fmen-backend-codebase%2Fmain%2Fassets%2Finsomnia-example.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%2Fraw.githubusercontent.com%2Fhalented%2Fmen-backend-codebase%2Fmain%2Fassets%2Finsomnia-example.png" alt="Successful Insomnia Post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We got an ID! Massive success. &lt;/p&gt;

&lt;p&gt;With the completion of this route, we have a working MEN backend. Of course, we need to complete filling out the other CRUD routes, but the hardest work of ensuring that Express, MongoDB, and Mongoose can chat nicely is over. Now might be another good time for a rousing glass of water.&lt;/p&gt;

&lt;p&gt;Since the rest of the routes are simply variations of the first one that we made, I'll put them all in one chunk together that we can look at as a whole:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// using .find() without a parameter will match on all user instances&lt;/span&gt;
    &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;allUsers&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;allUsers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error! &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/delete/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deleteOne&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="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Success! User deleted.&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;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error! &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/update/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdAndUpdate&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;params&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="nx"&gt;req&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Success! User updated.&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;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error! &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&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;Some items to pay attention to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can retrieve the ID from the request URL by accessing req.params&lt;/li&gt;
&lt;li&gt;The update method requires the frontend request to include information for &lt;em&gt;all&lt;/em&gt; fields aside from the id -- this is the most straightforward way of updating our database at the present&lt;/li&gt;
&lt;li&gt;You're in full control of what response is sent back to the frontend. If you wanted to hide the server errors for security reasons, all you would have to do is change what your &lt;code&gt;catch&lt;/code&gt; sends back. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it. Congratulations on conquering MEN!&lt;/p&gt;

</description>
      <category>mern</category>
      <category>mongoose</category>
      <category>express</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>Part 1: Setting Up Your Backend with Mongoose, Express &amp; MongoDB</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Mon, 25 Jan 2021 20:15:31 +0000</pubDate>
      <link>https://dev.to/halented/part-1-setting-up-your-backend-with-mongoose-express-mongodb-2f2p</link>
      <guid>https://dev.to/halented/part-1-setting-up-your-backend-with-mongoose-express-mongodb-2f2p</guid>
      <description>&lt;p&gt;&lt;em&gt;The fully complete codebase for this project is public at &lt;a href="https://github.com/halented/men-backend-codebase" rel="noopener noreferrer"&gt;THIS&lt;/a&gt; github repo, if you would rather just poke around than reading this whole walkthrough.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;__&lt;/p&gt;

&lt;p&gt;I've been a big fan of React for a while, but that part of the MERN stack isn't involved in this post. If anyone does have a specific question about how to hook the backend we are about to build up with a React frontend, please leave a comment and I would be happy to make a new post with that information as well.&lt;/p&gt;

&lt;p&gt;Since this is focused on MERN without the R, the acronym we'll use for this post is MEN. Why not. 😄&lt;/p&gt;

&lt;p&gt;Here's the overview of the two part series. The steps in bold will be covered in this first installment: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Initializing a folder with a package manager&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Adding necessary dependencies (and discussing the purposes of each)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Establishing a connection to MongoDB through &lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;Atlas&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Establishing an Express application &amp;amp; selecting the local port on which to run it&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Creating A Model&lt;/li&gt;
&lt;li&gt;Creating CRUD routes for that model&lt;/li&gt;
&lt;li&gt;Testing your code out with an API tester like Postman or Insomnia&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It should be a good time. This post will assume that you have a medium level of JavaScript/programming capabilities -- that is, if you are just starting out with learning JS, this post might knock you on your ass, so bookmark it for later and revisit when you're pretty good with the basics. This post also assumes you have NodeJs installed on your computer already. You can check if you do by running the command &lt;code&gt;node -v&lt;/code&gt; in your terminal. If it does not spit out a version for you, please install Node from &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;HERE&lt;/a&gt; before beginning this walkthrough. &lt;/p&gt;

&lt;p&gt;Other than that, if you're ready to go then let's go!&lt;/p&gt;

&lt;h2&gt;
  
  
  Initializing a folder with a package manager
&lt;/h2&gt;

&lt;p&gt;This part is pretty straightforward. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From your terminal, navigate to whatever directory you want this project to live in&lt;/li&gt;
&lt;li&gt;Make a new folder with &lt;code&gt;mkdir MEN-backend&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cd&lt;/code&gt; into that folder&lt;/li&gt;
&lt;li&gt;Enter the command &lt;code&gt;yarn init&lt;/code&gt; or &lt;code&gt;npm init&lt;/code&gt;, depending on which package manager you want to use. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The differences between yarn and npm are negligable for our circumstances, but I use yarn because the word's cute. Once you've done that, your terminal will ask you a series of questions -- you can just slam on the enter key a bunch of times to stick with the defaults, or you can change the versioning/name/licensing info at your discretion. These details will not affect the project.&lt;/p&gt;

&lt;p&gt;Once that's complete, you'll notice a package.json file appear inside your MEN-backend directory. This is where your project will keep track of necessary dependencies and libraries that we'll be installing, like Express.&lt;/p&gt;

&lt;p&gt;One that note, let's get to installing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding necessary dependencies
&lt;/h2&gt;

&lt;p&gt;One important tip for this section: the words &lt;em&gt;dependency&lt;/em&gt;, &lt;em&gt;library&lt;/em&gt;, and &lt;em&gt;package&lt;/em&gt; are going to be used pretty much interchangably. I'm just using these phrases to reference any outside code base that we'll be making use of in our project. &lt;/p&gt;

&lt;p&gt;The first thing add is &lt;a href="http://expressjs.com/" rel="noopener noreferrer"&gt;Express&lt;/a&gt;, of course. Very necessary for MEN. Adding packages to your established &lt;code&gt;package.json&lt;/code&gt; file is pretty easy; from inside of our backend directory you can run the command &lt;code&gt;yarn add express&lt;/code&gt; and watch as your terminal does the work of adding it. Once it's done, you will notice that a new folder, &lt;code&gt;node_modules&lt;/code&gt; has appeared in our directory, as well as a file called &lt;code&gt;yarn.lock&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;These two items help your project keep track of not only the libraries that you want to use in your project, but any libraries that those libraries are using. Do not edit these items directly. If something goes wonky with your yarn lock or node modules, just delete both of them and run &lt;code&gt;yarn install&lt;/code&gt; to have 'em regenerate. &lt;/p&gt;

&lt;p&gt;Here are the other packages you should install. With each you can just run &lt;code&gt;yarn add &amp;lt;package name&amp;gt;&lt;/code&gt; to add it to your package.lock file: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://www.npmjs.com/package/cors" rel="noopener noreferrer"&gt;cors&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CORS stands for "cross-origin resource sharing" and the package allows you to configure which domains are trusted and which ones are not. We will be making requests to our backend from a separate origin (that's where insomnia or postman will come into play), so we need to install CORS to allow us to do that. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/dotenv" rel="noopener noreferrer"&gt;dotenv&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are going to need a sort of secret key to access our MongoDB database on the Atlas website. If you plan on uploading your project to Github or some other code sharing site, you &lt;em&gt;shouldn't&lt;/em&gt; upload your secret key as well. Best practices dictate that you should keep that kind of information in a local environment variable. The dotenv package will allow us to add a file called &lt;code&gt;.env&lt;/code&gt; to our project and put our secret key in there. The library will configure environment variables to your &lt;a href="https://codeburst.io/process-env-what-it-is-and-why-when-how-to-use-it-effectively-505d0b2831e7?gi=ce14aad160a0" rel="noopener noreferrer"&gt;process.env&lt;/a&gt; object, which makes it global for your project. We aren't going to go through uploading this project to Github, but if you are doing that, you'll want to add the &lt;code&gt;.env&lt;/code&gt; file to your &lt;code&gt;.gitignore&lt;/code&gt; list. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://mongoosejs.com/docs/index.html" rel="noopener noreferrer"&gt;mongoose&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mongoose allows us to: map the model attributes and requirements to the database, create new collections and documents in our database, and make queries to retrieve info from the database.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://nodemon.io/" rel="noopener noreferrer"&gt;nodemon&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will use nodemon to actually serve the backend routes locally on whatever port we choose. If we were to choose port 5000, say, we can run nodemon to have our app served on &lt;a href="http://localhost:5000/" rel="noopener noreferrer"&gt;http://localhost:5000/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Establishing a connection to MongoDB / Establishing an Express App
&lt;/h2&gt;

&lt;p&gt;There are different ways to use MongoDB, but using the Atlas website gives you a pretty clear and easy tools for interacting with your data, and you get one free database to fiddle around with so you don't have to pay to use it for our purposes. &lt;/p&gt;

&lt;p&gt;First, set up an account. Head to &lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;https://www.mongodb.com/cloud/atlas&lt;/a&gt; &amp;amp; fill out the Start Free steps. Pick the free options of course, and select "Shared Clusters" when prompted.&lt;/p&gt;

&lt;p&gt;Under provider and region, select Google Cloud, and then pick whichever region is closest to you. This is where the server is located, so the closer you get the less latency between requests. Make sure your Cluster Tier is set to "M0 Sandbox", name it whatever you'd like, and click Create Cluster. &lt;/p&gt;

&lt;p&gt;You can just let that sit while it creates -- don't mess with the webpage or close it until it's done. While you're waiting, maybe listen to a song. Have some water. Stretch your back &amp;amp; unfocus your eyes for a second.&lt;/p&gt;

&lt;p&gt;Okay, now it's time to write some code. &lt;/p&gt;

&lt;p&gt;In the topmost directory of your project, create a file called &lt;code&gt;server.js&lt;/code&gt;. This is will act as the main hub for your app and its connections to the DB. The first thing we need to do is get Express in there. Here's what it looks like to actually establish an Express app:&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The invoking parentheses following express call a constructor method from inside the Express library which builds the boilerplate for our app. &lt;/p&gt;

&lt;p&gt;⚠️ Reminder! Express is a framework using NodeJs. It's the most important part of allowing us to use JavaScript as a backend server. ⚠️&lt;/p&gt;

&lt;p&gt;Now that we've actually got an app to fiddle around with, let's tell that app that it should be allowed to accept requests from outside sources by handing it the CORS library. &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;cors&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;cors&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Next we will tell the express app that it should expect to serve and receive data in the JSON format. Our MongoDB backend will take care of that for us, so we don't need to configure it on that end, just this one. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Okay, the next thing is to actually connect your DB with your Express app. Here we have a group of libraries coming together to make ends meet. Firstly, we need to get Mongoose in there to usher the data back and forth for us -- we can think of Mongoose as a messenger which speaks to our Express app, travels over to MongoDB, delivers some info, then carries Mongo's response back to the app. Begin by requiring it -- &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;mongoose&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;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;and then calling upon the &lt;code&gt;connect&lt;/code&gt; function to open a connection.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;useNewUrlParser&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;useCreateIndex&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;useUnifiedTopology&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt;
&lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;once&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;open&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="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;DB connected.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Okay, that's a chunk of code, so let's chat through it. The &lt;code&gt;connect&lt;/code&gt; function accepts two arguments: the first is the the URI which points at our actual database on Atlas, and the second is a configuration object for how it should talk to that database. It's not highly important to memorize the details of the config object, but there have been some updates to both Mongoose and the Atlas setup which caused bumps in the road, so these configurations are just some standard fixes to make sure the communications still go smoothly. &lt;/p&gt;

&lt;p&gt;The second part, where we grab &lt;code&gt;mongoose.connection&lt;/code&gt; out of the Mongoose library, is simply a listener. It listens your &lt;code&gt;connect&lt;/code&gt; function, and throws up a little message on the server logs once that connection is successful. You can log whatever message you like. Maybe just a put a Shakespeare quote on there or something. The world's your oyster. &lt;/p&gt;

&lt;p&gt;You may have noticed that &lt;code&gt;source&lt;/code&gt; is not defined anywhere in our code yet, so let's fix that. Head on over to the Atlas website again. It should have had plenty of time to finish setting up, so hopefully you see a screen that looks something like 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%2Fraw.githubusercontent.com%2Fhalented%2Fmen-backend-codebase%2Fmain%2Fassets%2Fatlas-example.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%2Fraw.githubusercontent.com%2Fhalented%2Fmen-backend-codebase%2Fmain%2Fassets%2Fatlas-example.png" alt="Atlas Website Screenshot"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Click the "CONNECT" button. Add your current IP address (since you will be making requests from your own computer), then make up whatever name and password you want to use for your secret connection URI. This doesn't really have to be that secure, since we are just using it to learn. I'm going to set mine to something pretty simple; username: "Hal", password: "Hal". Whatever you pick, keep ahold of it as we will use it. Click the "Choose a connection method" button. &lt;/p&gt;

&lt;p&gt;We won't be installing the mongo shell or MongoDB Compass, so let's just pick "Connect your application". After clicking that you'll see the URI we want! It'll look something like this: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;mongodb+srv://&amp;lt;username&amp;gt;:&amp;lt;password&amp;gt;@testercluster.m7k7n.mongodb.net/&amp;lt;dbname&amp;gt;?retryWrites=true&amp;amp;w=majority&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Fill in the username/password and whatever you named your DB (I named mine TesterCluster). Now, we &lt;em&gt;could&lt;/em&gt; just pop that URI into the &lt;code&gt;connect&lt;/code&gt; function, and it would work just fine, but as was mentioned at the beginning, you shouldn't actually put your credentials and/or DB connection secrets out there in the open. So now we get to use the &lt;code&gt;dotenv&lt;/code&gt; library. Add this to server.js to do so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;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;dotenv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Calling on &lt;code&gt;config()&lt;/code&gt; allows it to set up using the out-of-the-box configurations, which is totally fine for our purposes. With dotenv required, we can now create a file in our directory called &lt;code&gt;.env&lt;/code&gt; which will house the new URI we just grabbed. The inside of our &lt;code&gt;.env&lt;/code&gt; file just has a single line:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ATLAS_CONNECTION = mongodb+srv://Hal:&amp;lt;Hal@testercluster.m7k7n.mongodb.net/TesterCluster?retryWrites=true&amp;amp;w=majority&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once that is saved, the dotenv library will read your .env file and add a key of "ATLAS_CONNECTION" to your process.env object, with the value of the correct URI there. Remember that the process.env is globally available -- so all that remains is to add this line into your server.js file:&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;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ATLAS_CONNECTION&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Now for the moment of truth. Head to your terminal and enter the command &lt;code&gt;nodemon server&lt;/code&gt;. If everything is set up correctly, you should see some startup messages from nodemon, and then your own console log, "DB connected." Or a Shakespeare quote. Whatever you went with.&lt;/p&gt;

&lt;p&gt;The last step for this post to set up our express app to serve our data locally.&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;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&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;5000&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="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;`Successfully served on port: &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 first line is saying, "check if our process.env object has specified a port to use, and if not, default to using port 5000." Once you hit save, you should see the nodemon server reload in your terminal, and the second message confirming that our local server is working too. &lt;/p&gt;

&lt;p&gt;And that's it for the first installment in this two-part series! Follow my posts to get a notification for the release of the second installment. And please submit a comment with any questions, improvements, or corrections!&lt;/p&gt;

</description>
      <category>mern</category>
      <category>node</category>
      <category>express</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>JavaScript Stuff I Thought Was Weird Which Just Turned Out to Make Sense After All</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Tue, 15 Dec 2020 16:03:43 +0000</pubDate>
      <link>https://dev.to/halented/javascript-stuff-i-thought-was-weird-which-just-turned-out-to-make-sense-after-all-fgo</link>
      <guid>https://dev.to/halented/javascript-stuff-i-thought-was-weird-which-just-turned-out-to-make-sense-after-all-fgo</guid>
      <description>&lt;p&gt;When I first started learning JavaScript, I definitely had a beat-it-to-fit, paint-it-to-match mentality. My wrangling of code was basically just elbow grease and duct tape, and I filed a lot of things away as cute little mysteries, assuming that JavaScript kept as them a personality quirk.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fGYeErIQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/EckGOhe.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fGYeErIQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/EckGOhe.jpeg" alt="keep your secrets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But it turns out, most of the stuff that I thought was totally bonkers, actually does make sense when you get down to the brass tacks of it.&lt;/p&gt;

&lt;p&gt;I'm writing this post about a favorite example of this. Here are two code snippets which are true:&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;uno&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;topple&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;arg&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="nx"&gt;topple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uno&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;uno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&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;arr&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="nx"&gt;topple&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;arg&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="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toodledoo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;topple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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;arr&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toodledoo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a newbie programmer, &lt;em&gt;this made absolutely no sense&lt;/em&gt;. In one case, you pass an argument and perform an alteration, and it does not change the variable. In the second case, it totally does change the variable. I'm also a bootcamp grad, so at the time I really had to pick my battles for which topic I could do a deep dive into, and this was not one of them. I settled for memorizing the truth that you could change arrays and hashes, but for some reason you couldn't get the same functionality out of variables pointing to a string or a number. I'm...perhaps a little &lt;em&gt;too&lt;/em&gt; good at going with the flow, so for a while I chalked it up to a silly weird thing left it at that. &lt;/p&gt;

&lt;p&gt;Since I've had a little more time and a lot more freedom to explore topics of interest, I've found some super fascinating things out about that code. &lt;/p&gt;

&lt;p&gt;Firstly, I learned that in JavaScript, there are a limited number of &lt;strong&gt;types&lt;/strong&gt; that any one piece of code can be. Here are all of them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Object&lt;/li&gt;
&lt;li&gt;Function&lt;/li&gt;
&lt;li&gt;Undefined&lt;/li&gt;
&lt;li&gt;Null&lt;/li&gt;
&lt;li&gt;Symbol&lt;/li&gt;
&lt;li&gt;Bigint&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that's it. Everything you touch and see and get slapped with in the form of an error message is gonna be one of these things. For the purposes of explaining the weirdness above, I'm really just going to talk about &lt;code&gt;Numbers&lt;/code&gt; and &lt;code&gt;Objects&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before moving on, it's paramount to define &lt;code&gt;Objects&lt;/code&gt;, because I feel like that gets thrown around a lot with different meanings. In terms of the underlying skeleton of JavaScript, MDN Docs defines an &lt;code&gt;Object&lt;/code&gt; like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In computer science, an object is a value in memory which is possibly referenced by an identifier.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which, is just, so useless. What does that even mean? You may have heard elsewhere that "everything in JavaScript is an object," and I feel like that's closer. That's almost true. Arrays, hashes, regex, these are all objects. Importantly, &lt;code&gt;Objects&lt;/code&gt; are mutable.  Changeable. Hashes and arrays can alter their contents constantly.&lt;/p&gt;

&lt;p&gt;There are a few things in JS that are &lt;em&gt;not&lt;/em&gt; &lt;code&gt;objects&lt;/code&gt;, and &lt;code&gt;numbers&lt;/code&gt; are one of them. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Numbers&lt;/code&gt; are considered a special sort of thing in JS, called a "primitive." Primitives are immutable. Can you change the value of one? Think for a sec, what if you add 1+5...Does 1 stop being 1? Nope, 1 will always exist, in its primitive state, unchanging. &lt;/p&gt;

&lt;p&gt;So, that brings us back around to our examples. Let's take a deeper look into the first 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;uno&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;topple&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;arg&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="nx"&gt;topple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uno&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;uno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step One: Set a variable called &lt;code&gt;uno&lt;/code&gt; which points to the primitive &lt;code&gt;number&lt;/code&gt; 1.&lt;/p&gt;

&lt;p&gt;Step Two: Define a function which accepts an argument, and in the body of the function, permanently increases that argument by 1.&lt;/p&gt;

&lt;p&gt;Step Three: Call function. Hand it our &lt;code&gt;uno&lt;/code&gt; variable. This step is where the secret magic happens that I previously didn't understand. I was saying things like "hand it a variable" but failing to realize that you can't hand &lt;em&gt;anything&lt;/em&gt; a variable in JavaScript. You hear that craziness? You go around for a couple years of your life thinking that you pass information in your code around using variables and it's a total lie. &lt;strong&gt;You &lt;em&gt;can't hand anything a variable&lt;/em&gt; in JavaScript&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It's because variables are instantaneously evaluated. In step three, we aren't handing that function anything called &lt;code&gt;uno&lt;/code&gt;, we are handing it a tiny puzzle, which JavaScript works out right then and there in the arguments. It follows &lt;code&gt;uno&lt;/code&gt; back to the source and finds out that we set it to point to the number 1, and it actually just passes the number 1 through to the body of the function. The reason that our variable is unchanged on the other side of the function is that our body never saw nor touched the variable.&lt;/p&gt;

&lt;p&gt;Okay. Now, that is super cool, and very exciting to be able to see the secrets of JavaScript happening in real time -- I imagine this is how I would feel if I could watch human bones and tendons interacting with perfect clarity under the skin of a living person. I wonder if surgeons get this feeling.&lt;/p&gt;

&lt;p&gt;But we still have another mystery on our hands: If this first example can't alter the variable, why in God's green earth would it be able to alter the second one, in a nearly identical example?&lt;/p&gt;

&lt;p&gt;Remember what we talked about with the mutable items versus the non-mutable items. As we said before, there is only one 1. You can't change the number 1, and any time you reference it, you are always pointing at the same ol' number 1. One symptom of being mutable is the ability to be &lt;em&gt;multiple&lt;/em&gt;. Because arrays can change, they can have many different instances that exist in different places in your computer's memory. You can get a really good visual of this by running a short test in your browser's console. First, run this 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;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&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="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="mi"&gt;1&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;Notice the result. Now don't clear your screen, and run this 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;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&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="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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the first one, you see the number 1 with a little 10 to the left of it, indicating that the number was logged 10 times in a row. What's up with the next one? Why does it show ten arrays logged on separate lines?&lt;/p&gt;

&lt;p&gt;Right, it's not repeating anything. &lt;em&gt;Those are all different arrays&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Cool, so it's possible that you've already put together where I'm going with this, but might as well finish it out. Let's dive into the second example like we did the first.&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;arr&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="nx"&gt;topple&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;arg&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="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toodledoo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;topple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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;arr&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toodledoo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step One: Set a variable called &lt;code&gt;arr&lt;/code&gt; which points to some new  &lt;code&gt;object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Step Two: Define a function which accepts an array, and in the body of the function, permanently adds "toodledoo" to that array.&lt;/p&gt;

&lt;p&gt;Step Three: Call function. Hand it our &lt;code&gt;arr&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;But! As we know, you can't pass variables anywhere in JS. So &lt;code&gt;arr&lt;/code&gt; will get evaluated right then and there, and JavaScript will instead pass the array itself into the body of the function. And here's where it all comes together: &lt;/p&gt;

&lt;p&gt;Because you can have multiple arrays in a single coding project, and because each individual array holds a different place in memory, when our function adds "toodledoo" into the array that it's given, that changes its value in the actual computer. So when we go to check the variable again, it doesn't try to grab a new array, it goes back to the place in memory where our array was stored and finds that this particular instance has been changed. &lt;/p&gt;

&lt;p&gt;To recap: If you pass a function the number 1, it can't change that number at all. Even if you set 10,000 variables to the number 1, they'd all be pointing to the same place in memory, cause nobody can mess with math. However, if you pass a function an &lt;code&gt;object&lt;/code&gt; such as an array, all new objects point to a different place in memory, so if you change that array you can still see the changes later down the line.&lt;/p&gt;

&lt;p&gt;Fascinating, like I said. Now, one last thing. This code is also true:&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;uno&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;topple&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;uno&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="nx"&gt;topple&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;uno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why do you think that is?&lt;/p&gt;







&lt;p&gt;&lt;em&gt;All credit to Dan Abramov and his wonderful newsletter for helping me become a more enlightened programmer.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>objects</category>
      <category>primitives</category>
    </item>
    <item>
      <title>scalability in system design -- lazy post style!</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Tue, 07 Apr 2020 01:02:28 +0000</pubDate>
      <link>https://dev.to/halented/scalability-in-system-design-lazy-post-style-5320</link>
      <guid>https://dev.to/halented/scalability-in-system-design-lazy-post-style-5320</guid>
      <description>&lt;p&gt;when this started out, it was not but a journal entry. after re-reading it for studying purposes, i determined that there was enough information involved so as to be considered useful in the world. i did NOT, however, determine that i should go back and add in capitalization, or a professional tone. this here is a rundown of some Very Basic stuff that one should consider if one is interested in making one's system scalable. enjoy!&lt;/p&gt;

&lt;p&gt;in terms of system design, when you are making a personal project, you are probably gonna start out with the whole thing on your one computer. that makes sense. you don’t have 50 computers. you don’t have 100 hands. &lt;/p&gt;

&lt;p&gt;but, if the project that you are making gets crazy popular, and starts getting a ton of hits, what happens if your one single computer goes down, or the one web host that you are using has a pause in service, or something? your site goes down and all your users turn their backs on this project and every project you will ever make, never to trust again. &lt;/p&gt;

&lt;p&gt;you’ll want to prevent this with a little proactive planning. maybe you decide to host with TWO web servers. eh? good for you. now, how do you decide which user will go to which web server? the client comes in, clicks something on your site, sends a packet of information and…unless you plan on hooking every client up to every server that you have, you will need a way of managing these requests, and sending them in a balanced way to your separate servers. you will need a &lt;strong&gt;load balancer&lt;/strong&gt;. this is sometimes referred to as a black box. its relationships can be visually represented like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mj2KQE5e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bgef0vynbiv36k1o9ruv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mj2KQE5e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bgef0vynbiv36k1o9ruv.png" alt="simple implementation of a load balancer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;cool. you will need to determine some way that the load balancer makes its choices, based on how busy the servers are, just the next one in line (which is called round robin style), or what information each server is responsible for (for example, if one server only has information on images and the other server only has information on text files). how you divvy up the requests is totally up to you, that’s between you and God. and your company. and your servers. and well, also your load balancer. &lt;/p&gt;

&lt;p&gt;the next problem is that if a client who is initially directed to server 1, somehow gets shunted off to server 2 (say, if they log in from a different device, or they use an incognito window, or something), that client will wind up with two different sessions, thereby defeating the power of sessions. the client might have a cart filled up with stuff on one session, log onto their phone, and be frustrated to see that none of their items are there anymore. &lt;/p&gt;

&lt;p&gt;what we need are &lt;strong&gt;sticky sessions&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;how do we get that? unless we want to do some trickery with "shared state", we need to store this information somewhere on the user. we could house the data for the specific server that the client belongs to inside of a cookie set by the load balancer. this way when the client comes back to log on, it presents its cookie, and the balancer can say oh! looks like that cookie says server one, off you go. &lt;/p&gt;

&lt;p&gt;so that’s fantastic. but, if these servers are operating independently, and each one is trying to persist data, we will wind up with two separate databases. so instead of letting each one keep a DB, we will want to pull that out and have the DB live separately. so our chart will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JkbjJV1L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bc510ctuuubf1x2ie44i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JkbjJV1L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bc510ctuuubf1x2ie44i.png" alt="multiple servers with the database abstracted away"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and this looks really good. i can filter clients, they can have sticky sessions with server-filled cookies dispensed by the load balancer, and i can ensure that all DB information is up to date. but there are a lot of vulnerabilities to the system, due to something called &lt;strong&gt;single point of failure&lt;/strong&gt; — that is to say, there are two sections of this which depend fully upon one single machine to &lt;em&gt;not fail&lt;/em&gt; or the whole thing goes down. It’s a replication of our original problem of only using one host. if the load balancer dies, or the DB blows up somehow, the whole operation is doomed.&lt;/p&gt;

&lt;p&gt;counteracting that vulnerability can be surprisingly straightforward: buy two of the things that are single. &lt;/p&gt;

&lt;p&gt;or, buy several. buying more of the thing is called horizontal scaling. You can also try a method of making your one single thing indestructible — installing the most RAM, the best software, more CPU, etc. that would be considered vertical scaling. &lt;/p&gt;

&lt;p&gt;you get the picture. you can choose to house everything in one glimmering skyscraper, or you can choose to install fifty ground level buildings that don’t have the fanciest amenities but can handle the smaller workloads they are given. i think most opt for the ground levels, as spreading out your assets is just, inherently more secure.&lt;/p&gt;

&lt;p&gt;with your typical load balancer that you can buy off the market for, &lt;a href="https://kemptechnologies.com/compare-kemp-f5-big-ip-citrix-netscaler-virtual-load-balancers/"&gt;anywhere between $3,500 and $70,00&lt;/a&gt;, it does usually get you two load balancers. these balancers “listen to each others’ heartbeats”, I read somewhere, which just means that they can each detect if the other is down. in an “active-active” system of load balancers, each of them always handle all the requests, and one is just temporarily given all responsibility if the other goes down. in an “active-passive” system, the active one is on the job for forever, and if the passive one ever hears of trouble, it automatically promotes itself to active, and takes over until its downed teammate is able to res. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/SyejxPoET3pEk/giphy-downsized-large.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/SyejxPoET3pEk/giphy-downsized-large.gif" alt="silly gif of a dog pretending to give CPR to a human"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;with your typical DB…well…there are a lot of options there, but they come in a similar format to the active-active/active-passive setups. you might go with the master-master, where both databases have all information and can be read and written to, and share changes with each other. if one goes down, no biggie, the other one just manages all read and write requests. or, you might go with the master-slave approach, which just has the absolute worst name, and i think they should consider renaming it. for my own mental health, we will refer to this one as the master-understudy approach. the master stores all the information, and the understudy or understudies all store exact copies. any time something is written to the master, it is written to all others as well. &lt;/p&gt;

&lt;p&gt;this setup is powerful for sites that have read-heavy requests, because any one of the understudies can take those requests — the master only really HAS to worry about the write requests. &lt;/p&gt;

&lt;p&gt;if the master goes down in this case, it is only unavailable for long enough for one of the understudies to promote itself to master. then you can go to the store and grab another drive or replace the cable or fix whatever went wrong with the original db. &lt;/p&gt;

&lt;p&gt;bear in mind that with multiple DB’s you’ve got another need for a load balancer — one to manage the which server request goes to which database at what particular time.  &lt;/p&gt;

&lt;p&gt;so now you have something that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y_b_l5Di--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mrwokmqis7r2tfup7iyp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y_b_l5Di--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mrwokmqis7r2tfup7iyp.png" alt="scaled system using multiple databases, servers, and load balancers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and they all live together in a happy family in their house, in the middle of their street. if the house burns down, well, that’s another single-point-of-failure-fish you would have to think about frying. perhaps, something something something The Cloud, and another house.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>scalability</category>
      <category>silliness</category>
    </item>
    <item>
      <title>Passing Functions as Arguments in Ruby</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Thu, 13 Feb 2020 22:27:30 +0000</pubDate>
      <link>https://dev.to/halented/passing-functions-as-arguments-in-ruby-5b5i</link>
      <guid>https://dev.to/halented/passing-functions-as-arguments-in-ruby-5b5i</guid>
      <description>&lt;p&gt;It can be done! As per my last blog post, with no further ado, here is how you do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;first_option&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"space jam"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;second_option&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"dogs rule"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;receives_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;receives_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:first_option&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is &lt;strong&gt;imperative&lt;/strong&gt; to notice that you must pass the function through as a symbol when you are ready to send it along. &lt;/p&gt;

&lt;p&gt;There are a few things about this setup that you may find interesting. When I first started looking into this, I assumed that you could just pass them through normally, using &lt;code&gt;receives_function(first_option)&lt;/code&gt;. I figured, Ruby is nice right? &lt;/p&gt;

&lt;p&gt;Wrong. &lt;/p&gt;

&lt;p&gt;Just kidding, I mean Ruby is nice, but it's not magic. Because Ruby is a synchronous language and function calls only require the name of the function, Ruby will read &lt;code&gt;receives_function(first_option)&lt;/code&gt; and &lt;em&gt;immediately&lt;/em&gt; invoke the &lt;strong&gt;first_option&lt;/strong&gt; method, before it enters &lt;strong&gt;receives_function&lt;/strong&gt; method at all.&lt;/p&gt;

&lt;p&gt;So let's dive into the other stuff. The symbol terminology is Ruby's built-in way to allow you to reference a function without calling it. By placing the symbol in the argument for &lt;strong&gt;receives_function&lt;/strong&gt;, we are able to pass all the info along and actually get into the &lt;strong&gt;receives_function&lt;/strong&gt; code block before executing anything. &lt;/p&gt;

&lt;p&gt;The next step is to figure out how to call a function which has been sent as a symbol. It gets a little hairy here because once we are inside of the &lt;strong&gt;receives_function&lt;/strong&gt; code block, all we have to work with is a variable called &lt;em&gt;func&lt;/em&gt;. Not a symbol at all. This is where Ruby's &lt;strong&gt;method&lt;/strong&gt; method comes into play. &lt;/p&gt;

&lt;p&gt;As a side note, googling "Ruby method method" is marginally annoying, so &lt;a href="https://ruby-doc.org/core-2.6/Object.html#method-i-method"&gt;here&lt;/a&gt; is the link to the Ruby Docs to save you the time if you're interested.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;method&lt;/strong&gt; method takes an argument of a symbol and returns information about that symbol. When you pass it a symbol of another method, it will return something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&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="n"&gt;pry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;Method: main.first_option&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this it's saying hey, you passed me a method, and its name is first_option. You can chain a few commands on the end of this return value to determine other information about the symbol. For example, you could add &lt;code&gt;.source_location&lt;/code&gt; and it would give you the file name and the line on which the original &lt;strong&gt;first_option&lt;/strong&gt; function was defined. That's very useful for debugging, but in our case we don't care about that -- we just wanna invoke it. So, as you saw in the example, we chain a &lt;code&gt;.call&lt;/code&gt; on there and "call" it a day. &lt;/p&gt;

&lt;p&gt;Sigh.&lt;/p&gt;

&lt;p&gt;So that's that! Have fun out there.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Passing State to Components Rendered by React Router (and Other Fun Things)</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Wed, 17 Apr 2019 18:54:52 +0000</pubDate>
      <link>https://dev.to/halented/passing-state-to-components-rendered-by-react-router-and-other-fun-things-3pjf</link>
      <guid>https://dev.to/halented/passing-state-to-components-rendered-by-react-router-and-other-fun-things-3pjf</guid>
      <description>&lt;p&gt;One of the most frustrating thing about clicking on blog posts is having to scroll through people's long winded explanations of stuff when you can just put the answer at the top. Here's how you do the thing in the title:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NavLink&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/homepage"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Homepage&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;NavLink&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/homepage"&lt;/span&gt; &lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
  &lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Homepage&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;pieceOfState&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pieceOfState&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;)&lt;/span&gt;
&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;If you want details on that, please feel free to read on :)&lt;/p&gt;

&lt;p&gt;The Router itself you can put in whatever place you want -- but it makes the most sense to pick a pretty top-level part of your app, so usually in the render method of your App.js file. As you can see above, the NavLink we are using points to the homepage of this particular site or app, and the route is the driving force which will actually do the work of rendering the component. If you don't need to pass the component any state, you would usually just see the router like so: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'/homepage'&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Homepage&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;But in React, passing state (or helper methods) is where all the power comes from -- it's what makes React so reactive. So you will want to use the first code snippet   to get the functionality you want. The Route path in that code is using the render method to pass an inline function which will render the Homepage -- you may be wondering, why can't we just pass an inline function using the regular component method from snippet #2 and get the same result? The answer is that the component method will actually unmount and remount the entire component every time the state changes if you use an inline function with it. This creates an unnecessarily energy-expensive program when you could just use the neat render method that the friendly React devs intended you to use. &lt;/p&gt;

&lt;p&gt;Now that that part's out of the way, here are the aforementioned Other Fun Things:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Passing the whole dang state&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes, when writing in React, it's hard to keep the code &lt;a href="https://dev.to/juuh42dias/drydont-repeat-yourself-43g3"&gt;DRY&lt;/a&gt;. You may find yourself writing this.state a ton of times while passing specific state pieces to the components you want. A fun little tip to help avoid that issue: you can pass the whole dang state over without specifying pieces. It looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Homepage&lt;/span&gt; &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;That's pretty straight-forward. That's pretty state-forward? At any rate, you can then access the state pieces inside of that component by using this.props.state.pieceOfState. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Active links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stylizing a link so that it responds when a user is on the associated page has never been easier. You can simply give the NavLink a class of activestyle (along with whatever CSS you want to occur) like so: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NavLink&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'/homepage'&lt;/span&gt; &lt;span class="na"&gt;activeStyle&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;fontWeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Homepage&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;NavLink&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;React will handle listening for which page the user is on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Rendering a 404&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes the users of your site will get wayward and decide that they can probably guess the available paths, so they will just type that path in expecting to see it come up. React is nice, and it won't break your site, but it won't tell the user that the page doesn't exist. To render a 404, it's useful to group your routes with a Switch tag. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'/homepage'&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Homepage&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'/profile'&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Profile&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'/seaturtles'&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Seaturtles&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;NoMatch&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In the above, the component "NoMatch" is not given a route, so all routes which are not defined will render the component, which you can build out to render whatever you want your 404 page to look like. You can put anything there. An image of Johnny Bravo. A link to the Wikipedia page on 404's. A never ending scroll loop of the Constitution. The world is your oyster. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Redirects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Intuitively, if your user is logged in, you won't want them to be able to navigate to the '/signin' page. BUT, you also don't want them to see a 404 page there. It's time to implement a redirect. This is accomplished by specifying another Route to '/signin' and giving it the instructions to render a Redirect. Observe:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/signin"&lt;/span&gt; &lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Redirect&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'/search'&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This code shows the Route using the same render method as with passing props, but without the props themselves. The anonymous function points to our Redirect, and we get to specify the URL to which we want our user sent. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An Important Note&lt;/strong&gt;&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%2Fz738e3uwbck0pguldnl4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz738e3uwbck0pguldnl4.gif" alt="note"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You &lt;em&gt;will&lt;/em&gt; have to import any and all Router elements into whatever file you are intending to use them. For example, to do everything listed in this post, you would need to import the proper items at the top of your file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;BrowserRouter&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NavLink&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Redirect&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="s1"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Thanks for stopping by, and happy routing!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>router</category>
    </item>
    <item>
      <title>The Consequence of Sound</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Thu, 04 Apr 2019 16:09:12 +0000</pubDate>
      <link>https://dev.to/halented/the-consequence-of-sound-46ak</link>
      <guid>https://dev.to/halented/the-consequence-of-sound-46ak</guid>
      <description>&lt;p&gt;Sound is powerful. With no prior training, sound can make you happy, or angry, or calm at the drop of a note. Certain sounds can &lt;a href="https://en.wikipedia.org/wiki/Misophonia"&gt;make you go insane&lt;/a&gt;, or &lt;a href="https://en.wikipedia.org/wiki/Synesthesia"&gt;hallucinate&lt;/a&gt;. Often we go through the day thoughtlessly experiencing sound, or allowing others in our lives to control what we hear and when. This doesn’t have to be the case!&lt;/p&gt;

&lt;p&gt;There are many, many ways that you can latch onto the power of sound and make your life more pleasant, and your day ore productive. Two of the phenoms that always come to mind for me (and which I find incredibly useful) involve using sound with state-dependent memory and classical (or Pavlovian) conditioning. &lt;/p&gt;

&lt;p&gt;State-dependent memory was first tested in 1937 in Illinois, where Edward Girden and Elmer Culler discovered that dogs who were in the same “state of consciousness” while learning a particular trick were much less likely to remember the trick if their state of consciousness was altered. In this case, the dogs were high. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KO5slrMk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/592qdgbby4aicgq7eahr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KO5slrMk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/592qdgbby4aicgq7eahr.jpg" alt="this cupcake, man"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Altering a “state of consciousness” can have a lot of different meanings. It can mean transitioning from sleeping to awakening, from sobriety to inebriation, from relaxed to alert, or, in our case, just listening to a really excellent song. Music and sound have the power to affect your state of consciousness as quickly as any other drug might. &lt;/p&gt;

&lt;p&gt;In application, this means that if you turn on the same particular ocean noise while you are learning to code javascript, you will be much more likely to remember javascript down the line if you turn that ocean noise back on while you’re doing it. It’s an interesting and effective way to categorize your skillset and “hack” your brain into the right groove at the right time.&lt;/p&gt;

&lt;p&gt;State-depending memory and classical conditioning go hand in hand. If state-dependent learning is a mental hack, classical conditioning is its physical counterpart. As we’re all familiar with, classical conditioning was popularized by that one guy, also with the dogs. Pavlov’s work really took off when he noticed the fact that dogs cannot consciously drool on command, and yet, they could be trained to do so. In the same way, you can’t always wake up motivated and ready to work. But if you keep an awareness of when you are feeling most motivated and always play a particular noise, you can eventually train your body into productivity as a reaction to that noise. &lt;/p&gt;

&lt;p&gt;It’s important to note that in practice, it’s easy to accidentally get this training cycle reversed. If you turn consistently turn on a noise and tell yourself to “be productive, damnit!” then you are actually going to train yourself into dreading or disliking that noise. So try to avoid that one. No personal experience there or anything. 🙄 &lt;/p&gt;

&lt;p&gt;With all that said, here are some of my favorite noise-based resources that I highly recommend:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://noises.online/"&gt;Noises.online&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.noisli.com/"&gt;Noisli.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://calmyleon.com/"&gt;CalmyLeon.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://purrli.com/"&gt;Purrli&lt;/a&gt; (This one is a cat)&lt;/p&gt;

&lt;p&gt;And you can actually skip all of those, because here is the best website on the internet:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://mynoise.net/"&gt;MyNoise.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MyNoise offers a basic rain noise out the gate that you really don’t have to mess with if you just need to blank out external sounds, but beyond that, it is a truly dynamic, user-friendly, simple, brilliant website. You can save sound settings to a URL, download sounds, publish sounds, check out other user’s published settings, turn it on with Alexa, Spotify, Google Play, and other music services. There is also an app for android and iPhone. The whole project was created and is owned by Dr. Stéphane Pigeon, an engineer, programmer, and sound designer who can be found on on his website here. &lt;/p&gt;

</description>
      <category>sounds</category>
      <category>code</category>
      <category>brainhack</category>
    </item>
    <item>
      <title>Invader Vim</title>
      <dc:creator>Hal Friday</dc:creator>
      <pubDate>Wed, 20 Mar 2019 23:44:02 +0000</pubDate>
      <link>https://dev.to/halented/invader-vim-2h32</link>
      <guid>https://dev.to/halented/invader-vim-2h32</guid>
      <description>&lt;p&gt;Every day is a new adventure in code exploration lately. I grew up using computers for their true purpose: video games. The extent of my knowledge on how to navigate a computer reached all the way from how to install a video game to how to play one. That is, until I took an interest in code about 6 months ago. Now, the computer IS the video game, and there are so many Easter eggs my basket is overflowing.  &lt;/p&gt;

&lt;p&gt;For another frame of reference, back in September I was reading through my first bit of code and it told me that I could copy and paste some stuff into my favorite text editor. So I opened  Text Edit, Mac's lightweight notepad. &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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fegw4luk8yav1k8wncvqp.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fegw4luk8yav1k8wncvqp.png" alt="Master Coder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;"I am a master hacker," I thought. It didn't take long to figure out that this was NOT what was meant by text editor, so I re-situated myself with a fancy new &lt;em&gt;code&lt;/em&gt; text editor (VS Code). &lt;/p&gt;

&lt;p&gt;These days, there are a ton of choices available for which text editor to use. Honestly, I just picked the most popular. Check out this graph from StackOverflow on &lt;a href="https://insights.stackoverflow.com/survey/2018#development-environments-and-tools" rel="noopener noreferrer"&gt;2018's top editors&lt;/a&gt;: &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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6nnd87yt8zb72xi6ofc2.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6nnd87yt8zb72xi6ofc2.png" alt="so many options"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Navigating through text editors, as a super new practice for me, feels how I imagine my grandfather must have felt the first time he tried to use a keyboard. Hunt and peck, hunt and peck. Recently in my coding journey I was observing one of my instructors &lt;em&gt;fly&lt;/em&gt; through his text editor, manipulating lines of code more quickly than anything I'd seen before. Throughout the lecture I was writing this fat list of questions down to ask him how on earth he got around so quickly -- how to pop to the front of the word? The end of the line? The end of the document? Select inside brackets? The list went on.  &lt;/p&gt;

&lt;h2&gt;
  
  
  It turns out, all of these questions point to the same answer: Vim.
&lt;/h2&gt;

&lt;p&gt;Vim was first released on November 2nd, 1991. If you notice on that graph up there, 27 years later, Vim is still the 5th most popular text editor available. In 2015, it was the 3rd. "But hang on," my brain protested, "that guy wasn't using Vim, he was using VS Code." &lt;/p&gt;

&lt;p&gt;The solution to that mystery begins with the fact that Vim wasn't originally built for mac. Vim was built for the Amiga, the raddest looking computer of all time: &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%2Fcdn.arstechnica.net%2Fwp-content%2Fuploads%2F2015%2F07%2FScreen-Shot-2015-07-23-at-11.10.08-AM-640x387.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%2Fcdn.arstechnica.net%2Fwp-content%2Fuploads%2F2015%2F07%2FScreen-Shot-2015-07-23-at-11.10.08-AM-640x387.png" alt="Hot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just look at that floppy disk port. Vim was used in those days to edit your code inside of the command line in an efficient, legible manner. The setup (which works most fluidly with QWERTY keyboards but can totally be used with whatever keyboard you want) is designed to let you hop around the page without having to use the mouse. Vim has been ported for use with a ton of different devices at this point, and you can definitely use it as your primary text editor. It doesn't have all the bells and whistles you might find in a newer editor like Atom or Sublime but it does it's job and &lt;strong&gt;it does it damn well&lt;/strong&gt;. Ok, so next question:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3o85xIO33l7RlmLR4I/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o85xIO33l7RlmLR4I/giphy.gif" alt="Porque no los dos?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What my instructor was doing was &lt;em&gt;emulating&lt;/em&gt; the Vim commands with an extension in his particular text editor. According to him, most all text editors have some way of emulating Vim, because it's so OG. &lt;/p&gt;

&lt;p&gt;I added the emulator into my own VS Code extensions, but as a tribute to my programming elders, I decided to check out the Real Deal as well. I quickly asked Google where the Internet keeps its Vim. To my surprise, Google replied, &lt;/p&gt;

&lt;p&gt;"Look inside. It is you who has the Vim."&lt;/p&gt;

&lt;p&gt;Apparently Vim comes pre-installed with any Unix-based OS (such as Mac OS or Linux). I felt mildly invaded by this, but it did make using it pretty simple. To get started, you just have enter your terminal and open up a file with it using the command "vim filename.ext". &lt;/p&gt;

&lt;h2&gt;
  
  
  Learning the Vim Basics
&lt;/h2&gt;

&lt;p&gt;At first, I tried to wing it. While the advantages of being fluent in Vim are undeniable, the on-ramp for understanding what the hell is going on in Vim is a little steep without help, so I sought some tutorials instead. &lt;/p&gt;

&lt;p&gt;The most important thing to know is that Vim has two main modes: &lt;strong&gt;normal&lt;/strong&gt; and &lt;strong&gt;input&lt;/strong&gt;. Normal is the navigational mode, while input acts as more of a regular typing mode. You can toggle between the two modes by using the &lt;em&gt;i&lt;/em&gt; and &lt;em&gt;esc&lt;/em&gt; keys. The third mode, &lt;strong&gt;visual&lt;/strong&gt; mode, is used primarily for selection commands. &lt;/p&gt;

&lt;p&gt;The following super quick tips are some key examples that you can use while you are in Vim's normal mode: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;h&lt;/em&gt; and &lt;em&gt;l&lt;/em&gt; keys for left and right movement respectively &lt;/li&gt;
&lt;li&gt;The &lt;em&gt;k&lt;/em&gt; and &lt;em&gt;j&lt;/em&gt; keys for up and down movement respectively &lt;/li&gt;
&lt;li&gt;The &lt;em&gt;w&lt;/em&gt; key to skip to the next word&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;b&lt;/em&gt; key to skip to the previous word&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are an absolute %&amp;amp;$#-ton of other commands, but a bullet point hay day ain't great blog content. You can find a cheat sheet on Vim commands from &lt;a href="https://www.fprintf.net/vimCheatSheet.html" rel="noopener noreferrer"&gt;this super helpful page&lt;/a&gt;, and you can use &lt;a href="https://www.openvim.com/" rel="noopener noreferrer"&gt;this interactive Vim tutorial&lt;/a&gt; to begin your journey into navigational masterdom. &lt;/p&gt;

&lt;p&gt;More importantly, however, video games. You can actually &lt;a href="https://vim-adventures.com/" rel="noopener noreferrer"&gt;play your way to Vim literacy&lt;/a&gt; with this stellar, throwback-style adventure game which only allows you to use Vim commands. &lt;/p&gt;

&lt;p&gt;This little bit of history on Vim and keyboard navigation gave me a delightful wave of nostalgia and a deeper sense of understanding for what a Text Editor is and does. In reading through the articles and comments about Vim, you could really feel peoples' attachment to the program. It's easy to see how the soft spot was developed -- after 27 years, Vim has only had 8 total versions released, because it was just built so well in the first place. Now if you'll excuse me, I'm off to my VIM Adventures!&lt;/p&gt;

</description>
      <category>vim</category>
      <category>texteditors</category>
      <category>todayilearned</category>
    </item>
  </channel>
</rss>
