<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shemona Singh</title>
    <description>The latest articles on Forem by Shemona Singh (@singhshemona).</description>
    <link>https://forem.com/singhshemona</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%2F395770%2F08d447c4-4e8f-456d-a1d7-51e3c66de177.jpeg</url>
      <title>Forem: Shemona Singh</title>
      <link>https://forem.com/singhshemona</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/singhshemona"/>
    <language>en</language>
    <item>
      <title>Junior Developers vs Senior Developers</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 13 Mar 2022 17:19:43 +0000</pubDate>
      <link>https://forem.com/singhshemona/junior-developers-vs-senior-developers-3230</link>
      <guid>https://forem.com/singhshemona/junior-developers-vs-senior-developers-3230</guid>
      <description>&lt;p&gt;Sometime last year, I came across Julie Zhou's article on &lt;a href="https://medium.com/the-year-of-the-looking-glass/junior-designers-vs-senior-designers-fbe483d3b51e"&gt;&lt;strong&gt;Junior Designers vs Senior Designers&lt;/strong&gt;&lt;/a&gt;. I love it - she does a fantastic job of highlighting the thought process behind different levels of a role. And with the understanding I've gathered in my blossoming career, today I'd like to do the same for developers.&lt;/p&gt;

&lt;p&gt;I've had the privilege of working with exceptionally talented and thoughtful professionals since the start of my career. This might have intimidated me at first, but I quickly came to view them as mentors - a source of growth and exciting challenge. Much of my current skillset is influenced by them, but also quality time set aside in my day-to-day that's dedicated to &lt;a href="https://www.calnewport.com/blog/2020/07/03/a-deliberate-tribute/"&gt;&lt;strong&gt;deliberate practice&lt;/strong&gt;&lt;/a&gt;. As I reach a point in my career where I'm steadily being assigned more senior-level tasks, I can't help but look back at my thought processes when I first started off and compare them to today's.&lt;/p&gt;




&lt;h3&gt;
  
  
  1 - Grasp on Foundational Knowledge
&lt;/h3&gt;

&lt;p&gt;The tapestry of foundational knowledge for senior developers seem to be without a hole. The greater your understanding of how things come together or why something works the way it does, the greater your chances of building an optimal solution. In one of my first React projects, I wasn't sure how to get a component to update based off of a variable changing in another same-level component. I spent more time than I should have going down a rabbit hole of custom hooks, &lt;code&gt;useMemo&lt;/code&gt;, &lt;code&gt;useRef&lt;/code&gt;, and ended up with a hacky fix with useState. Looking back, it was clear that if I truly understood the component lifecycle and the reasoning behind some common React hooks, I would have known useEffect was built for the very purpose that I was attempting to solve for and I would have been done in just a few lines of code. If my fundamentals were clearer, I would have caught that earlier. Solid foundations are what contributes to razor sharp troubleshooting skills too...which leads me to my next point.&lt;/p&gt;

&lt;h3&gt;
  
  
  2 - Troubleshooting
&lt;/h3&gt;

&lt;p&gt;I find that when junior devs encounter a bug, they'll attempt to through every bit of knowledge they have in that arena until something sticks (and by sticks I mean until they have a different error). Seniors approach a bug more heuristically, taking the majority of the time to understand what the problem even is before jumping to a solution. They'll ponder on what the missing puzzle piece is. Once that information gap is realized, the understanding naturally leads them to possible solutions they can retrieve from their tight-knit tapestry of previous troubleshooting scenarios. &lt;/p&gt;

&lt;h3&gt;
  
  
  3 - Knowing When to Break the Rules
&lt;/h3&gt;

&lt;p&gt;A common beginners versus experts trope is the famous Picasso quote: "Learn the rules like a pro, so you can break them like an artist". Oftentimes when you're starting out, you're constantly seeking affirmation if the code you've produced checks off all the boxes. After some time, once you've gained the ability to make that judgement call yourself, you can bring that judgement into the process of coding itself. You know where it's ok to cut corners to postpone a requirement to version 2 because you know how version 1 will meet the expectations of stakeholders in the most important ways. This also ties into the laziness factor. There is a difference between being "efficiently lazy" in creating a speedier process to improve a future need versus being lazy as a conclusion of not understanding what cutting that corner will cost the team.&lt;/p&gt;

&lt;h3&gt;
  
  
  4 - Approaches to Problem Solving
&lt;/h3&gt;

&lt;p&gt;You might be wondering how this differs from my point on troubleshooting. When I say troubleshooting, I mean that there is a clear error to be unblocked or bug to be fixed. When I refer to understanding a problem, this could mean a project requirement, like &lt;em&gt;build a modal that pops up when you click on button X&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;My Operating Systems professor in college once told our class, "if you understand the problem, but you don't know how to solve it, you don't understand the problem." This stuck with me. If I ever came to a point when trying to make something work where it seemed every direction was a dead end, it was a sign for me to step back and refine my approach. Refining my approach always meant going back to my understanding of the problem. It can be tempting for many junior developers to begin coding as soon as they have a quick, abstract grasp of what the problem is asking and then figure out edge cases along the way. Senior folks have mastered understanding what a problem is going to entail, and with speed.&lt;/p&gt;

&lt;p&gt;A lovely example of this is when a project I was on involved building a text input with autocomplete ability. Upon seeing this requirement I was unsure of where to even begin. My teammate on the other hand, was able to build it in a few hours. Curious as to how, I asked him to walk me through his thought process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I need this text input to autocomplete...where else has there been an autocomplete in the company's products? Ah, yes the landing page of product XYZ.&lt;/li&gt;
&lt;li&gt;Dissect that autocomplete feature that's already in production. Open up the Network tab on Chrome DevTools and reload the page. What resources is it requesting? Looks like it's using resource ABC.&lt;/li&gt;
&lt;li&gt;Use sourcegraph (a tool to universally search through all code in the company) to find instances of components using those resources.&lt;/li&gt;
&lt;li&gt;Dissect those instances to see how they've built autocomplete.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While it might take a junior a few steps back into contemplating the problem once they've already dove deep into coding, seniors usually stick with the problem and then start once they fully grasp the scope. This helps them to build more reliable foundations with flexibility in mind, like what data should pass through which components as they have an idea of what else might come up if the direction changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  5 - Giving Estimates on Deadlines
&lt;/h3&gt;

&lt;p&gt;This ties in with point #4. Chances are, if you understand the problem better you're better equipped to know how long something will take to get done. This is helpful in kickoff meetings for a new project, pointing tickets in sprint planning, or even just any conversation with a stakeholder who's not an engineer. As the builder, heads obviously turn to you when a Product Manager asks, "So how long do we think this project will take?" or "Is end of June a viable target?" I'd always watch my fellow developers who were far senior to me with a look of mesmerization as they gave an answer consisting of the perfect balance between giving exact dates but leaving room for unexpected delays or dependencies. This is a skill that cannot really be "practiced" in a sense of an online course. It's something that comes with more development experience as well as time in the industry.&lt;/p&gt;

&lt;h3&gt;
  
  
  6 - Understanding Procedures in Software Beyond Coding
&lt;/h3&gt;

&lt;p&gt;This is a jab at the commonly argued differences between studying computer science in an academic sense and applying software engineering in industry. Much of what happens before or after you push your code is not quite discussed in college because you rarely work in environments where what you're developing is directly going to affect someone else's branch - considering you're usually working alone. Let's think about the pipeline. Senior developers have a strong grasp on steps like linting, testing and building. When you're thrown into a new project as a junior developer who's only ever worked on python assignments that require you to build a function for some outdated mathematical function, it can be frustrating to troubleshoot your way through a pipeline failure.&lt;/p&gt;




&lt;p&gt;Some of the points I've discussed here are ones I'm still practicing. Many are in your control, like picking up a new skill. But things like robust problem solving ability and confidence in scope understanding need time to develop. This is exactly why experiences like internships, freelancing and volunteering are &lt;strong&gt;invaluable&lt;/strong&gt; assets to sharpen your judgement. Strive to put yourself in situations that challenge you with people that inspire you. &lt;/p&gt;

&lt;p&gt;I know all too well this process of growth can feel maddening, like taking 2 steps forward and 3 steps back. Writer Alice Walker penned the discomfort of growth beautifully:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Some periods of our growth are so confusing that we don’t even recognize that growth is happening. We may feel hostile or angry or weepy and hysterical, or we may feel depressed. It would never occur to us, unless we stumbled on a book or a person who explained to us, that we were in fact in the process of change, of actually becoming larger than we were before. Whenever we grow, we tend to feel it, as a young seed must feel the weight and inertia of the earth as it seeks to break out of its shell on its way to becoming a plant. Often the feeling is anything but pleasant. But what is most unpleasant is the not knowing what is happening. Those long periods when something inside ourselves seems to be waiting, holding its breath, unsure about what the next step should be... for it is in those periods that we realize that we are being prepared for the next phase of our life and that, in all probability, a new level of the personality is about to be revealed."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For all the junior developers out there: May the pressure to perform be lifted. I hope this article will help you to see there are key areas you can productively focus your energy. As for the rest, time will tell. &lt;/p&gt;




&lt;p&gt;Shoutout to ShutMeLoud for the hilarious gif on the cover. Be sure to &lt;a href="https://youtu.be/hJbjLaCJd6g"&gt;&lt;strong&gt;check out the video&lt;/strong&gt;&lt;/a&gt; for the full clip.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>seniors</category>
      <category>career</category>
    </item>
    <item>
      <title>Animated Progress Bar With Only SVGs</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 06 Jun 2021 20:33:41 +0000</pubDate>
      <link>https://forem.com/singhshemona/animated-progress-bar-with-only-svgs-2k0j</link>
      <guid>https://forem.com/singhshemona/animated-progress-bar-with-only-svgs-2k0j</guid>
      <description>&lt;p&gt;Have you ever wondered how to make &lt;a href="https://dribbble.com/shots/3198381-Progress?utm_source=Clipboard_Shot&amp;amp;utm_campaign=Volorf&amp;amp;utm_content=Progress&amp;amp;utm_medium=Social_Share&amp;amp;utm_source=Clipboard_Shot&amp;amp;utm_campaign=Volorf&amp;amp;utm_content=Progress&amp;amp;utm_medium=Social_Share"&gt;one of those neat little progress bars&lt;/a&gt; you see in so many loading interfaces?&lt;/p&gt;

&lt;p&gt;A little while back I talked about how we can build the other category of loading animations, a spinner. Today, I want to show how you can use the power of SVGs yet again to create the simplest progress bar you've ever seen to date. No double layered divs filling, no glitches. Plain ol' React and scss.&lt;/p&gt;

&lt;p&gt;What would be needed for something like this? Much like the loading spinner we need some data to wait on. I'll fake that part for tutorial purposes. While we're waiting, we can launch a keyframe animation to fill the svg bar. Is the waiting done? Put a 'completed!' message of your choosing. &lt;/p&gt;

&lt;p&gt;Ok, so what does this look like in code speak. For the structure we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Progress&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hasLoaded&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;hasLoaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1550&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`progress &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;progress-success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;progress-failure&lt;/span&gt;&lt;span class="dl"&gt;"&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="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ending-msg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Loaded&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;svg&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;alert&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;aria&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;live&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;assertive&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;rect&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;border&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;rx&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;15&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;ry&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;15&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;rect&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;filling&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;rx&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;15&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;ry&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;15&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/svg&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Brief overview of what's happening:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We create a state called &lt;code&gt;loading&lt;/code&gt; to check whether we are waiting for data or the waiting has ended. This only need be a boolean, because either the progress bar is loading or not. It initially starts off as false, because we haven't requested any data yet.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt; is where we can check the loading state and show our progress bar animation if the loading state is true. The sample fetch request is just there to illustrate this point, as you will likely be waiting on some event when using a progress bar. The &lt;code&gt;setTimeout&lt;/code&gt; is just to mock the waiting time, in reality you would not need this since the program would naturally be waiting on whatever it is you need the bar for.&lt;/li&gt;
&lt;li&gt;If you do end up using the setTimeout, you might be wondering why I choose 1550 milliseconds. This number depends on however long the &lt;code&gt;fillBar&lt;/code&gt; animation is running for, which we will see in the styles. Basically, it needs to be at least 50 milliseconds longer than the &lt;code&gt;fillBar&lt;/code&gt; animation. You can update this or remove the &lt;code&gt;setTimeout&lt;/code&gt; entirely.&lt;/li&gt;
&lt;li&gt;In terms of what we end up rendering, we have one div that holds everything. Inside, we have a conditional for our two states: If the content has loaded already, print an ending message. If the content has not yet loaded, then keep the progress bar animation going.&lt;/li&gt;
&lt;li&gt;The bar is split into two rectangle svgs, one defining its outline and the other defining its border. We also set the radius to make it a little more curved and define attributes like &lt;code&gt;height&lt;/code&gt; and &lt;code&gt;width&lt;/code&gt;. Everything else will be handled in the styles.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Speaking of which, let's make the magic happen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.progress-success&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.ending-msg&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&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="nc"&gt;.progress-failure&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.ending-msg&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&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="nc"&gt;.progress&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.border&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="na"&gt;stroke-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="na"&gt;stroke&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="na"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nc"&gt;.filling&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="na"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fillBar&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="mi"&gt;.5s&lt;/span&gt; &lt;span class="n"&gt;ease-in-out&lt;/span&gt; &lt;span class="m"&gt;0s&lt;/span&gt; &lt;span class="nb"&gt;normal&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="n"&gt;forwards&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="nt"&gt;fillBar&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nt"&gt;0&lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;30&lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;80px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;70&lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;120px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;100&lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are doing a good amount of setting colors, border widths, etc. But what's actually making the animation run is the &lt;code&gt;fillBar&lt;/code&gt; keyframe we have running on the svg with the className of &lt;code&gt;.filling&lt;/code&gt;. In &lt;code&gt;fillBar&lt;/code&gt;, we define the progression of how we want to fill the progress bar by simply increasing the width of the svg, which by being inside of the svg with a class name of &lt;code&gt;.border&lt;/code&gt; gives the illusion that it's filling the bar. The actual speed and transition is defined by the way we call the keyframe, seen in the &lt;code&gt;animation&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;Now let's see it completed. Click on the 'rerun' button on the bottom right to see it again.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/singhshemona/embed/jOBxYgR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Now you know how to make a smooth progress bar with just a bit of smart svg manipulation. Play around with timings and styles to really jazz up your animation! &lt;/p&gt;

</description>
      <category>animation</category>
      <category>svgs</category>
      <category>scss</category>
      <category>react</category>
    </item>
    <item>
      <title>How to make the Firebase Realtime Database Rules Secure for a Blog</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sat, 13 Mar 2021 14:21:29 +0000</pubDate>
      <link>https://forem.com/singhshemona/how-to-make-the-firebase-realtime-database-rules-secure-for-a-blog-1g45</link>
      <guid>https://forem.com/singhshemona/how-to-make-the-firebase-realtime-database-rules-secure-for-a-blog-1g45</guid>
      <description>&lt;p&gt;Last year I revamped &lt;a href="https://shemonasingh.com/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt; with a new design and a Firebase Realtime Database. It was a fairly smooth ride with some minor complications along the way. As for setting up the database, I found some awesome tutorials that eventually helped me filter it all down to &lt;a href="https://dev.to/singhshemona/reading-and-writing-from-a-database-with-react-simplified-1mhe"&gt;some pretty simple steps&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The blog was running as planned, until one day I received an email from the Firebase team telling me the rules I had set for my database were insecure. &lt;/p&gt;

&lt;p&gt;I was confused, so I did what I usually do when I try to get to the bottom of things: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I read through a bunch of articles.&lt;/li&gt;
&lt;li&gt;Played trial and error with variations of rules.&lt;/li&gt;
&lt;li&gt;Posted on the Firebase community chat.&lt;/li&gt;
&lt;li&gt;Went back and forth with the lovely Firebase support team. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, after weeks of those daily emails, they stopped. Let's take a look at what database setup and rules finally secured them. &lt;/p&gt;

&lt;p&gt;The only information my database stores is in regards to the posts. It looks 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%2Fkx4d6uvzjgjlsbwe0cfw.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%2Fkx4d6uvzjgjlsbwe0cfw.png" alt="Realtime Database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you've looked through the documentation for &lt;a href="https://firebase.google.com/docs/rules" rel="noopener noreferrer"&gt;Firebase's security rules&lt;/a&gt;, you may be familiar with the read / write set up. My website is a public blog, with no authentication or collecting data from the user of any kind. So, I only needed read requests to be allowed, but any write requests would be denied as I maintain the adding of new blog posts via uploading a new json file in the console. &lt;/p&gt;

&lt;p&gt;So, my rules look 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%2F8qkhf88ancyjh6fwrv2e.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%2F8qkhf88ancyjh6fwrv2e.png" alt="Database rules"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Obviously, my &lt;code&gt;auth.uid&lt;/code&gt; is greyed out. You might be wondering where I got that id from. I opened up my project on the Firebase console, clicked on 'Authentication' on the left menu and set up one user - myself. Under the 'User UID' column you will find the &lt;code&gt;auth.uid&lt;/code&gt;. However, as I mentioned I don't have authentication set up for my website, so you may be able to get by without doing this step. If you read on, you'll see the biggest security step was not this &lt;code&gt;auth.uid&lt;/code&gt;, but the addition of &lt;code&gt;posts&lt;/code&gt; in my rules. Try out both and see what happens.&lt;/p&gt;

&lt;p&gt;It's important to note I then also had to change the way I requested data from the database in my &lt;code&gt;useEffect()&lt;/code&gt; hook. All I had to do was add the word 'posts' after the &lt;code&gt;/&lt;/code&gt; when requesting the slug:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;child&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/posts/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;slug&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="c1"&gt;// ...rest of code&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you'd like to see the rest of my useEffect() hook and how it all works together to retrieve and place data, check out my post on &lt;a href="https://dev.to/singhshemona/reading-and-writing-from-a-database-with-react-simplified-1mhe"&gt;Reading and Writing from a Database with React&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Previously, I was doing &lt;code&gt;.child('/${slug}')&lt;/code&gt;. When speaking to the Firebase support team, I was informed that requesting from the root node of my database is what was causing the security vulnerability. So, adding another node called 'posts' between the root level and the posts secured it. Before, I didn't have the &lt;code&gt;posts&lt;/code&gt; node in the rules or database and it went straight to all the posts objects.&lt;/p&gt;

&lt;p&gt;And that's all! Now my rules are secure. 🎉 I feel better about not contributing to security leaks but better yet I no longer get those worrisome emails.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Note: If you have authentication set up or require extensive database layering and security rules, &lt;a href="https://howtofirebase.com/firebase-security-rules-88d94606ce4a" rel="noopener noreferrer"&gt;this is a good blog post&lt;/a&gt; to refer to.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>database</category>
      <category>security</category>
      <category>react</category>
    </item>
    <item>
      <title>The Easiest Way to Animate a Loading Spinner </title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 28 Feb 2021 15:52:01 +0000</pubDate>
      <link>https://forem.com/singhshemona/how-to-animate-an-svg-into-a-loading-spinner-4g40</link>
      <guid>https://forem.com/singhshemona/how-to-animate-an-svg-into-a-loading-spinner-4g40</guid>
      <description>&lt;p&gt;While on a quest to learn how to build some of the most commonly requested animations by designers, the loading spinner seems like a rite of passage.&lt;/p&gt;

&lt;p&gt;This time around, I wanted to see if I could &lt;a href="https://dev.to/singhshemona/breaking-down-svgs-1201"&gt;use the awesome power of svgs&lt;/a&gt; to draw out a circle then animate that circle. This could be a lot cleaner than attempting to animate borders or place rotating circles on top of other circles in CSS. &lt;/p&gt;

&lt;p&gt;We will be building today's spinner here with React. Thinking in terms of states, there are two main ones. We are either:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Waiting for something - show the spinner&lt;/li&gt;
&lt;li&gt;That something has happened - no longer show the spinner&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To make this feel more realistic, we will have the spinner wait for a response from the Fetch api. There are plenty of open apis for us to request from for the sake of this tutorial.&lt;/p&gt;

&lt;p&gt;Take a look at the set up for our component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Loading.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Loading&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hasLoaded&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;hasLoaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1100&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;spinner-container&lt;/span&gt;&lt;span class="dl"&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;loading&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Content&lt;/span&gt; &lt;span class="nx"&gt;has&lt;/span&gt; &lt;span class="nx"&gt;loaded&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;svg&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;spinner&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;alert&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;aria&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;live&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;assertive&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;circle&lt;/span&gt; &lt;span class="nx"&gt;cx&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;30&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;30&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;20&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;circle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/svg&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Let's walk through what's going on here.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, we set up the two states I mentioned at the beginning. The only two states the spinner can be in: either we are waiting for something to happen, or it has already happened. A simple boolean does the trick.&lt;/li&gt;
&lt;li&gt;The handy &lt;code&gt;useEffect&lt;/code&gt; hook is where we can handle what it is that we're waiting for. It's likely you'll be waiting for some data to return, so I've set up a sample fetch request. You may also notice I have it wrapped inside of a &lt;code&gt;setTimeout&lt;/code&gt;. This is because the information comes far too fast for us to see the spinner in action, so for the purposes of delaying our response I've added a &lt;code&gt;setTimeout&lt;/code&gt; that you're welcome to adjust in order to see the spinner for longer. I have it set to 1100 milliseconds so that we can see the spinner for at least a second. In reality, you might not need a &lt;code&gt;setTimeout&lt;/code&gt; since the data you'll be requesting will likely take it's own time.&lt;/li&gt;
&lt;li&gt;In the return of the &lt;code&gt;useEffect&lt;/code&gt;, I clean up the &lt;code&gt;setTimeout&lt;/code&gt; like the responsible developer I am. 😇&lt;/li&gt;
&lt;li&gt;Now for the actual component. We have one &lt;code&gt;div&lt;/code&gt; that holds everything. Inside, we set our two states: If the content has loaded already, print something that tells us this. If the content has not yet loaded, this is where we render our animated spinner.&lt;/li&gt;
&lt;li&gt;The spinner is a simple &lt;code&gt;circle&lt;/code&gt; tag wrapped inside of an &lt;code&gt;svg&lt;/code&gt; tag. We define some attributes like height and width, as well as those that will make it accessible like &lt;code&gt;aria-live&lt;/code&gt; and &lt;code&gt;role&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ok! We have the skeleton of a spinner. But, there's nothing to see yet. The styles are where the actual magic happens. Let's take a look at them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.spinner-container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nc"&gt;.spinner&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;-90deg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;60px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;60px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;.circle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;stroke-linecap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;round&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="na"&gt;stroke-dasharray&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;360&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="na"&gt;stroke-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="na"&gt;stroke&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="na"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;spin&lt;/span&gt; &lt;span class="mi"&gt;.6s&lt;/span&gt; &lt;span class="n"&gt;ease-in-out&lt;/span&gt; &lt;span class="m"&gt;0s&lt;/span&gt; &lt;span class="nb"&gt;normal&lt;/span&gt; &lt;span class="n"&gt;infinite&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;@keyframes&lt;/span&gt; &lt;span class="nt"&gt;spin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nt"&gt;from&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;stroke-dashoffset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;360&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;stroke-dashoffset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's walk through the styles.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We have the &lt;code&gt;.spinner-container&lt;/code&gt; wrapped around everything. Pretty straightforward.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;svg&lt;/code&gt; gets a class of &lt;code&gt;.spinner&lt;/code&gt; with height and width specified as well as the rotation that will occur while being animated.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;.circle&lt;/code&gt; class is where I first define some stylistic qualities to the actual circle and then the &lt;code&gt;animation&lt;/code&gt; property is the key to it's movement. I set it to be the keyframe animation named &lt;code&gt;spin&lt;/code&gt;, which is simply pulling the filling of the circle forward.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is what it all looks like in action. Be sure to hit the 'rerun' button on the bottom right.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/singhshemona/embed/bGBaZYz?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Voila! Just a few lines of scss to make the spinner come to life. Years ago before I knew this handy tactic of manipulating and animating the fill of svgs, I had built a different spinner. It used bulky, confusing styles to make the drawing of the border for a circle div seem fluid. &lt;/p&gt;

&lt;p&gt;Makes you question coding patterns you might be unconsciously following now that could be done more efficiently. 🤔 &lt;/p&gt;

</description>
      <category>animations</category>
      <category>react</category>
      <category>scss</category>
      <category>svg</category>
    </item>
    <item>
      <title>What's Really Going On In A Chrome Extension</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 14 Feb 2021 16:36:18 +0000</pubDate>
      <link>https://forem.com/singhshemona/what-s-really-going-on-in-a-chrome-extension-4na1</link>
      <guid>https://forem.com/singhshemona/what-s-really-going-on-in-a-chrome-extension-4na1</guid>
      <description>&lt;p&gt;A few weeks back, I set out to create my own chrome extension with Typescript and React. I had a rough understanding from a talk I had attended at work some years back. So, I approached it assuming it was going to be as simple as getting back in the swing of my previous understanding. &lt;/p&gt;

&lt;p&gt;It was in fact pretty simple to get started and even more easy to render visible changes. But there was something else that took up most of the time I spent that day setting up - understanding the anatomy of a chrome extension. That is, there seemed to be a few key files but I wasn't sure which one to change when. I browsed several articles and youtube tutorials but nothing seemed to click. After a while, it was as if my brain had picked a little bit from each tutorial and started to make sense of it all. To further solidify my understanding, I begin to draw out a representation of the anatomy. This diagram would be updated upon testing if the assumptions I had made were in fact correct.&lt;/p&gt;

&lt;p&gt;Turns out they were, and this diagram is what I'd like to break down today.  &lt;/p&gt;

&lt;p&gt;The file names I'm using come from a typescript chrome extension template I started with. When attempting to build an extension, do yourself a favor and don't start from scratch. &lt;a href="https://github.com/hindmost/cra-template-complex-browserext-typescript" rel="noopener noreferrer"&gt;Start with this&lt;/a&gt;, or a different template of your choosing. Most of my annoyances involved setting up the precious &lt;code&gt;manifest.json&lt;/code&gt; file, but starting from a template helps to avoid all of this. Before proceeding, I recommend having either &lt;a href="https://github.com/hindmost/cra-template-complex-browserext-typescript" rel="noopener noreferrer"&gt;the template repo&lt;/a&gt; up or a chrome extension project you have already started open on your choice of text editor. This is so when I mention a file name it'll be easier to follow along.  &lt;/p&gt;

&lt;p&gt;If &lt;a href="https://github.com/hindmost/cra-template-complex-browserext-typescript" rel="noopener noreferrer"&gt;you're on the repo&lt;/a&gt;, take a look at the contents of the &lt;code&gt;src&lt;/code&gt; and &lt;code&gt;public&lt;/code&gt; folders. Identify the files: &lt;code&gt;background.ts&lt;/code&gt;, &lt;code&gt;content.ts&lt;/code&gt;, &lt;code&gt;index.tsx&lt;/code&gt;, and &lt;code&gt;options.tsx&lt;/code&gt;. If you're using a different template they might be named differently, but that's a pretty standard naming convention for an extension. (Sometimes &lt;code&gt;index&lt;/code&gt; can be named &lt;code&gt;popup&lt;/code&gt;). These are the four vital bones of the extension. Let's break down each bone and help grasp what they support.&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%2Fi%2Fkmorlpn5zklg5l5t4l5n.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%2Fi%2Fkmorlpn5zklg5l5t4l5n.png" alt="Anatomy of a chrome extension"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the purposes of explaining this diagram, let's create an example to work with. Let's say you want to make a chrome extension that makes it rain images of coffee cups every time someone is on the Starbucks website. Let's dissect the diagram with this example in mind:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The labels in green are for the 4 files I mentioned earlier. &lt;/li&gt;
&lt;li&gt;The labels in black are for what each webpage represents.&lt;/li&gt;
&lt;li&gt;The webpage on the top left I labeled as 'specified in manifest.json' would be the Starbucks homepage. We would specify the url of Starbucks in the &lt;code&gt;manifest.json&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The webpage on the bottom left represents all the webpages that are not the Starbucks webpage. We don't have to specify these urls anywhere, it's just important to note what file you would have to edit if you want your extension to do something no matter what webpage you are on.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note that putting something into each of these 4 files is optional. Say you set up your falling coffee cups script on &lt;code&gt;content.js&lt;/code&gt;, that will trigger on the Starbucks page. If that's all you want to do and don't want anything to happen on webpages that are not Starbucks, feel free to leave &lt;code&gt;background.ts&lt;/code&gt; empty. &lt;/p&gt;

&lt;p&gt;The realization of each file's purpose and knowing what to change when was the single biggest factor in continuing the successful development of an extension. It's hard to get something to trigger properly when you're not sure which file it belongs in. My hope is that you come across this diagram in your journey of chrome extension tutorials, and everything will finally click the way it did for me.&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>chrome</category>
      <category>extension</category>
    </item>
    <item>
      <title>Should learning be a means to an end?</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Fri, 29 Jan 2021 20:23:59 +0000</pubDate>
      <link>https://forem.com/singhshemona/should-learning-be-a-means-to-an-end-4c0</link>
      <guid>https://forem.com/singhshemona/should-learning-be-a-means-to-an-end-4c0</guid>
      <description>&lt;p&gt;For a few months now, when it came to adding a new skill to my technical tool belt I was under the school of thought that the best way to do so was with a project. &lt;/p&gt;

&lt;p&gt;Well maybe I shouldn't say best, but at least I was highly biased towards that line of reasoning. I went down a whirlwind of learning via the &lt;a href="https://jamesclear.com/deliberate-practice-theory"&gt;deliberate practice strategy&lt;/a&gt; but without a clear goal. Eventually I reached a point where I didn't feel any sense of progress. With any habit, you want to make some noticeable move to the needle. After all, with no project how would I even work through the pile of information there is to learn and find what interests me? Why pick up a new technical skill for the unknown sake of it when I could build something that excited me and learn as a natural side effect?&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;My bias ended when that thought process didn't work so well in a project. A few months ago I had an idea for an app that would solve a recurring problem of mine. I had a long term vision, but to prevent my vision getting in the way of an MVP I decided on some key features that absolutely needed to be there to get the point across. I came up with 3 main screens, each of which had 1 or 2 core functionalities. As someone with a background in web development (like that made a difference), this seemed attainable in at least under a month. Right?&lt;/p&gt;

&lt;p&gt;Well, turns out it was attainable in about 2 weeks. But like all projects, unpredictable obstacles arose. All of which I was able to debug my way through just fine. Except for one. One that opened my eyes to further nightmares of mobile development. &lt;/p&gt;

&lt;p&gt;Thanks to fellow techie friends I started off with the knowledge that learning mobile development might start off feeling like working with an angry dragon. I acknowledged this and continued working. Once I reached 3 screens, I felt like I had conquered the dragon. It wasn't until I needed to implement the next feature that I realized I had just been playing with the tail of the dragon and in fact the dragon was yet to be woken. Being in the weeds of the errors gave me the confidence to recognize the size of the dragon.&lt;/p&gt;

&lt;p&gt;This led me to question my aforementioned approach in learning something new. If I had just dabbled in mobile development for the sake of it, could I have foreseen this bug occurring? When picking up a new skill in the field of software, is it better to approach it with some conclusive means? Or does one learn better when there is no conclusion in sight, and rather for the pure joy of being immersed in the knowledge? &lt;/p&gt;

&lt;p&gt;This naturally led me to brainstorm some pros and cons for each:&lt;/p&gt;

&lt;h3&gt;
  
  
  Learning with an end goal in mind
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;End with something to show for your work&lt;/li&gt;
&lt;li&gt;Motivation might be higher? With the understandable clarity&lt;/li&gt;
&lt;li&gt;Some structure to your learning, you know what trees to traverse in the forest of knowledge for that field &lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;You can get caught up in the product, not the learning (build something small article about side projects)&lt;/li&gt;
&lt;li&gt;If you don't reach the goal, it's easy to adapt the negative mentality that failure of the product is failure in the field&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Learning just for the sake of learning
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;No pressure, no expectations&lt;/li&gt;
&lt;li&gt;Helps you connect what your learning to other things you've learning because by nature learning is the art of connection&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Can easily forget what you've learned if you don't revisit it for some time&lt;/li&gt;
&lt;li&gt;Might be confusing where to start&lt;/li&gt;
&lt;li&gt;Can fall into the habit of passive consumption without active implementation - the phase where you actually learn&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Discuss
&lt;/h3&gt;

&lt;p&gt;I recently came across Paul Graham's &lt;a href="http://www.paulgraham.com/hs.html"&gt;What You'll Wish You'd Known&lt;/a&gt;. In it, he had a fantastic quote about recognizing the scope of where your learning can take you: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Writing novels is hard. Reading novels isn't. Hard means worry: if you're not worrying that something you're making will come out badly, or that you won't be able to understand something you're studying, then it isn't hard enough. There has to be suspense.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I suppose then, the answer is not that you should enroll in either of the schools of thought, but rather that learning in general should strain you in the best way possible. If that's not happening, whether you're building something or taking an online class, it's unlikely you're learning.&lt;/p&gt;

&lt;p&gt;When you want to dive into a new field of tech, are you more likely to follow one of these schools of thought? Maybe you approach each scenario as a standalone battle, that requires a mixture of both schools? That seems to be where I'm leaning at the moment.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Reading and Writing from a Database with React (simplified)</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Fri, 15 Jan 2021 15:31:08 +0000</pubDate>
      <link>https://forem.com/singhshemona/reading-and-writing-from-a-database-with-react-simplified-1mhe</link>
      <guid>https://forem.com/singhshemona/reading-and-writing-from-a-database-with-react-simplified-1mhe</guid>
      <description>&lt;p&gt;The ability to make read and write commands from a database is one of the most standard, necessary skill for any backend developer. So naturally, when I began to venture into the world of building &lt;a href="https://dev.to/singhshemona/learning-nodejs-part-2-server-side-and-dynamic-sites-4a0h"&gt;dynamic websites&lt;/a&gt; it was one of the first requirements that came up. When I attempt to picture logistically the flow of what reading and writing from a database must be, it feels simple. My only hope is that it's as simple in practice. &lt;/p&gt;

&lt;p&gt;It is.&lt;/p&gt;

&lt;p&gt;Well, I should clarify - I'm using Firebase. Why? It has one of the more generous free plans amongst the database land, and as a tinkerer who is never quite sure what restrictions some of the spaces I'm working in have, it made sense that I choose Firebase. I went through several awesome tutorials (two of my favorites linked at the bottom of this post), and many of which had some step that either did not work for me or were missing some vital feature. This tutorial is going to be the most barebones version of my collection of steps that ended up working. &lt;/p&gt;

&lt;p&gt;Any frustrations I had from the wonderful tutorials performing similar tasks stemmed from not being embarrassingly simple to do. Most of them had an end-UI goal in mind, which the building out of added fluff to the parts of the tutorial I was really trying to get at. &lt;strong&gt;Therefore in this post, we're not building anything. We are simply learning to send some data to a database and then retrieve that data.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1 - Set up your account
&lt;/h3&gt;

&lt;p&gt;If you go to the &lt;a href="https://console.firebase.google.com/" rel="noopener noreferrer"&gt;Firebase console&lt;/a&gt; and you don't see an option to add a project:&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%2Fi%2Fltegevkvn815opuhu8v0.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%2Fi%2Fltegevkvn815opuhu8v0.png" alt="Option to add project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then that means you aren't yet set up with an account. Pretty hard to go on without doing this part. So, let's get you an account. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://console.firebase.google.com/" rel="noopener noreferrer"&gt;Sign up with an existing Google account&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Now you should be led to your console. Click the "Add project" button (from the above image) and follow through with the default settings it provides.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  2 - Save your configuration details
&lt;/h3&gt;

&lt;p&gt;In order for you to talk to your database, you need to get some special configuration details that the database can then authorize your attempted connection with. On approval of these details, the database can then spill the details you request. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click on the project you just made. You should be seeing a dashboard of sorts that provides all kinds of insights. On the left, you'll see a menu. Under 'Build', click on 'Realtime Database'. &lt;/li&gt;
&lt;li&gt;When you scroll down, you'll see a white box that says 'There are no apps in your project'. Click on the &lt;code&gt;&amp;lt;/&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Name the app what you'd like. Leave the hosting option unchecked and click on 'Register app'. Follow the default prompts. &lt;/li&gt;
&lt;li&gt;You're going to get some HTML with a variable called 'firebaseConfig'. These are your config details. In your code editor of choice, open your project (or create a new React project to follow along with this tutorial) and create a file called &lt;code&gt;config.js&lt;/code&gt; (or whatever you'd like to name your config details). Paste the following into that file:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Firebase&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;firebase&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;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;authDomain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;databaseURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;storageBucket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;messagingSenderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;appId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;measurementId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="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="nx"&gt;Firebase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initializeApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&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;db&lt;/span&gt; &lt;span class="o"&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;database&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Go back to that HTML that Firebase gave you. If you can't find it, it's in your project settings. Fill out the empty strings in the &lt;code&gt;config&lt;/code&gt; object you just made with the corresponding details from the &lt;code&gt;firebaseConfig&lt;/code&gt; object.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  3 - Make a call to the database
&lt;/h3&gt;

&lt;p&gt;Now that you have what you need to be authorized, you can set up a connection to your database so it knows you want to be heard. But, you need a package to help you do this. So first install the firebase package with the following command: &lt;code&gt;npm install firebase&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  4 - Now I want to...send some data
&lt;/h3&gt;

&lt;p&gt;Since our database is empty, it makes sense that we first want to put something into it. Firebase works with json files, so you can just make a json file containing the data you'd like and upload it to your database with the 'import json' button. You can find this button by clicking on 'Realtime Database' on the left menu and selecting the 3 vertical dots on the top right of your database.&lt;/p&gt;

&lt;p&gt;But, that's no fun. Also, it's not how you would work with a database in actuality. So, let's pretend some event just occurred and we want to let Firebase know to store a piece of information resulting from it. I've outlined below how this would be done. To make sense of it, follow the code in the order of the steps provided:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="c1"&gt;// 1 - Import your db config&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;db&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;/config.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// 2 - Let's start building our component, we're giving it&lt;/span&gt;
&lt;span class="c1"&gt;// the name SendData&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SendData&lt;/span&gt; &lt;span class="o"&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="c1"&gt;// 3 - We'll set up two states, the values of which will &lt;/span&gt;
&lt;span class="c1"&gt;// be sent to the database&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setNum&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="c1"&gt;// 5 - This is where the magic happens. When the 'Send to&lt;/span&gt;
&lt;span class="c1"&gt;// Database' button is clicked, we tell the database we &lt;/span&gt;
&lt;span class="c1"&gt;// imported in step #1 as db to push to its root (/) whatever &lt;/span&gt;
&lt;span class="c1"&gt;// values we have in our 'text' and 'num' states. You might be &lt;/span&gt;
&lt;span class="c1"&gt;// wondering what 'number' and 'words' are - they are the &lt;/span&gt;
&lt;span class="c1"&gt;// names of the keys that will be paired with the values from &lt;/span&gt;
&lt;span class="c1"&gt;// our state. You can change them to what you'd like  &lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;send&lt;/span&gt; &lt;span class="o"&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ref&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;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; 
      &lt;span class="na"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;words&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="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="c1"&gt;// 4 - A simple form (yes I know it's not accessible, bare &lt;/span&gt;
&lt;span class="c1"&gt;// with me) that will store the value of the number and any &lt;/span&gt;
&lt;span class="c1"&gt;// words you input into the states we setup in step #3. The &lt;/span&gt;
&lt;span class="c1"&gt;// button runs the 'send' function (see step #5) when it's clicked&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;getNum&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setNum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getNum&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt; &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Enter a number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;getText&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getText&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt; &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Enter some text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Send&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;Database&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&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;Try playing around with this component, sending different keys and values. Maybe make different state types altogether.&lt;/p&gt;

&lt;p&gt;Assuming you followed steps #1-4 with success, upon clicking that 'Send to Database' button the data you enter will now show up in your Firebase console under 'Realtime Database'. Try entering and submitting more values and see how your database fills up! If you don't see anything, be sure to refresh the Firebase page.&lt;/p&gt;

&lt;h3&gt;
  
  
  5 - Now I want to....get some data
&lt;/h3&gt;

&lt;p&gt;Now we have a filled database - yay! How about if we wanted to see all the values in this database? Let's add another component that does just that. This time, we'll use the &lt;code&gt;useEffect&lt;/code&gt; hook to establish a connection to the database once the component mounts, and let Firebase know what we want within there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1 - Import the usual statements &lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;db&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;../../config.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// 2 - Now we're building a component that gets the data, so &lt;/span&gt;
&lt;span class="c1"&gt;// we'll call it GetData&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;GetData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="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;// 3 - Set up a state that will hold the data we receive from &lt;/span&gt;
&lt;span class="c1"&gt;// the database in useEffect(). This will make it easy for us &lt;/span&gt;
&lt;span class="c1"&gt;// to just read from this state. It's going to give us back an &lt;/span&gt;
&lt;span class="c1"&gt;// object, so we'll initialize the state as an empty object &lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;

&lt;span class="c1"&gt;// 4 - Here, we're saying at the root (/) of the database, get &lt;/span&gt;
&lt;span class="c1"&gt;// a snapshot of all the values you see. Set the result of&lt;/span&gt;
&lt;span class="c1"&gt;// this attempt into a variable named 'info'. Since 'info' is &lt;/span&gt;
&lt;span class="c1"&gt;// an object, we'll use the spread operator to pass all of &lt;/span&gt;
&lt;span class="c1"&gt;// it's values to a variable called 'allInfo', and then update &lt;/span&gt;
&lt;span class="c1"&gt;// our state to be the value of 'allInfo'&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ref&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;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;querySnapShot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;querySnapShot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;val&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;querySnapShot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;val&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;allInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;allInfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="c1"&gt;// 5 - We potentially have something inside of our 'data' &lt;/span&gt;
&lt;span class="c1"&gt;// state. I say 'potentially', because something could go &lt;/span&gt;
&lt;span class="c1"&gt;// wrong and we might not be able to read from the database. &lt;/span&gt;
&lt;span class="c1"&gt;// If this happens, our 'data' state will be empty. So, let's &lt;/span&gt;
&lt;span class="c1"&gt;// use a [conditional operator](https://reactjs.org/docs/conditional-rendering.html#inline-if-else-with-conditional-operator) to first test if it's empty and give our reader a message. &lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no data to show here!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="c1"&gt;// 6 - Now for the 'assuming we have some data' part. Since &lt;/span&gt;
&lt;span class="c1"&gt;// it's an object, we can use the following syntax to parse &lt;/span&gt;
&lt;span class="c1"&gt;// through it (much like we can loop through an array with &lt;/span&gt;
&lt;span class="c1"&gt;// map()) and spit out each value of the keys we set earlier &lt;/span&gt;
&lt;span class="c1"&gt;// in step #4, 'number' and 'words'.&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&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;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&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;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;words&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will render on the page a list of all of the 'numbers' along with their 'words' that you submitted through the form in step #4. If you skipped step #4 and just uploaded a json file, then it will still work - just be sure that you change 'number' and 'words' to be whatever keys you set in your database.&lt;/p&gt;

&lt;p&gt;There are many tiny details throughout this setup that can go haywire, such as the specific values you use in your database, getting your configuration correct, TypeScript-specific issues and much more. It's the reason why I wasn't able to follow just one tutorial. Thus, I've tried many variations of making a simple read and write system and finally boiled it down to the above code. I'm hoping this tutorial helps drown out the noise by filtering down to the absolute basics what it takes to interact with a Firebase database.&lt;/p&gt;

&lt;p&gt;If you have specific problems that come up while going through this tutorial, feel free to message me and I'd be happy to help debug. I've probably come across it as well. 😅&lt;/p&gt;




&lt;p&gt;Thank you to the following posts that were a part of the journey in making sense of working with Firebase. There were several more, but these brought me the furthest along:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://ashleemboyer.com/react-firebase-blog-01" rel="noopener noreferrer"&gt;Build a React &amp;amp; Firebase Blog Site&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.logrocket.com/storing-retrieving-data-react-native-apps-firebase/" rel="noopener noreferrer"&gt;Storing and retrieving data for React Native apps with Firebase&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks to &lt;a href="https://www.lynda.com/SQL-Server-tutorials/Database-Fundamentals-Administration/385695-2.html" rel="noopener noreferrer"&gt;Lynda.com&lt;/a&gt; for the cover photo.&lt;/p&gt;

</description>
      <category>react</category>
      <category>database</category>
      <category>fullstack</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning NodeJS Part 5: Setting up a Node (Express) Development Environment</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 03 Jan 2021 21:33:45 +0000</pubDate>
      <link>https://forem.com/singhshemona/learning-nodejs-part-5-setting-up-a-node-express-development-environment-1h38</link>
      <guid>https://forem.com/singhshemona/learning-nodejs-part-5-setting-up-a-node-express-development-environment-1h38</guid>
      <description>&lt;p&gt;&lt;em&gt;In late fall of 2020, I went on a mission to better understand Node. I wanted to improve the way I use it, how I might be able to use more of its features, and moreover grow my front end knowledge to full stack. This series includes the notes I've compiled from my learnings over at &lt;a href="https://www.theodinproject.com/courses/nodejs"&gt;The Odin Project&lt;/a&gt;. The lessons also include general web concepts necessary to better work with Node.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Now let's setup a basic Express app and breakdown some of its included files. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Express Application Generator&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the Express Application Generator?
&lt;/h3&gt;

&lt;p&gt;NPM can be used to globally install the Express Application Generator, a tool that creates skeleton Express web apps that follow MVC.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install express-generator -g&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you create an Express app named "helloworld" with default settings
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to where you want to create it and run &lt;code&gt;express helloworld&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;It will generate a package.json file for you, with some dependencies. Install all dependencies via &lt;code&gt;cd helloworld&lt;/code&gt; and &lt;code&gt;npm install&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run the app! &lt;code&gt;DEBUG=helloworld:* npm start&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Navigating Contents of a Node project&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What version of Node/Express should you use?
&lt;/h3&gt;

&lt;p&gt;Generally, you should use the most recent and long-term supported release of Node. Always use the latest version of Express.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the purpose of a package.json file?
&lt;/h3&gt;

&lt;p&gt;You can manually use NPM to separately fetch each needed package. Typically dependencies are managed using a plain-text definition file named package.json. This file lists all the dependencies for a specific JavaScript "package".&lt;/p&gt;

&lt;p&gt;It should contain everything NPM needs to fetch and run your application. If you were writing a reusable library you could use this definition to upload your package to the npm repository and make it available for other users!&lt;/p&gt;

&lt;h3&gt;
  
  
  What are development dependencies?
&lt;/h3&gt;

&lt;p&gt;If a dependency is only used during development, you should instead save it as a "development dependency" so that your package users don't have to install it in production. For example, to use the popular JavaScript Linting tool eslint you would call NPM as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install eslint --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The following entry would then be added to your application's package.json:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;devDependencies&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eslint&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^7.10.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What do named scripts allow us to do?
&lt;/h3&gt;

&lt;p&gt;Named scripts are defined in the package.json and call NPM to execute run-script on whatever it is you've set it to. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lint&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eslint src/js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding this, we can now run &lt;code&gt;npm run lint&lt;/code&gt; and it would run &lt;code&gt;npm run-script eslint src/js&lt;/code&gt; for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Middleware&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the role of middleware?
&lt;/h3&gt;

&lt;p&gt;A middleware is just a plain JavaScript function that Express will call for you between the time it receives a network request and the time it fires off a response (i.e. it’s a function that sits in the middle). For example, you might have an authenticator that checks to see if the user is logged in, or otherwise has permission to access whatever they’re requesting.&lt;/p&gt;

&lt;p&gt;Route functions end the HTTP request-response cycle by returning some response to the HTTP client whereas middleware functions typically perform some operation on the request or response and then call the next function in the "stack", which might be more middleware or a route handler.&lt;/p&gt;

&lt;p&gt;Let's see what a typical middleware function looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myLogger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Request IP: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Request Method: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Request date: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// THIS IS IMPORTANT!&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="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myLogger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;req&lt;/code&gt; - object with data about incoming request, ex. any parameters in the URL&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;res&lt;/code&gt; - object that represents the response that Express is going to send back to the user. You usually use the information in &lt;code&gt;req&lt;/code&gt; to determine what you're going to do with &lt;code&gt;res&lt;/code&gt; by calling &lt;code&gt;res.send()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;next&lt;/code&gt; - very important, tells express to move to the next middleware in the stack, but if you forget to call it then your app will pause and nothing will happen&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app.use&lt;/code&gt; - is how you load your middleware function into Express so that it knows to use it. It will write these details to the console every time you get a network request&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is the only middleware function that is part of Express that allows us to serve files like images, CSS and JS?
&lt;/h3&gt;

&lt;p&gt;express.static, or &lt;code&gt;static()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How are errors handled?
&lt;/h3&gt;

&lt;p&gt;By one or more special middleware functions that have four arguments, instead of the usual three: &lt;code&gt;(err, req, res, next)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Express comes with a built-in error handler, which takes care of any remaining errors that the app might encounter.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Routes, Views and Controllers&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are routes?
&lt;/h3&gt;

&lt;p&gt;A route is a section of Express code that associates an HTTP verb (GET, POST, PUT, DELETE, etc.), a URL path/pattern, and a function that is called to handle that pattern.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are template or "view" engines?
&lt;/h3&gt;

&lt;p&gt;Allow you to specify the structure you want the data to be outputted via a template. You can have placeholders for data that will be filled in when a page is generated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://strongloop.com/strongblog/compare-javascript-templates-jade-mustache-dust/"&gt;Express supports many template engines&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How would one get started with using a template engine?
&lt;/h3&gt;

&lt;p&gt;Start by installing the package containing your template library, then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;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;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&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="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// 1 - Set directory to contain the templates ('views')&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;views&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;views&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// 2 - Set view engine to use, in this case 'some_template_engine_name'&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;view engine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some_template_engine_name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// 3 - The appearance of the template will depend on what engine you use &lt;/span&gt;
&lt;span class="c1"&gt;// Assuming that you have a template file named &lt;/span&gt;
&lt;span class="c1"&gt;// "index.&amp;lt;template_extension&amp;gt;" that contains placeholders for data &lt;/span&gt;
&lt;span class="c1"&gt;// variables named 'title' and "message", you would call &lt;/span&gt;
&lt;span class="c1"&gt;// Response.render() in a route handler function to create and &lt;/span&gt;
&lt;span class="c1"&gt;// send the HTML response:&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;'About dogs', message: 'Dogs rock!' });&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;This brings us to the conclusion of foundations in Node and Express. To put all of your new knowledge to the test, I recommend following the &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Tutorial_local_library_website"&gt;Local Library tutorial&lt;/a&gt; over at MDN Web Docs. This project will use all the concepts I've mentioned in this 5-part series, and will truly help solidify a holistic view of Express.&lt;/p&gt;

</description>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning NodeJS Part 4: Introduction to Express</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 03 Jan 2021 21:33:37 +0000</pubDate>
      <link>https://forem.com/singhshemona/learning-nodejs-part-4-introduction-to-express-34ol</link>
      <guid>https://forem.com/singhshemona/learning-nodejs-part-4-introduction-to-express-34ol</guid>
      <description>&lt;p&gt;&lt;em&gt;In late fall of 2020, I went on a mission to better understand Node. I wanted to improve the way I use it, how I might be able to use more of its features, and moreover grow my front end knowledge to full stack. This series includes the notes I've compiled from my learnings over at &lt;a href="https://www.theodinproject.com/courses/nodejs" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt;. The lessons also include general web concepts necessary to better work with Node.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;The purpose of this section is to gain familiarity with what Express is, how it superpowers Node with the functionalities it provides, and the main building blocks of an Express application.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Understanding Express&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why use a web framework?
&lt;/h3&gt;

&lt;p&gt;Common web-development tasks are not directly supported by Node itself. If you want to add specific handling for different HTTP verbs (e.g. GET, POST, DELETE, etc.), handle requests at different URL paths ("routes"), serve static files, or use templates to dynamically create the response, Node won't be of much use on its own. You will either need to write the code yourself, or you can avoid reinventing the wheel and use a web framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explain the general process of how a dynamic web app functions. Where does Express fit into this?
&lt;/h3&gt;

&lt;p&gt;In a traditional data-driven website, a web application waits for HTTP requests from the web browser (or other client). When a request is received the application works out what action is needed based on the URL pattern and associated information contained in POST / GET data. Depending on what is required it may then read or write information from a database or perform other tasks required to satisfy the request. The application will then return a response to the web browser, often dynamically creating an HTML page for the browser to display by inserting the retrieved data into placeholders in an HTML template.&lt;/p&gt;

&lt;p&gt;After the request is received, this is when Express can come in handy. From Node's perspective, &lt;strong&gt;Express is just another package&lt;/strong&gt; that you need to install using NPM and then &lt;code&gt;require&lt;/code&gt; in your own code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Express is often described as 'unopinionated', what does that mean?
&lt;/h3&gt;

&lt;p&gt;Unopinionated frameworks have far fewer restrictions on the best way to glue components together to achieve a goal, or even what components should be used. They suggest the best setup is the one you think would work optimally for use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Write "Hello World" with Express
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;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="c1"&gt;// import express module&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// create an Express application&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// route definition, callback function that will be invoked whenever&lt;/span&gt;
&lt;span class="c1"&gt;// there is an HTTP GET request with a path relative to the site root&lt;/span&gt;
&lt;span class="c1"&gt;// callback function takes a request and a response object as arguments&lt;/span&gt;
&lt;span class="c1"&gt;// and calls send() on the response to return the string "Hello World!"&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// starts up the server on a specified port ('3000')&lt;/span&gt;
&lt;span class="c1"&gt;// prints a log comment to the console&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Example app listening on port ${port}!&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;h2&gt;
  
  
  &lt;strong&gt;Databases with Express&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How do CRUD operations correlate to HTTP methods in Express?
&lt;/h3&gt;

&lt;p&gt;The CRUD operations roughly correlate to the HTTP methods that you can employ in an express app. This definition can be somewhat flexible, but in general &lt;code&gt;create&lt;/code&gt; correlates to &lt;code&gt;POST&lt;/code&gt; (or &lt;code&gt;app.post()&lt;/code&gt; in an express app), &lt;code&gt;read&lt;/code&gt; correlates to &lt;code&gt;GET&lt;/code&gt; (&lt;code&gt;app.get()&lt;/code&gt;), &lt;code&gt;update&lt;/code&gt; to &lt;code&gt;PUT&lt;/code&gt; (&lt;code&gt;app.put()&lt;/code&gt;) and &lt;code&gt;delete&lt;/code&gt; to &lt;code&gt;DELETE&lt;/code&gt; (&lt;code&gt;app.delete()&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  What are two common ways for interacting with a database and the benefits of each?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Using the databases' native query language (e.g. SQL), which results in the best performance&lt;/li&gt;
&lt;li&gt;Using an Object Data Model ("ODM") or an Object Relational Model ("ORM"). An ODM/ORM represents the website's data as JavaScript "objects" or "models", which are mapped to the underlying database by the ORM. Some ORMs are tied to a specific database, while others provide a database-agnostic backend. The benefit here is that programmers can continue to think in terms of JavaScript objects rather than database semantics.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When designing your models it makes sense to have separate models for every "object". What are "objects"? If you were thinking of making an app to represent the functionings of a library, what would be some example objects you might have?
&lt;/h3&gt;

&lt;p&gt;Objects are a group of related information. Some obvious candidates for a library project's models are books, book instances, and authors.&lt;/p&gt;

&lt;p&gt;You might also want to use models to represent selection-list options (e.g. like a drop-down list of choices), rather than hard-coding the choices into the website itself — this is recommended when all the options aren't known up front or may change.&lt;/p&gt;

&lt;h3&gt;
  
  
  Once we've decided on our models and fields, we need to think about the relationships between them. How is this planned?
&lt;/h3&gt;

&lt;p&gt;With a UML association diagram.&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%2Fi%2Fby4yh8jiove9skrv95sy.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%2Fi%2Fby4yh8jiove9skrv95sy.png" alt="UML association diagram"&gt;&lt;/a&gt;&lt;/p&gt;

The multiplicities are the numbers on the diagram showing the numbers (maximum and minimum) of each model that may be present in the relationship. For example, the connecting line between the boxes shows that Book and Genre are related. The numbers close to the Book model show that a Genre must have zero or more Books (as many as you like), while the numbers on the other end of the line next to the Genre show that a book can have zero or more associated Genres.







&lt;p&gt;Let's move onto setting up a working development environment with Express.&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning NodeJS Part 3: Getting Started</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 03 Jan 2021 21:33:28 +0000</pubDate>
      <link>https://forem.com/singhshemona/learning-nodejs-part-3-getting-started-5d2e</link>
      <guid>https://forem.com/singhshemona/learning-nodejs-part-3-getting-started-5d2e</guid>
      <description>&lt;p&gt;&lt;em&gt;In late fall of 2020, I went on a mission to better understand Node. I wanted to improve the way I use it, how I might be able to use more of its features, and moreover grow my front end knowledge to full stack. This series includes the notes I've compiled from my learnings over at &lt;a href="https://www.theodinproject.com/courses/nodejs"&gt;The Odin Project&lt;/a&gt;. The lessons also include general web concepts necessary to better work with Node.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Let's look at some common Node jargon, and what it means to develop these features in a Node environment. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Modules&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a module?
&lt;/h3&gt;

&lt;p&gt;Think of modules as libraries - a set of functions you can choose to include in your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does one create and use modules?
&lt;/h3&gt;

&lt;p&gt;To create your own module, use &lt;code&gt;exports&lt;/code&gt; to make properties and methods available outside of the module file. The following returns the current date and time:&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;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myDateTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Date&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;Once you've created a module, to actually use the module in a different JS files we utilize the &lt;code&gt;require()&lt;/code&gt; function, passing to it the name of the desired module. You also use this for including modules that are built-in:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var myDateTime = require('myDateTime');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, let's practice making modules as well as using existing ones.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating and Using a Module
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;square.js&lt;/code&gt; module with a method called &lt;code&gt;area()&lt;/code&gt;, then include &lt;code&gt;area()&lt;/code&gt; in a new file (&lt;code&gt;example.js&lt;/code&gt;) to calculate the area of a square with a width of 4.&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;// square.js &lt;/span&gt;
&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;width&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;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// OR&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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;area&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;width&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;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// example.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./square&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The area of a square with a width of 4 is &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using the HTTP Module: Set up a basic web server with Node
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// The function passed into the http.createServer() method&lt;/span&gt;
&lt;span class="c1"&gt;// will be executed when someone tries to access the computer on port 8080&lt;/span&gt;
&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's happening here? This code creates a server and says “any time we get a network request, run this callback function”. The function happens to respond with the text &lt;em&gt;Hello World!&lt;/em&gt; So if you save this code to a &lt;code&gt;.js&lt;/code&gt; file, run it using &lt;code&gt;node name-of-file.js&lt;/code&gt; and navigate to &lt;code&gt;http://localhost:8080/&lt;/code&gt; on your browser of choice, you will see &lt;em&gt;Hello World!&lt;/em&gt; on your screen.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using the URL Module: Parse a url address and split it into readable parts
&lt;/h3&gt;

&lt;p&gt;What would this take?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We first need to include the url module. &lt;/li&gt;
&lt;li&gt;Then, store in a variable the address we would like to parse along with it's parsed version in a third variable.&lt;/li&gt;
&lt;li&gt;You can see what the parsed version gives us in the 3 &lt;code&gt;console.log()&lt;/code&gt; statements below.&lt;/li&gt;
&lt;li&gt;Lastly, we can query through the last bits of the url to return whatever it is we are looking for.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see this in action:&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;var&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;url&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;adr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:8080/default.htm?year=2017&amp;amp;month=february&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;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;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//returns 'localhost:8080'&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;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//returns '/default.htm'&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;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//returns '?year=2017&amp;amp;month=february'&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;qdata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//returns an object: { year: 2017, month: 'february' }&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;qdata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//returns 'february'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we know how to parse a url address and in the section before that we learned how to set up a basic webserver in Node, let's combine the two.&lt;/p&gt;

&lt;h3&gt;
  
  
  Serve a requested file to the client
&lt;/h3&gt;

&lt;p&gt;Let's again break down what these steps would look like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create two html files, &lt;code&gt;summer.html&lt;/code&gt; and &lt;code&gt;winter.html&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Put some basic html into both files, ensuring they both consist of different content&lt;/li&gt;
&lt;li&gt;Within that same folder, create a &lt;code&gt;.js&lt;/code&gt; file requiring the http, url and fs modules&lt;/li&gt;
&lt;li&gt;Using these modules to create a server, parse through the url and open the requested file to the client. (&lt;code&gt;http://localhost:8080/summer.html&lt;/code&gt; should render the contents of your &lt;code&gt;summer.html&lt;/code&gt; page and &lt;code&gt;http://localhost:8080/winter.html&lt;/code&gt; should render the contents of your &lt;code&gt;winter.html&lt;/code&gt; page.)&lt;/li&gt;
&lt;li&gt;Be sure to throw a 404 error if anything goes wrong
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;url&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&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;url&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;var&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;404 Not Found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;CRUD with Node&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now, onto creating, reading, updating and deleting files with Node.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's say we wanted our basic server to read from some sample file (say &lt;code&gt;demofile1.html&lt;/code&gt;) and return to the client (write on the page) whatever is in &lt;code&gt;demofile1.html&lt;/code&gt;:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;demofile1.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Write a program that uses the &lt;code&gt;appendFile()&lt;/code&gt; method to append 'Hello content!' to a new file named &lt;code&gt;mynewfile1.txt&lt;/code&gt;.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mynewfile1.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello content!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Saved!&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;h3&gt;
  
  
  What is the difference between appendFile(), open(), and writeFile()?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;appendFile()&lt;/code&gt; adds specified content to the end of a file. If the file does not exist, the file will be created.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;open()&lt;/code&gt; takes a "flag" as the second argument, if the flag is "w" for "writing", the specified file is opened for writing. If the file does not exist, an empty file is created.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;writeFile()&lt;/code&gt; replaces the specified file with content (if the file exists). If the file does not exist, a new file containing the specified content, will be created.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Write a program that deletes a file, say &lt;code&gt;mynewfile1.tst&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unlink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mynewfile2.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;File 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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Packages&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are packages?
&lt;/h3&gt;

&lt;p&gt;NPM packages contain all the files you need for a module. Think of them as add-ons to your project to give it extra abilities or avoid having to write some functionality from scratch.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait - what's the difference between modules and packages?
&lt;/h3&gt;

&lt;p&gt;A package is a file or directory that is described typically by a &lt;code&gt;package.json&lt;/code&gt; file. A module is any file or directory in the &lt;code&gt;node_modules&lt;/code&gt; directory of a project that can be loaded in by Node. Since modules are not required to have a &lt;code&gt;package.json&lt;/code&gt; file, not all modules are packages. Only modules that have a package.json file are also packages.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you use an NPM package?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;NPM is already installed once you install Node&lt;/li&gt;
&lt;li&gt;Install package of your choice via &lt;code&gt;npm install name-of-package&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Include the package just like you would with any module: &lt;code&gt;var package = require('package-name');&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Events&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We briefly discussed events in part 1 of this series. To refresh:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every action on a computer is an event. It could be a network request that triggers something else or someone trying to access a port on a server.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  How do we interact with events in Node, i.e. how does one create, fire and listen for events?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Include the Events module - this allows you to create, fire and listen for events: &lt;code&gt;var events = require('events');&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;All event properties and methods are an instance of an EventEmitter object, so to access them you have to create an EventEmitter object: &lt;code&gt;var eventEmitter = new events.EventEmitter();&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Assign event handlers to your own events with the EventEmitter object. To fire an event, use &lt;code&gt;emit()&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, suppose I wanted a &lt;code&gt;scream&lt;/code&gt; event to fire whenever some action occurred:&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;var&lt;/span&gt; &lt;span class="nx"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;events&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;eventEmitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//Create an event handler:&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myEventHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I hear a scream!&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="c1"&gt;//Assign the event handler to an event:&lt;/span&gt;
&lt;span class="nx"&gt;eventEmitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;scream&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;myEventHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//Fire the 'scream' event:&lt;/span&gt;
&lt;span class="nx"&gt;eventEmitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;scream&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;Now that we're more familiar with some of Node's powers, let's move on to the way Node is actually used in most scenarios - via a framework.&lt;/p&gt;

</description>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning NodeJS Part 2: Server Side and Dynamic Sites</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 03 Jan 2021 21:33:17 +0000</pubDate>
      <link>https://forem.com/singhshemona/learning-nodejs-part-2-server-side-and-dynamic-sites-4a0h</link>
      <guid>https://forem.com/singhshemona/learning-nodejs-part-2-server-side-and-dynamic-sites-4a0h</guid>
      <description>&lt;p&gt;&lt;em&gt;In late fall of 2020, I went on a mission to better understand Node. I wanted to improve the way I use it, how I might be able to use more of its features, and moreover grow my front end knowledge to full stack. This series includes the notes I've compiled from my learnings over at &lt;a href="https://www.theodinproject.com/courses/nodejs" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt;. The lessons also include general web concepts necessary to better work with Node.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;A continuation of fundamentals, I'll now be diving into what Node does for websites and why we would want websites like these in the first place.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction to the server side&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the difference between client-side code and sever-side code?
&lt;/h3&gt;

&lt;p&gt;Code running in the browser is known as client-side code and is primarily concerned with improving the appearance and behavior of a rendered web page. Server-side web programming involves choosing which content is returned to the browser in response to requests. The server-side code handles tasks like validating submitted data and requests, using databases to store and retrieve data and sending the correct data to the client as required. &lt;strong&gt;Node is a server-side web language.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Servers can store and use information about clients to provide a convenient and tailored user experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Describe the purpose of a server.
&lt;/h3&gt;

&lt;p&gt;A server is a computer that is connected &lt;strong&gt;directly&lt;/strong&gt; to the internet. The computers we use regularly are called clients because they are connected &lt;strong&gt;indirectly&lt;/strong&gt; to the internet via an ISP. Servers allow you to access information from databases or file storage.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is HTTP for?
&lt;/h3&gt;

&lt;p&gt;Web browsers communicate with web servers using the &lt;strong&gt;HyperText Transfer Protocol&lt;/strong&gt;. When you click a link on a web page, submit a form, or run a search, an HTTP request is sent from your browser to the target server.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does a HTTP request contain?
&lt;/h3&gt;

&lt;p&gt;An HTTP request includes: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;a URL identifying the affected resource&lt;/li&gt;
&lt;li&gt;Method that defines the required action (to get, delete, or post the resource)&lt;/li&gt;
&lt;li&gt;May include additional information encoded in URL parameters (the field-value pairs sent via a query string), as POST data (data sent by the HTTP POST method), or in associated cookies.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 These GET/POST/HEAD methods cost differently, some are more 'expensive' operations than others&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What is an HTTP response?
&lt;/h3&gt;

&lt;p&gt;Web servers wait for client request messages, process them when they arrive, and reply to the web browser with an HTTP response message. The response contains a status line indicating whether or not the request succeeded (e.g. &lt;code&gt;HTTP/1.1 200 OK&lt;/code&gt; for success).&lt;/p&gt;

&lt;p&gt;The body of a successful response to a request would contain the requested resource. This could be a new HTML page, an image, etc, which could then be displayed by the web browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a web framework?
&lt;/h3&gt;

&lt;p&gt;Frameworks are collections of functions, objects, rules and other code constructs designed to solve common problems, speed up development, and simplify the different types of tasks faced in a particular development stream. Client-side frameworks are often used to help speed up development of client-side code, but you can also choose to write all the code by hand. In fact writing your code by hand can be quicker and more efficient if you only need a small, simple web site UI.&lt;/p&gt;

&lt;p&gt;In contrast, you would &lt;em&gt;almost never&lt;/em&gt; consider writing the server-side component of a web app without a framework. Implementing a vital feature like an HTTP server is really hard to do from scratch in say, Python, but Python web frameworks like Django provide one out-of-the-box, along with other very useful tools.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Checkout the section titled &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/First_steps/Web_frameworks" rel="noopener noreferrer"&gt;A few good web frameworks?&lt;/a&gt; to learn more about the advantages of using some of the more popular frameworks out there.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What is AJAX?
&lt;/h3&gt;

&lt;p&gt;AJAX stands for Asynchronous JavaScript and XML. It is not a programming language. &lt;strong&gt;It's a technique for accessing web servers from a web page.&lt;/strong&gt; It was ideated based on the principle of returning data to a web browser so that it can dynamically update its own content.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Dynamic and Static Sites&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What do we mean when we categorize sites as dynamic or static?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Static&lt;/strong&gt; sites return the same hard-coded content from the server whenever a particular resource is requested.&lt;/li&gt;
&lt;/ul&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%2Fi%2Fnkv3hfwyfcrvuuudd50m.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%2Fi%2Fnkv3hfwyfcrvuuudd50m.png" alt="Static"&gt;&lt;/a&gt;&lt;/p&gt;

Pressing a button, clicking on a link will result in same style files being requested and expected behavior regardless of who is accessing this site.




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic&lt;/strong&gt; sites are ones in which some of the responding content is generated only when needed. Dynamic sites can highlight content that is more relevant based on user preferences and habits. It can also make sites easier to use by storing personal preferences and information, like reusing stored credit card details to streamline subsequent payments.&lt;/li&gt;
&lt;/ul&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%2Fi%2F5jfu1jwavxn8vq7gx3bo.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%2Fi%2F5jfu1jwavxn8vq7gx3bo.png" alt="Dynamic"&gt;&lt;/a&gt;&lt;/p&gt;

Based on a variety of conditions (being a new user, not logged in, long-time existing user) how you view this site could be drastically different. Different navigation may be available, and clicking on a button like "Show More Recommendations" will load different options for two users.




&lt;h3&gt;
  
  
  Why might you need a back-end for a project?
&lt;/h3&gt;

&lt;p&gt;Perhaps the most significant benefit of server-side code is that it allows you to tailor website content for individual users.&lt;/p&gt;

&lt;h3&gt;
  
  
  When would you &lt;em&gt;not&lt;/em&gt; need a back-end for a project?
&lt;/h3&gt;

&lt;p&gt;If you want to build a small, static page, one that requires no information be stored from any interactions and receives the same hard coded content.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explain some things that Node is commonly used for.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Generating dynamic page content&lt;/li&gt;
&lt;li&gt;Creating, opening, reading, writing, deleting and closing files on the server&lt;/li&gt;
&lt;li&gt;Collecting form data&lt;/li&gt;
&lt;li&gt;Adding, deleting, and modifying data in your database&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Onto the next section, where we'll now get to see Node in action.&lt;/p&gt;

</description>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning NodeJS Part 1: Understanding the Essentials</title>
      <dc:creator>Shemona Singh</dc:creator>
      <pubDate>Sun, 03 Jan 2021 21:33:09 +0000</pubDate>
      <link>https://forem.com/singhshemona/learning-nodejs-part-1-understanding-the-essentials-4h4o</link>
      <guid>https://forem.com/singhshemona/learning-nodejs-part-1-understanding-the-essentials-4h4o</guid>
      <description>&lt;p&gt;&lt;em&gt;In late fall of 2020, I went on a mission to better understand Node. I wanted to improve the way I use it, how I might be able to use more of its features, and moreover grow my front end knowledge to full stack. This series includes the notes I've compiled from my learnings over at &lt;a href="https://www.theodinproject.com/courses/nodejs" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt;. The lessons also include general web concepts necessary to better work with Node.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;In order to pass the potential bumps when learning Node, it's important that some of your web fundamentals are in order. For this section and the next, I'll be revisiting key topics and tying it back to Node. Let's begin!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Understanding what Node really is&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Node is an open-source, cross-platform runtime environment that allows developers to create all kinds of server-side tools and applications in JavaScript.&lt;/p&gt;

&lt;p&gt;But that's just the textbook definition - let's break down what those words mean.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's so groundbreaking about Node?
&lt;/h3&gt;

&lt;p&gt;JavaScript was originally designed to run in the browser. This means it was impossible to run it in anything that was not a website. &lt;strong&gt;Node brings JS out of the browser-world, so you can accomplish with it most anything other server-side languages can do. This is what is so groundbreaking about Node.&lt;/strong&gt; It gives JavaScript backend capabilities. &lt;/p&gt;

&lt;p&gt;To make this happen Node must have added functionality that original JS did not have, right? Indeed, Node enables JS to have abilities like: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create http connections&lt;/li&gt;
&lt;li&gt;Listen to network requests &lt;/li&gt;
&lt;li&gt;Generate dynamic page content&lt;/li&gt;
&lt;li&gt;Create/open/read/write/delete/close files on a server&lt;/li&gt;
&lt;li&gt;Collect form data&lt;/li&gt;
&lt;li&gt;Add/delete/modify data in a database&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The term 'V8' comes up alongside Node. What is V8?
&lt;/h3&gt;

&lt;p&gt;JS runtime environments need engines to run. As we discussed, Node is a JS runtime environment so it needs an engine as well. Node's engine is built on Chrome's open source JS engine, V8. V8 is written in C++, and allows you to write your own C++ via hooks that you can make available to JavaScript. &lt;/p&gt;

&lt;p&gt;There are other runtime engines like &lt;a href="https://wiki.mozilla.org/JavaScript:New_to_SpiderMonkey" rel="noopener noreferrer"&gt;SpiderMonkey&lt;/a&gt; by Mozilla and &lt;a href="https://en.wikipedia.org/wiki/Chakra_(JScript_engine)" rel="noopener noreferrer"&gt;Chakra&lt;/a&gt; by Microsoft.&lt;/p&gt;

&lt;h3&gt;
  
  
  What would “hello world” look like in Node?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a file (name it anything) but be sure it has the extension &lt;code&gt;.js&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Put the following in it: &lt;code&gt;console.log("Hello World!");&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Open your node terminal, &lt;code&gt;cd&lt;/code&gt; into the directory to the folder where the file is saved and run &lt;code&gt;node [name-of-file].js&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Look familiar? Since Node is a runtime environment for JS, you can power it with the JS syntax you know and love.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Key Concepts in Understanding Node&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Node is &lt;strong&gt;asynchronous&lt;/strong&gt; and &lt;strong&gt;event-driven&lt;/strong&gt;, let's break down these terms:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Event-driven:&lt;/strong&gt; every action on a computer is an event. It could be a network request, someone trying to access a port on a server, clicking submit on a button - the possibilities are endless. Events trigger some response to occur, which is what Node helps us to handle. &lt;/p&gt;

&lt;p&gt;Synchronous and asynchronous describes two different ways code can be run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronous:&lt;/strong&gt; means each operation must complete before the next operation can start.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asynchronous:&lt;/strong&gt; means instead of writing code in prediction of when each line will run, you write it as a collection of smaller functions that are called in response to an event.&lt;/p&gt;

&lt;p&gt;For example, let’s say you are writing a program and you need it to do the following: It should read some text from a file, print that text to the console, query a database for a list of users and filter the users based on their age. &lt;/p&gt;

&lt;p&gt;This is what those steps would look like if they were to be executed &lt;strong&gt;synchronously&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read File&lt;/li&gt;
&lt;li&gt;Print File Contents&lt;/li&gt;
&lt;li&gt;Query Database&lt;/li&gt;
&lt;li&gt;Filter Database Query results&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To make these steps execute &lt;strong&gt;asynchronously&lt;/strong&gt; we would instead break up the tasks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read File &lt;em&gt;AND THEN&lt;/em&gt; Print File Contents&lt;/li&gt;
&lt;li&gt;Query Database &lt;em&gt;AND THEN&lt;/em&gt; Filter Database Query Results&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this way, Node is not waiting around. While the file is being read, it can query the database. Then depending on which one is complete first (an event!) it will move on to the next task at hand.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This process is almost exactly like the way that you would use &lt;code&gt;addEventListener&lt;/code&gt; in front-end JavaScript to wait for a user action such as a mouse-click or keyboard press. The main difference is that the events are going to be things such as network requests and database queries. This functionality is facilitated through the use of &lt;strong&gt;callbacks&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What are callbacks?
&lt;/h3&gt;

&lt;p&gt;Callbacks are functions that are passed into another function as an argument:&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;// this has no callback&lt;/span&gt;
&lt;span class="c1"&gt;// it prints "First" then "Second"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;First&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Second&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// this has a callback &lt;/span&gt;
&lt;span class="c1"&gt;// it prints "Second" then "First"&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;First&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="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Second&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;If you're not careful, this can result in &lt;a href="http://callbackhell.com/" rel="noopener noreferrer"&gt;callback hell&lt;/a&gt;, which you can reduce with practices like using the &lt;a href="https://www.npmjs.com/package/async" rel="noopener noreferrer"&gt;async&lt;/a&gt; module or &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noopener noreferrer"&gt;Promises&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about error-first callbacks?
&lt;/h3&gt;

&lt;p&gt;A common convention for Node and Express is to use &lt;a href="http://fredkschott.com/post/2014/03/understanding-error-first-callbacks-in-node-js/" rel="noopener noreferrer"&gt;error-first callbacks&lt;/a&gt;. In this convention, the first value in the callback functions is an error value, while subsequent arguments contain success data. This ensures if you get really in the weeds with callbacks, you can be notified when a request is being held up due to a process never going through.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explain the event loop.
&lt;/h3&gt;

&lt;p&gt;This leads me to the last large concept for this section - the event loop. You can &lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ&amp;amp;feature=youtu.be&amp;amp;t=852" rel="noopener noreferrer"&gt;watch this excellent video explanation&lt;/a&gt; on the event loop. But it's on the lengthier side, so this section will be summarizing its highlights.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The event loop is the secret behind JavaScript's asynchronous programming. JS executes all operations on a single thread, but using a few smart data structures, it gives us the illusion of multi-threading.&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%2Fi%2Fczf5ypvh4ef3rvir9c8w.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%2Fi%2Fczf5ypvh4ef3rvir9c8w.png" alt="Screenshot from Event loop Video"&gt;&lt;/a&gt;&lt;br&gt;
The job of the event loop is to watch for the stack and the task queue. If the stack is empty and the task queue is not, it will push the first item from the queue to the stack, effectively running it. The 'webapis' section is what we get from whatever our environment provides us with, in this case it's the browser providing us with the ability to keep track of the &lt;code&gt;setTimeout&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ajax requests would be treated the same way - put into the webapi section, because the code for a request does not live in the JS runtime, it lives in the browser webapi.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;We have one more section after this that lays down fundamentals. Onto part 2!&lt;/p&gt;

</description>
      <category>node</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
