<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sespinoza</title>
    <description>The latest articles on DEV Community by sespinoza (@sespinozj).</description>
    <link>https://dev.to/sespinozj</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%2F60318%2F3851d39f-e2c9-4f62-ab7f-b51519d9c83a.jpg</url>
      <title>DEV Community: sespinoza</title>
      <link>https://dev.to/sespinozj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sespinozj"/>
    <language>en</language>
    <item>
      <title>React Router Dom Essentials</title>
      <dc:creator>sespinoza</dc:creator>
      <pubDate>Wed, 24 Feb 2021 16:26:03 +0000</pubDate>
      <link>https://dev.to/sespinozj/react-router-dom-essentials-14ki</link>
      <guid>https://dev.to/sespinozj/react-router-dom-essentials-14ki</guid>
      <description>&lt;p&gt;Hey there! In this article, we'll cover how to add routes to a React app. &lt;/p&gt;

&lt;p&gt;It will be useful if you are familiar with these concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ES6 syntax&lt;/li&gt;
&lt;li&gt;JSX.&lt;/li&gt;
&lt;li&gt;React: functional components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Index&lt;br&gt;
  &lt;/p&gt;
&lt;ul&gt;

    &lt;li&gt;The repository&lt;/li&gt;

    &lt;li&gt;A little context first&lt;/li&gt;

    &lt;li&gt;Starting a project&lt;/li&gt;

    &lt;li&gt;

      The initial routes.
      &lt;ul&gt;

        &lt;li&gt;The HashRouter component&lt;/li&gt;

        &lt;li&gt;The Route component&lt;/li&gt;

        &lt;li&gt;The Switch component&lt;/li&gt;

        &lt;li&gt;The Redirect component&lt;/li&gt;

      &lt;/ul&gt;

    &lt;/li&gt;

    &lt;li&gt;More juice using Link&lt;/li&gt;

  &lt;/ul&gt;

&lt;h2&gt;
  
  
  The repository
&lt;/h2&gt;

&lt;p&gt;Show, don't tell, right? Here is the repository with a quick example for you to clone it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone git@gitlab.com:sespinoz/react-router-dom-essentials-book-example.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quick note: At the moment of writing this article we are using &lt;code&gt;react-router-dom&lt;/code&gt; &lt;code&gt;5.2.0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As a good React Developer, you should always have the &lt;a href="https://reactrouter.com/web/guides/quick-start" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; opened and also try things for yourself :)&lt;/p&gt;

&lt;h2&gt;
  
  
  A little context first
&lt;/h2&gt;

&lt;p&gt;When the web was younger, routing meant to ask for a new HTML page to the server each time a user clicked on a link inside a page. This meant more traffic, more delay in loading that page and a poor user experience. &lt;/p&gt;

&lt;p&gt;With React, you're able to create Single Page Applications (SPA), which means that you only have to request for the page once, and then you can manipulate the DOM to change the content of the current page on each event triggered by the user. This indeed can be achieved with vanilla javascript, but there are libraries like &lt;code&gt;react-router-dom&lt;/code&gt; that will help you to do just that.&lt;/p&gt;

&lt;p&gt;React is a lightweight library, not a complete framework like Angular, for this reason, it doesn't provide a routing by default, it let you decide which routing method to use: a library or to do it yourself. Here is where the Facebook team, that created React, would recommend you to use it's &lt;code&gt;react-router-dom&lt;/code&gt; library, the one we'll use now.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting a project
&lt;/h2&gt;

&lt;p&gt;For the propose of this, we'll make a &lt;em&gt;SPA&lt;/em&gt; for a bookstore that will have three routes and a header:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/home&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/books&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/books/:slug&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;em&gt;Home&lt;/em&gt; page will have a welcome message. The &lt;em&gt;Header&lt;/em&gt; will have a link to Home and to the Book's page. In the &lt;em&gt;Books&lt;/em&gt; page we'll display a list of books with links that will redirect you to the &lt;em&gt;Book description&lt;/em&gt; page (&lt;code&gt;/books/:slug&lt;/code&gt;) with mode details.&lt;/p&gt;

&lt;p&gt;React allows you to create Single Page Applications (SPA) very easily using &lt;code&gt;create-react-app&lt;/code&gt;. I highly recommend using it as it has a lot of the configurations such as Babel and Webpack already set for you so that you won't have to do it from scratch. Let's do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-react-app react-router-dom-essentials-book-example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, just &lt;code&gt;cd react-router-dom-essentials-book-example&lt;/code&gt; and we'll add &lt;code&gt;react-router-dom&lt;/code&gt; to our dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;react-router-dom &lt;span class="nt"&gt;--save&lt;/span&gt;
&lt;span class="c"&gt;# or&lt;/span&gt;
yarn add react-router-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll focus on the functionality and we'll leave the folder structure and moving the components to each file to another article for better understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  The initial routes.
&lt;/h2&gt;

&lt;p&gt;To give the routing functionality to our application we need to wrap our main component inside a special component that react-router provides for us: &lt;code&gt;&amp;lt;HashRouter/&amp;gt;&lt;/code&gt;. Let's open the &lt;code&gt;src/App.js&lt;/code&gt; file and set an initial setting to try things.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;HashRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Redirect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Header&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Header&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Home&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Books&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BookItem&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Book Item&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;HashRouter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;exact&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;exact&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/books/"&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Books&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/books/:slug&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;BookItem&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Redirect&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;HashRouter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;At this point, start the react application with &lt;code&gt;yarn start&lt;/code&gt; and check the following routes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://localhost:3000/#/" rel="noopener noreferrer"&gt;http://localhost:3000/#/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:3000/#/books" rel="noopener noreferrer"&gt;http://localhost:3000/#/books&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:3000/#/books/harry-potter" rel="noopener noreferrer"&gt;http://localhost:3000/#/books/harry-potter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:3000/#/this-does-not-match-anything-sam" rel="noopener noreferrer"&gt;http://localhost:3000/#/this-does-not-match-anything-sam&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should see that a basic routing it's already working showing you the three components. The last URL should redirect you to the home page as we set it like that for any URL that doesn't match the ones we defined here.&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%2Fsespinoza.me%2Ffiles%2Fimages%2Finitial-routes.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsespinoza.me%2Ffiles%2Fimages%2Finitial-routes.gif" alt="use the Link component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  HashRouter component
&lt;/h3&gt;

&lt;p&gt;The first thing to notice is that we have everything wrapped inside &lt;code&gt;HashRouter&lt;/code&gt; to enable routing in those components. &lt;/p&gt;

&lt;p&gt;The second thing to notice is the &lt;code&gt;Switch&lt;/code&gt; and the &lt;code&gt;Route&lt;/code&gt; components.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Route component
&lt;/h3&gt;

&lt;p&gt;The route component has three main properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;exact&lt;/code&gt;:  define that the URL should match exactly the &lt;code&gt;path&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;path&lt;/code&gt;: the path in the URL to match.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;component&lt;/code&gt;: the component to render if the path matches.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Why do we need &lt;code&gt;exact&lt;/code&gt; exactly? simply put: Because if you don't, it will render all the components in the routes that will partly match the URL.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;p&gt;For example, here are some URLs that match "...but not exactly" (&lt;code&gt;exact&lt;/code&gt;) are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/books/categories/fiction&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/books/categories/terror&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/books/categories/historical&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means that they are sharing part of the path &lt;code&gt;/books/categories/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Back to our example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;exact&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will match "exactly" &lt;code&gt;/&lt;/code&gt; and render the &lt;code&gt;Home&lt;/code&gt; component &lt;strong&gt;only&lt;/strong&gt; and will not render the rest.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Switch component
&lt;/h3&gt;

&lt;p&gt;The Switch component provides more control over the list of routes so that we don't need to write the &lt;code&gt;exact&lt;/code&gt; property in all our routes, what it does is to render the &lt;em&gt;first&lt;/em&gt; &lt;em&gt;Route&lt;/em&gt; that matches the URL and it stops rendering the other components no matter if they have the &lt;code&gt;exact&lt;/code&gt; word or not. Is equivalent to place the &lt;code&gt;exact&lt;/code&gt; property in all the routes. It just gives you more control if you forget to use the &lt;code&gt;exact&lt;/code&gt; property.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Redirect component
&lt;/h3&gt;

&lt;p&gt;The redirect component allows us to redirect to a specific URL if there's no match in the list of routes.&lt;/p&gt;

&lt;p&gt;Until this point, you can check the progress in the &lt;code&gt;initial-routes&lt;/code&gt; branch in the example project.&lt;/p&gt;

&lt;h2&gt;
  
  
  More juice using Link
&lt;/h2&gt;

&lt;p&gt;The link component allows us to navigate to the routes that we defined.&lt;/p&gt;

&lt;p&gt;Let's update the &lt;code&gt;Header&lt;/code&gt;, &lt;code&gt;Books&lt;/code&gt; and &lt;code&gt;BookItem&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Header&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="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/books"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Books&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Books&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
         &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/books/the-lord-of-the-rings`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;The Lord of the Rings&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/books/harry-potter`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Harry Potter&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BookItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;match&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;slug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;match&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="nf"&gt;split&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="mi"&gt;2&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Book Item &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&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;With all of these updates we should be able to have this result:&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%2Fsespinoza.me%2Ffiles%2Fimages%2Fbasic-navigation-with-link.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsespinoza.me%2Ffiles%2Fimages%2Fbasic-navigation-with-link.gif" alt="use the Link component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, you can check the progress in the &lt;code&gt;basic-navigation-with-link&lt;/code&gt; branch in the example project.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;match&lt;/code&gt; property is a special property that is passed to all components rendered directly in the &lt;code&gt;Route&lt;/code&gt; component and allows us to get the URL among other properties. In this case, we're using it to the slug from the params in, for example, &lt;code&gt;/books/the-lord-of-the-rings&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I'll end this article here because it's already enough to get started. In a second part I'll focus on folder structure and the use of the &lt;code&gt;withRouter&lt;/code&gt; component to use the &lt;code&gt;math&lt;/code&gt;, &lt;code&gt;history&lt;/code&gt; and &lt;code&gt;location&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;Did you like this article? would you like me to write more about other topics? please drop me a message or leave your comments down below, I'll be more than happy to fix an issue that you found, constructive critics, suggestions, or to simply expand on these ideas.&lt;/p&gt;

&lt;p&gt;You can read the original article posted in my personal web page: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://sespinoza.me/#/articles/601cb01a9a9e502d5a4d09ac" rel="noopener noreferrer"&gt;https://sespinoza.me/#/articles/601cb01a9a9e502d5a4d09ac&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I’m a Software Engineer, writer, tech enthusiast, pianist, origami lover, amateur photographer. In my spare time, I go trekking, play the piano and learn history.&lt;/p&gt;

&lt;p&gt;My tech: JavaScript, Node.js, React, Ruby, Crystal, Bash, Docker.&lt;/p&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/sespinoza_dev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/espinoza-jimenez/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or visit my page to contact &lt;a href="https://sespinoza.me/#/contact/" rel="noopener noreferrer"&gt;me&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>router</category>
      <category>routes</category>
    </item>
    <item>
      <title>Put colour to your terminal prompt</title>
      <dc:creator>sespinoza</dc:creator>
      <pubDate>Wed, 25 Nov 2020 13:16:32 +0000</pubDate>
      <link>https://dev.to/sespinozj/put-colour-to-your-terminal-prompt-5fk5</link>
      <guid>https://dev.to/sespinozj/put-colour-to-your-terminal-prompt-5fk5</guid>
      <description>&lt;p&gt;If you live in the terminal, you may as well be comfortable in it.&lt;/p&gt;

&lt;p&gt;In this article you will learn:&lt;br&gt;
    &lt;/p&gt;
&lt;ul&gt;
        &lt;li&gt;How to set colours to your prompt terminal.&lt;/li&gt;
        &lt;li&gt;How to show the current git branch.&lt;/li&gt;
    &lt;/ul&gt;

&lt;p&gt;What you'll get:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zSyP1tyl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/terminal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zSyP1tyl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/terminal.png" alt="terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the things that I really wanted was to have a customized terminal, there are a handful of shells out there, one of the most popular begin &lt;a href="https://www.zsh.org/"&gt;zhs&lt;/a&gt; popularized by &lt;a href="https://ohmyz.sh/"&gt;oh my zsh&lt;/a&gt;. For some, these are great tools, but for me, that I only want to have some colour and display the git branch... It felt like hitting a tiny nail with Thor's hammer. I knew this could be achieved just by using bash, so I did some digging, here is how to achieve it.&lt;/p&gt;



&lt;h2&gt;
  
  
  Where is the prompt?
&lt;/h2&gt;

&lt;p&gt;Like many things in bash, everything is inside a configuration variable, in this case, the prompt is stored in the &lt;code&gt;PS1&lt;/code&gt; variable, check it out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ echo $PS1
${debian_chroot:+($debian_chroot)}\u@\h:\w\$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most simplistic structure of the above is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="se"&gt;\u&lt;/span&gt;@&lt;span class="se"&gt;\h&lt;/span&gt;:&lt;span class="se"&gt;\w\$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\u&lt;/code&gt; : username&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@&lt;/code&gt; : at symbol&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\h&lt;/code&gt; : hostname&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;W&lt;/code&gt; : working directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$&lt;/code&gt; : # sign if is root or $ sign if normal user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can change this variable however we want. Let's say that we only want to have the working directory in the prompt, we could do that like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PS1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\w\$&lt;/span&gt;&lt;span class="s2"&gt; "&lt;/span&gt;
~/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As it happens, this change won't be permanent, if you open a new terminal you'll see that it's just like when you started.&lt;/p&gt;

&lt;p&gt;Now that we have the bones, lets put some colour, shall we?&lt;/p&gt;



&lt;h2&gt;
  
  
  Colours and styles
&lt;/h2&gt;

&lt;p&gt;The way a terminal access colour and style is by using sequences like &lt;code&gt;\e[92m&lt;/code&gt; for light_green, for example. We could print something with &lt;code&gt;echo -e "\e[92m This text is green"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0UlRfTnq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/green_text_terminal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0UlRfTnq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/green_text_terminal.png" alt="terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-e&lt;/code&gt; option in &lt;em&gt;echo&lt;/em&gt; it just "enable interpretation of backslash escapes" (man page).&lt;/p&gt;

&lt;p&gt;Let's look a bit closer to this sequence: &lt;code&gt;\e[92m&lt;/code&gt;. This is composed of the &lt;strong&gt;Escape character&lt;/strong&gt; which can be accessed with &lt;code&gt;\e&lt;/code&gt;, &lt;code&gt;\033&lt;/code&gt; or &lt;code&gt;\x1B&lt;/code&gt;, then a &lt;code&gt;[&lt;/code&gt; the &lt;em&gt;format code&lt;/em&gt; (92) and &lt;code&gt;m&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here are just a few colours you can choose from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\e[0m&lt;/code&gt;: Reset, removes styles and colours.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\e[30m&lt;/code&gt;: Black&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\e[31m&lt;/code&gt;: Red.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\e[32m&lt;/code&gt;: Green.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\e[33m&lt;/code&gt;: Yellow.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\e[34m&lt;/code&gt;: Blue.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\e[35m&lt;/code&gt;: Magenta.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\e[36m&lt;/code&gt;: Cyan.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So now we could use these in combination with the previous section to have styled our prompt. Let's make the &lt;em&gt;user&lt;/em&gt; and &lt;em&gt;host&lt;/em&gt; blue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export PS1="\e[34m\u@\h\e[0m:\w\$"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2-5DN9Fp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/terminal_color_blue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2-5DN9Fp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/terminal_color_blue.png" alt="terminal"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  The .bashrc configuration file
&lt;/h2&gt;

&lt;p&gt;Alright, now that you know how things work, we can make this our permanent configuration. To do this, we have to write to the &lt;code&gt;~/.bashrc&lt;/code&gt; configuration file.&lt;/p&gt;

&lt;p&gt;Open the file with your favourite editor (vim of course...) and paste the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;bold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[1m"&lt;/span&gt;
&lt;span class="nv"&gt;light_green&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[92m"&lt;/span&gt;
&lt;span class="nv"&gt;blue&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[34m"&lt;/span&gt;
&lt;span class="nv"&gt;yellow&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[33m"&lt;/span&gt;
&lt;span class="nv"&gt;reset_formating&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[0m"&lt;/span&gt;
get_branch&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   git symbolic-ref &lt;span class="nt"&gt;--short&lt;/span&gt; HEAD 2&amp;gt;/dev/null
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PS1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\[&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;bold&lt;/span&gt;&lt;span class="k"&gt;}${&lt;/span&gt;&lt;span class="nv"&gt;light_green&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\]\u&lt;/span&gt;&lt;span class="s2"&gt;@&lt;/span&gt;&lt;span class="se"&gt;\h&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\[&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;blue&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\]\W\[&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;yellow&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\]&lt;/span&gt;&lt;span class="s2"&gt; [&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;(get_branch)]&lt;/span&gt;&lt;span class="se"&gt;\[&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;reset_formating&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\]\$&lt;/span&gt;&lt;span class="s2"&gt; "&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point is pretty clear what we are doing here, so let's go through it: In the first lines, we are just declaring string variables. Then we defined &lt;code&gt;get_branch&lt;/code&gt; which is a function that will execute every time you change from one directory to another and extract the current git branch of that project.&lt;/p&gt;

&lt;p&gt;Note: &lt;code&gt;\[&lt;/code&gt; and &lt;code&gt;\]&lt;/code&gt; don't affect here, just keep the blocks organized.&lt;/p&gt;

&lt;p&gt;Note: &lt;code&gt;"In bash, this is how we access the value of the variable: ${variable_name}"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's it! if you run &lt;code&gt;source .bashrc&lt;/code&gt; or you open a new terminal, you will see your changes. Try to test it by &lt;code&gt;cd&lt;/code&gt; to a directory that has a git repository.&lt;/p&gt;

&lt;p&gt;I hope this was helpful to you. If you find it useful or if you find an issue, just send me a quick email and I'll respond to you as soon as possible.&lt;/p&gt;

&lt;p&gt;All the best!&lt;/p&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.zsh.org/"&gt;zhs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ohmyz.sh/"&gt;oh my zsh&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally published in my personal web page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://sespinoza.me/#/articles/5fbc6e4d9a9e50c1b04d09ab"&gt;https://sespinoza.me/#/articles/5fbc6e4d9a9e50c1b04d09ab&lt;/a&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I’m a Software Engineer, writer, tech enthusiast, pianist, origami lover, amateur photographer. In my spare time, I go trekking, play the piano and learn history.&lt;/p&gt;

&lt;p&gt;My tech: JavaScript, Node.js, React, Ruby, Crystal, Bash, Docker.&lt;/p&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/sespinoza_dev"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/espinoza-jimenez/"&gt;LinkedIn&lt;/a&gt; or visit my page to contact &lt;a href="https://sespinoza.me/#/contact/"&gt;me&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>terminal</category>
      <category>bash</category>
      <category>colours</category>
    </item>
    <item>
      <title>Functional Programming: Filter, Map and Reduce in JS (Advance).</title>
      <dc:creator>sespinoza</dc:creator>
      <pubDate>Thu, 17 Sep 2020 22:49:11 +0000</pubDate>
      <link>https://dev.to/sespinozj/functional-programming-filter-map-and-reduce-in-js-advance-mlf</link>
      <guid>https://dev.to/sespinozj/functional-programming-filter-map-and-reduce-in-js-advance-mlf</guid>
      <description>&lt;h1&gt;
  
  
  Functional Programming: Filter, Map and Reduce in JS (Advance).
&lt;/h1&gt;

&lt;h2&gt;
  
  
  How to use The pillars of functional programming with examples.
&lt;/h2&gt;

&lt;p&gt;These three functions are part of the main operations on arrays in almost every programming language.&lt;/p&gt;

&lt;p&gt;What you need to know before reading this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a callback.&lt;/li&gt;
&lt;li&gt;Some familiarity with ES6 syntax.&lt;/li&gt;
&lt;li&gt;Know how to run javascript code using a REPL in Nodejs or the browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article you will learn:&lt;br&gt;
    &lt;/p&gt;
&lt;ul&gt;
        &lt;li&gt;A better understanding of these functions.&lt;/li&gt;
        &lt;li&gt;Check on complex cases to see their potential.&lt;/li&gt;
    &lt;/ul&gt;

&lt;p&gt;As a side note, this is not a reference manual to use all the arguments these functions have and it's not an introduction on simple use cases.&lt;/p&gt;

&lt;p&gt;To understand how useful they are when working together we'll introduce some data and ask questions. Let us suppose that we are consuming an API from a bookstore that gives us a list of books with attributes such as &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;author&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;price&lt;/code&gt;, number of &lt;code&gt;pages&lt;/code&gt; and a &lt;code&gt;category&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;J.R.R Tolkien&lt;/span&gt;&lt;span class="dl"&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;The lord of the rings, The Fellowship of the Ring&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;8.54&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;555&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fiction&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;J.R.R Tolkien&lt;/span&gt;&lt;span class="dl"&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;The lord of the rings, The Two Towers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;8.34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;467&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fiction&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;J.K. Rowling&lt;/span&gt;&lt;span class="dl"&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;Harry Potter and the Philosopher's Stone&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;8.16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;345&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fiction&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lewis Carroll&lt;/span&gt;&lt;span class="dl"&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;Alice in Wonderland&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;2.70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;86&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fiction&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;C.S. Lewis&lt;/span&gt;&lt;span class="dl"&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;The Chronicles of Narnia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;2.99&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;118&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fiction&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Stephen Hawking&lt;/span&gt;&lt;span class="dl"&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;The universe in a nutshell&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;22.93&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;224&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;science&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;
  
  
  Filter
&lt;/h2&gt;

&lt;p&gt;Filter is a function that &lt;strong&gt;operates over an array&lt;/strong&gt; of elements and &lt;strong&gt;creates a new array&lt;/strong&gt; with the elements that pass the callback test, that is, when the callback returns &lt;code&gt;true&lt;/code&gt; the element is retrieved in the new array.&lt;/p&gt;

&lt;p&gt;This is the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&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="cm"&gt;/* condition */&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;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Usually, we only use the &lt;code&gt;element&lt;/code&gt;. Optional arguments are &lt;code&gt;index&lt;/code&gt; and &lt;code&gt;array&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's ask questions and see how filter can answer them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Give me all books from J.R.R Tolkien.&lt;/li&gt;
&lt;li&gt;Give me all books that worth less than 5 dollars.&lt;/li&gt;
&lt;li&gt;Give me all books with less than 100 pages.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Give me all books from J.R.R Tolkien.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;tolkiens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;J.R.R Tolkien&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*
[
  {
    author: "J.R.R Tolkien",
    title: "The lord of the rings, The Fellowship of the Ring",
    price: 8.54,
    pages: 555,
    category: "fiction"
  },
  {
    author: "J.R.R Tolkien",
    title: "The lord of the rings, The Two Towers",
    price: 8.34,
    pages: 467,
    category: "fiction"
  }
]
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Give me all books that worth less than 5 dollars.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;lessThanFive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*
[
  {
    author: "Lewis Carroll",
    title: "Alice in Wonderland",
    price: 2.70,
    pages: 86,
    category: "fiction"
  },
  {
    author: "C.S. Lewis",
    title: "The Chronicles of Narnia",
    price: 2.99,
    pages: 118,
    category: "fiction"
  }
]
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Give me all books with less than 100 pages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;lessThanAHundred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pages&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*
[
  {
    author: "Lewis Carroll",
    title: "Alice in Wonderland",
    price: 2.70,
    pages: 86,
    category: "fiction"
  },
]
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;Map takes an &lt;strong&gt;array of elements&lt;/strong&gt; and returns a &lt;strong&gt;new array&lt;/strong&gt; of elements transformed by the callback.&lt;/p&gt;

&lt;p&gt;This is the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&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="cm"&gt;/* mapping */&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;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;index&lt;/code&gt; and &lt;code&gt;array&lt;/code&gt; are optional.&lt;/p&gt;

&lt;p&gt;We'll have three examples.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Give me all the titles, that worth less than 5 dollars.&lt;/li&gt;
&lt;li&gt;Export the data to a CSV file.&lt;/li&gt;
&lt;li&gt;Render an array of object in Reactjs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To answer the first question we'll use the filter method to satisfy the condition of &lt;em&gt;less than 5 dollars&lt;/em&gt;, then we'll sort them by &lt;code&gt;author&lt;/code&gt; using the &lt;code&gt;sort&lt;/code&gt; function that will sort them according to the author's name and finally we'll map the books using the &lt;code&gt;title&lt;/code&gt; and the &lt;code&gt;author&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;lessThanFive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;second&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;nameA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&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;nameB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nameA&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nameB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; - &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&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="cm"&gt;/*
[
  'Lewis Carroll - Alice in Wonderland',
  'C.S. Lewis - The Chronicles of Narnia'
]
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;How about if we want to export our data to a &lt;code&gt;.csv&lt;/code&gt; file? we could do that like this using &lt;code&gt;fast-csv&lt;/code&gt;. Place the following in an &lt;code&gt;index.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;csv&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;fast-csv&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;ws&lt;/span&gt; &lt;span class="o"&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;createWriteStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;books.csv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;csv&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="p"&gt;[[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;author&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;price&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pages&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;category&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;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&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;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After running this script with &lt;code&gt;node index.js&lt;/code&gt; we'll find our &lt;code&gt;book.csv&lt;/code&gt; file created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;books.csv 
&lt;span class="c"&gt;# id,author,title,price,pages,category&lt;/span&gt;
&lt;span class="c"&gt;# J.R.R Tolkien,"The lord of the rings, The Fellowship of the Ring",8.54,555,fiction,&lt;/span&gt;
&lt;span class="c"&gt;# J.R.R Tolkien,"The lord of the rings, The Two Towers",8.34,467,fiction,&lt;/span&gt;
&lt;span class="c"&gt;# J.K. Rowling,"Harry Potter and the Philosopher's Stone",8.16,345,fiction,&lt;/span&gt;
&lt;span class="c"&gt;# Lewis Carroll,Alice in Wonderland,2.7,86,fiction,&lt;/span&gt;
&lt;span class="c"&gt;# C.S. Lewis,The Chronicles of Narnia,2.99,118,fiction,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, Map can be especially useful when rendering react components, for example, this is how we could render these books in the front end using &lt;code&gt;JSX&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"books-wrapper"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="si"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"book"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;by &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pages&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; pages&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;strong&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;$ &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;strong&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Reduce
&lt;/h2&gt;

&lt;p&gt;Reduce is regarded as the most complicated of the three, but we'll get to understand how it works step by step. First, the definition:&lt;/p&gt;

&lt;p&gt;Reduce operates over an array and returns a single value. It performs operations over each element and saves the result using an accumulated value. Then it returns that accumulated value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;callBack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentValue&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="cm"&gt;/* return the next accumulator value */&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;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note: Reduce's callback can also receive an &lt;code&gt;index&lt;/code&gt; and the &lt;code&gt;array&lt;/code&gt; as optional parameters.&lt;/p&gt;

&lt;p&gt;Let us ask some questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Tell me how much will it cost me to buy all &lt;em&gt;J.R.R Tolkien&lt;/em&gt; books in the store.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is the most basic use of reduce, to add elements in an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;tolkiensBooks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;J.R.R Tolkien&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;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;second&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;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; 16.88&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;A story:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I have three days off and I want to spend my time reading.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I like fiction.&lt;/li&gt;
&lt;li&gt;I prefer to buy as many books as I can.&lt;/li&gt;
&lt;li&gt;I have 20 dollars to spend on books.&lt;/li&gt;
&lt;li&gt;I read from 11:00 to 18:00.&lt;/li&gt;
&lt;li&gt;My speed read is 250 wpm (words per minute).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which books should I buy?&lt;/p&gt;

&lt;p&gt;Alright, that's a lot, to eat the elephant you have to start piece by piece, right?&lt;/p&gt;

&lt;p&gt;First, this person wants to buy fiction literature, so a simple filter will do &lt;code&gt;books.filter(book =&amp;gt; book.category === "fiction" )&lt;/code&gt;. Then we have two restrictions: the budget and the time he has to read. And finally, as he wants to buy &lt;em&gt;as many books as he can&lt;/em&gt; that gives us the idea that we have to choose the shipper books first until we get out of money or until we think we won't have the time to finish them in three days.&lt;/p&gt;

&lt;p&gt;To tackle this problem we'll use &lt;code&gt;reduce&lt;/code&gt; with a state that holds how much we are spending and how much time we are requiring as we buy book by book. This is the structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;readingTimeLeft&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="nx"&gt;titles&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;Harry Potter and the Philosopher's Stone&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Lord of the rings, The Two Towers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="nx"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;19.0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we iterate over the books, we'll be subtracting our reading time left &lt;code&gt;readingTimeLeft&lt;/code&gt; and adding to the &lt;code&gt;bill&lt;/code&gt; that we'll have to pay. In the end, we'll have our list of &lt;code&gt;titles&lt;/code&gt; that we'll buy.&lt;/p&gt;

&lt;p&gt;First, we need to perform some calculation, we want to define the variable &lt;code&gt;PAGES_PER_HOUR&lt;/code&gt;. He can read &lt;code&gt;250&lt;/code&gt; words per minute (the average), that is &lt;code&gt;15000 [word/hour]&lt;/code&gt;. A book, let's say, have &lt;code&gt;400 [word/page]&lt;/code&gt; words per page, it can vary, but that will be our estimation. So, he can read a total of &lt;code&gt;37.5 [page/hour]&lt;/code&gt; pages per hour. (&lt;code&gt;15000 [word/hour]/ 400 [word/page] = 37.5 [page/hour]&lt;/code&gt;). Excellent we have our speeding rate.&lt;/p&gt;

&lt;p&gt;If he can read three days from 11hrs to 18hrs, he has a total of &lt;code&gt;21 [hour]&lt;/code&gt;, so we have our &lt;code&gt;readingTimeLeft&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, we can code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;PAGES_PER_HOUR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;37.5&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;BUDGET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;20.00&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;initialStructure&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;readingTimeLeft&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// hours, 7 hrs, per 3 days.&lt;/span&gt;
  &lt;span class="na"&gt;titles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;bill&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;summary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fiction&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;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;second&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;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;current&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;readingTimeLeftAfterCal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readingTimeLeft&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pages&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;PAGES_PER_HOUR&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;billAfterCal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bill&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;readingTimeLeftAfterCal&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// we run out of time&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;billAfterCal&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;BUDGET&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;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// we run out of budget&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;readingTimeLeft&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;readingTimeLeftAfterCal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;titles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;titles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="c1"&gt;// we add the title&lt;/span&gt;
      &lt;span class="na"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;billAfterCal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="c1"&gt;// we round to two decimals&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;initialStructure&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A quick explanation on &lt;code&gt;readingTimeLeftAfterCal&lt;/code&gt; may be needed. We need to subtract the time that will take us reading that current book, to do that we need to subtract hours, that is &lt;code&gt;(current.pages [page] * (1 / PAGES_PER_HOUR [page/hour]))&lt;/code&gt; as our rate is &lt;code&gt;[page/hour]&lt;/code&gt; we need to invert it to have &lt;code&gt;[hour/page]&lt;/code&gt; to cancel the pages and have the hours.&lt;/p&gt;

&lt;p&gt;In each iteration we would have this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;readingTimeLeft&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;titles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;readingTimeLeft&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;18.706666666666667&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;titles&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;Alice in Wonderland&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;2.7&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;readingTimeLeft&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;15.56&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;titles&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;Alice in Wonderland&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;The Chronicles of Narnia&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;5.69&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;readingTimeLeft&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;6.359999999999999&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;titles&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;Alice in Wonderland&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;The Chronicles of Narnia&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;Harry Potter and the Philosopher's Stone&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;13.85&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see the last object which is saved in &lt;code&gt;summary&lt;/code&gt; give us all that we need: The titles that we want to buy and how much we need to pay. Of course if you wan you can reverse the &lt;code&gt;readingTimeLeft&lt;/code&gt; left so that you can have how much time you need to finish all those books. I leave that to you ;)&lt;/p&gt;

&lt;p&gt;I hope this article was useful to you, If you liked it, if you have a suggestion or if you found a mistake, please leave a comment or send me an email, I'll be grateful.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"&gt;Filter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;Map&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;Reduce&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort"&gt;Sort&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Originally posted in my &lt;a href="https://sespinoza.me/#/articles/5f6387ce9a9e50fef54d09a9"&gt;page&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I’m a Software Engineer, writer, tech enthusiast, pianist, origami lover, amateur photographer. In my spare time, I go trekking, play the piano and learn history.&lt;/p&gt;

&lt;p&gt;My tech: JavaScript, Node.js, React, Ruby, Crystal, Bash, Docker.&lt;/p&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/sespinoza_dev"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/espinoza-jimenez/"&gt;LinkedIn&lt;/a&gt; or visit my page to contact &lt;a href="https://sespinoza.me/#/contact/"&gt;me&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>filter</category>
      <category>map</category>
      <category>reduce</category>
    </item>
    <item>
      <title>RESTful APIs - Introduction to Express</title>
      <dc:creator>sespinoza</dc:creator>
      <pubDate>Mon, 31 Aug 2020 15:25:36 +0000</pubDate>
      <link>https://dev.to/sespinozj/restful-apis-introduction-to-express-5hld</link>
      <guid>https://dev.to/sespinozj/restful-apis-introduction-to-express-5hld</guid>
      <description>&lt;p&gt;In this article, you will learn to write a simple RESTful API using &lt;code&gt;Node.js&lt;/code&gt; and &lt;code&gt;Express&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This article will be useful if you are familiar with these concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ES6 syntax: arrow function, object notation, string templates.&lt;/li&gt;
&lt;li&gt;callbacks.&lt;/li&gt;
&lt;li&gt;JSON format.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;What we'll cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting up the project.&lt;/li&gt;
&lt;li&gt;Adding express.&lt;/li&gt;
&lt;li&gt;Running the server.&lt;/li&gt;
&lt;li&gt;Introducing middlewares.&lt;/li&gt;
&lt;li&gt;jq&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;This article does not cover the design of RESTful APIs. We'll cover that in another article. The aim of this article is to introduce you to the &lt;a href="https://expressjs.com/"&gt;Express&lt;/a&gt; web framework and show you how great is for creating RESTful APIs. I use it often when I have to provide testing APIs for client's integrations or when I'm developing full stack project and the front client needs to consume an API.&lt;/p&gt;

&lt;p&gt;All sorts of microservices can be made with &lt;code&gt;Node.js&lt;/code&gt; and &lt;code&gt;Express&lt;/code&gt;, within a few minutes you can have a nice and small server listening for requests. In this article, I’ll go through the whole process of setting up the project and running the server for the first time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the project
&lt;/h2&gt;

&lt;p&gt;Let’s start by creating the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;micro-server
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;micro-server
&lt;span class="nv"&gt;$ &lt;/span&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see &lt;code&gt;npm init&lt;/code&gt; creates a &lt;code&gt;package.json&lt;/code&gt;, this is the configuration file for our project. It contains scripts and dependencies. The &lt;code&gt;-y&lt;/code&gt; option tells &lt;code&gt;npm&lt;/code&gt; to autocomplete the questions that otherwise would ask you. Let's add some elements to our &lt;code&gt;package.json&lt;/code&gt; file, open your favourite editor and add the &lt;code&gt;author&lt;/code&gt;, the &lt;code&gt;description&lt;/code&gt; and a &lt;code&gt;start&lt;/code&gt; script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"temp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"A hello world example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node ."&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"S.Espinoza"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding Express.
&lt;/h2&gt;

&lt;p&gt;To add &lt;code&gt;express&lt;/code&gt; to the dependencies is straight forward by running &lt;code&gt;npm install express --save&lt;/code&gt;. This will create and install &lt;code&gt;express&lt;/code&gt; inside a &lt;code&gt;node_modules&lt;/code&gt; folder and the &lt;code&gt;--save&lt;/code&gt; option will save the stable version of &lt;code&gt;Express&lt;/code&gt; to our dependencies in the &lt;code&gt;package.json&lt;/code&gt;. With &lt;code&gt;express&lt;/code&gt; added, we now proceed to create an &lt;code&gt;index.js&lt;/code&gt; file and start using express.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;index.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Open your editor and put this in your file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;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="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;/user/:name&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="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;status&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="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Hi &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&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="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;status&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="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;These are not the droids you are looking for&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4000&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="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;listening in on 4000&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;In general, every method in &lt;code&gt;express&lt;/code&gt; receives as arguments a &lt;em&gt;path&lt;/em&gt; and a &lt;em&gt;callback&lt;/em&gt;, except &lt;code&gt;listen&lt;/code&gt; which receives the port to listen as the first argument. Once the request matches one of the methods, it executes its callback.&lt;/p&gt;

&lt;p&gt;An instance of express (&lt;code&gt;app&lt;/code&gt;) has may methods, with different arguments. Here are some of the most important.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;method&lt;/th&gt;
&lt;th&gt;arguments&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;get&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(path, callback)&lt;/td&gt;
&lt;td&gt;match &lt;code&gt;GET&lt;/code&gt; request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;post&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(path, callback)&lt;/td&gt;
&lt;td&gt;match &lt;code&gt;POST&lt;/code&gt; request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;put&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(path, callback)&lt;/td&gt;
&lt;td&gt;match &lt;code&gt;PUT&lt;/code&gt; request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;delete&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(path, callback)&lt;/td&gt;
&lt;td&gt;match &lt;code&gt;DELETE&lt;/code&gt; request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;all&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(path, callback)&lt;/td&gt;
&lt;td&gt;match all request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;listen&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(port, callback)&lt;/td&gt;
&lt;td&gt;start the server in &lt;code&gt;port&lt;/code&gt; and then executes a callback&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In our example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;app.listen&lt;/code&gt; start the server to listen to requests on port &lt;code&gt;4000&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app.get&lt;/code&gt; match requests to &lt;code&gt;localhost:4000/user/:name&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app.all&lt;/code&gt; match everything that doesn't match all the previews methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yes, the order in which you write you matches matter, they should be structured top to bottom according to it's relevance.&lt;/p&gt;

&lt;h3&gt;
  
  
  The callback
&lt;/h3&gt;

&lt;p&gt;In our callback for the main methods, excluding &lt;code&gt;listen&lt;/code&gt;, the first parameter is &lt;em&gt;the path&lt;/em&gt; which in our case is &lt;code&gt;/user/:name&lt;/code&gt; and the second parameter is the callback that receives two arguments: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;request&lt;/code&gt; object &lt;code&gt;req&lt;/code&gt; holds all the data that the user sends in this case, the &lt;code&gt;params&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;response&lt;/code&gt; object &lt;code&gt;res&lt;/code&gt; formats the response to be sent to the user.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Running the server.
&lt;/h2&gt;

&lt;p&gt;Let's run the server now. For that we could type &lt;code&gt;node index.js&lt;/code&gt; or &lt;code&gt;node .&lt;/code&gt; but as we added a start script in our &lt;code&gt;package.json&lt;/code&gt; we can run our server with &lt;code&gt;npm start&lt;/code&gt;. The message &lt;code&gt;listening in on 4000&lt;/code&gt; should appear if everything went well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm start

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; micro-server@1.0.0 start /home/sam/micro-server
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; node &lt;span class="nb"&gt;.&lt;/span&gt;

listening &lt;span class="k"&gt;in &lt;/span&gt;on 4000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now we can test it using &lt;code&gt;curl&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl localhost:4000/user/Alexander
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"msg"&lt;/span&gt;:&lt;span class="s2"&gt;"Hi Alexander"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great! we are up and running. If we forgot to the add the param or if we try to place any other path we would get Owi One's words:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl localhost:4000/this/path/does/not/exist
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"msg"&lt;/span&gt;:&lt;span class="s2"&gt;"These are not the droids you are looking for"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Introducing middlewares.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Understanding middlewares
&lt;/h3&gt;

&lt;p&gt;A middleware &lt;em&gt;is a piece of code that executes sequentially before hitting the last callback to respond to the user&lt;/em&gt;, this is useful, for example, to parse the content in the request object or for authentication proposes. Here we'll make a little example in our code and then we'll add some useful Third-party libraries to use as middlewares.&lt;/p&gt;

&lt;p&gt;A middleware is a function with the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="cm"&gt;/* do something*/&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once a middleware finish whatever it does, it should always call the &lt;code&gt;next()&lt;/code&gt; function so that the next middleware is called. You can have multiple middlewares that performs different tasks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* do something*/&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* do something*/&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="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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For our example we'll use something very simple: put the greeting message in the request object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;formatGreetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hi &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, coffee?`&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;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;/user/:name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;formatGreetings&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="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;status&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="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;msg&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;greetings&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&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="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;status&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="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;These are not the droids you are looking for&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4000&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="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;listening in on 4000&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 we test it again&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl localhost:4000/user/Alexander
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"msg"&lt;/span&gt;:&lt;span class="s2"&gt;"Hi Alexander, coffee?"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Excellent!&lt;/p&gt;

&lt;h3&gt;
  
  
  Third-party libraries
&lt;/h3&gt;

&lt;p&gt;Our example as it is wont handle &lt;em&gt;Cross-Origin Resource Sharing&lt;/em&gt; (&lt;a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing"&gt;CORS&lt;/a&gt;) nor will format well the body of POST requests. So we need to add two more libraries to our dependencies: &lt;code&gt;cors&lt;/code&gt; and &lt;code&gt;body-parser&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;cors body-parser &lt;span class="nt"&gt;--save&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's add a new POST match just before our GET.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bparser&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;body-parser&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;cors&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;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cors&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;bparser&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt; &lt;span class="c1"&gt;// parse JSON&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;bparser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;extended&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="c1"&gt;// parse URL-encoded&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formatGreetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hi &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, coffee?`&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&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="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;status&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="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`creating user &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="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;/user/:name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;formatGreetings&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="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;status&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="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Hi &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&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="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;status&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="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;These are not the droids you are looking for&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4000&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="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;listening in on 4000&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 if we test it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nt"&gt;-XPOST&lt;/span&gt; &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s1"&gt;'Content-type: application/json'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{ "name": "sam" }'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  localhost:4000/
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"msg"&lt;/span&gt;:&lt;span class="s2"&gt;"creating user sam"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Perfect. The &lt;code&gt;app.use()&lt;/code&gt; call tells express to use a top-level middleware before every request. In this case to parse JSON and URL-encoded.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;urlencoded&lt;/code&gt; option in the &lt;code&gt;body-parser&lt;/code&gt; just tells the middleware how to parse the body. Here I quote from the docs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body). This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  jq
&lt;/h2&gt;

&lt;p&gt;If you are like me and you always consume APIs in the terminal, one of the best tools I've found is &lt;code&gt;jq&lt;/code&gt;. It's an excellent parser and if you have a huge JSON object it's very useful. Here is an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-s&lt;/span&gt; localhost:4000/Alexander | jq &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"msg"&lt;/span&gt;: &lt;span class="s2"&gt;"Hi Alexander"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-s&lt;/span&gt; localhost:4000/Alexander | jq .msg
&lt;span class="s2"&gt;"Hi Alexander"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it for this article, I really hope this was useful for you, if you think there's a mistake in this article or if there's room to improve it let me know in the comments.&lt;/p&gt;

&lt;p&gt;If you want me to talk about a specific topic just leave a comment or contact me through &lt;a href="https://sespinoza.me/#/contact/"&gt;my page&lt;/a&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://expressjs.com/"&gt;Express&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://curl.haxx.se/"&gt;curl&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/JSON"&gt;JSON&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing"&gt;CORS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/expressjs/body-parser"&gt;Body-parser&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally posted in:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://sespinoza.me/#/articles/5f4362f6634b827bac0f43c9"&gt;https://sespinoza.me/#/articles/5f4362f6634b827bac0f43c9&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I’m a Software Engineer, writer, tech enthusiast, pianist, origami lover, amateur photographer. In my spare time, I go trekking, play the piano and learn history.&lt;/p&gt;

&lt;p&gt;My tech: JavaScript, Node.js, React, Ruby, Crystal, Bash, Docker.&lt;/p&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/sespinoza_dev"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/espinoza-jimenez/"&gt;LinkedIn&lt;/a&gt; or visit my page to contact &lt;a href="https://sespinoza.me/#/contact/"&gt;me&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>restful</category>
      <category>middleware</category>
    </item>
    <item>
      <title>Git. Write quality commits.</title>
      <dc:creator>sespinoza</dc:creator>
      <pubDate>Mon, 17 Aug 2020 14:49:41 +0000</pubDate>
      <link>https://dev.to/sespinozj/git-write-quality-commits-38ph</link>
      <guid>https://dev.to/sespinozj/git-write-quality-commits-38ph</guid>
      <description>&lt;p&gt;To write good quality commit messages will save you from the wrath of the next maintainer of your project. Your future self will also be grateful when there is a bug and you'll have to sail through all that history.&lt;/p&gt;

&lt;p&gt;In this article you will learn:&lt;br&gt;
    &lt;/p&gt;
&lt;ul&gt;
        &lt;li&gt;How Git messages are structured&lt;/li&gt;
        &lt;li&gt;How to write quality commit messages.&lt;/li&gt;
        &lt;li&gt;How to use a template to help you with that.&lt;/li&gt;
        &lt;li&gt;How to use Git Aliases.&lt;/li&gt;
    &lt;/ul&gt;

&lt;h3&gt;
  
  
  Git message structure
&lt;/h3&gt;

&lt;p&gt;Git messages are compound of two parts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Header or summary&lt;/li&gt;
&lt;li&gt;Body&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you are commiting using &lt;strong&gt;git commit&lt;/strong&gt; the default editor opens and waits for you to write a commit message, if you are using Git for the first time it's probably &lt;strong&gt;vi&lt;/strong&gt;, check at the end of the article for a quick intro. The first line is the &lt;em&gt;Header&lt;/em&gt; and after an empty line comes the &lt;em&gt;Body&lt;/em&gt; of a commit. This separation is intentional so that you can use options like &lt;strong&gt;git log --oneline&lt;/strong&gt; to list only the &lt;em&gt;Headers&lt;/em&gt; of the commit messages.&lt;/p&gt;

&lt;p&gt;Here an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DRf1H8JT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/commit_message_parts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DRf1H8JT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/commit_message_parts.png" alt="tree node one commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Everything with &lt;strong&gt;#&lt;/strong&gt; is interpreted as a comment and it's not added to the actual message.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quality commit messages
&lt;/h3&gt;

&lt;p&gt;To write good quality messages you first need to understand that commits are meant to hold small changes. So it's a good practice to edit fewer files and commiting changes often.&lt;/p&gt;

&lt;p&gt;When writing commits you have to have two things in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What changed&lt;/li&gt;
&lt;li&gt;Why the change is needed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You answer the first question in the &lt;em&gt;Header&lt;/em&gt; using &lt;em&gt;imperative language&lt;/em&gt;. Here are some examples of &lt;em&gt;Headers&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;[Add]&lt;/strong&gt; formatTime function to display dates in 'HH:mm' format.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[Fix]&lt;/strong&gt; formatBlocks function. missing slug attribute.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[Deprecate]&lt;/strong&gt; REST API. now interactions use socket.io only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[Document]&lt;/strong&gt; Add Swagger documentation for Articles API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[Remove]&lt;/strong&gt; phoenix-channels lib. The new protocol uses amqplib.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[Update]&lt;/strong&gt; Articles main font to Roboto.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[Secure]&lt;/strong&gt; Author's REST routes /author/articles/:id&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then you can answer the second question using &lt;em&gt;descriptive language&lt;/em&gt; and listing the reasons why the change was necessary. Let us see a full example using the first &lt;em&gt;Header&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Add] formatTime function to display dates in 'HH:mm' format.

- this is a helper function utility for the scheduler module.
- it uses moment.js to parse date formats using a particular timezone.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that looks quite good. Some companies enforce commit structures and ask you to refer to the id of the issue. You can append it to the action or at the end of the &lt;em&gt;Header&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git Template message
&lt;/h3&gt;

&lt;p&gt;As you continue to write new features it's good to have a template to use it every time you want to make a commit. Let us create that template.&lt;/p&gt;

&lt;p&gt;We'll create the files with the &lt;strong&gt;touch&lt;/strong&gt; command and open the file with the &lt;strong&gt;&lt;a href="https://www.nano-editor.org/"&gt;nano&lt;/a&gt;&lt;/strong&gt; editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch&lt;/span&gt; .git-commit-template
&lt;span class="nv"&gt;$ &lt;/span&gt;nano .git-commit-template

&lt;span class="c"&gt;# paste the following within the file:&lt;/span&gt;
&lt;span class="c"&gt;#[Add/Fix/Remove/Update/Refactor/Document/Secure/Deprecate]&lt;/span&gt;
&lt;span class="c"&gt;#[]&lt;/span&gt;

&lt;span class="c"&gt;# Why is it necessary? (Bug fix, feature, improvements?)&lt;/span&gt;
&lt;span class="c"&gt;#-&lt;/span&gt;

&lt;span class="c"&gt;# [issue #id]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that we have our template ready, we tell Git to use it. For that, we need to know the absolute path to that file. you can use &lt;em&gt;&lt;a href="https://www.computerhope.com/unix/upwd.htm"&gt;pwd&lt;/a&gt;&lt;/em&gt; to display it, in my case is &lt;strong&gt;/home/sam&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;
&lt;span class="c"&gt;# /home/sam&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git config commit.template /home/sam/.git-commit-template
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All your git configurations are saved in the &lt;strong&gt;~/.gitconfig&lt;/strong&gt; file. Mine looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[user]
    name = sespinoza
    email = espinoza.jimenez@gmail.com

[core]
    editor = vim

[commit]
    template = /home/sam/.git-commit-template
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you want to get rid of the template just delete the last two lines related to &lt;strong&gt;[commit]&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git alias
&lt;/h3&gt;

&lt;p&gt;Now that we know how to write quality commits we can list them in a compact comfortable manner using &lt;strong&gt;git log --oneline&lt;/strong&gt;. But we can do better, we can show more information like the date of the commit and the author.&lt;/p&gt;

&lt;p&gt;For example, you can get to a project and use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git log --pretty=format:'%h - %ad - %aN - %s' --date=short
fa64fad - 2020-08-15 - sespinoza - [Fix] Header's css. On mobile screen resolutions expand menu actions width to a 100%.
4240b85 - 2020-08-15 - sespinoza - [Add] login and logout action in dropdown menu when is in mobile screen resolution.
f3abfac - 2020-08-15 - sespinoza - [Update] Header. Colors, grouped user actions and compatibility with different screen size resolutions.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can use an &lt;strong&gt;alias&lt;/strong&gt; so that we don't have to type that command every time. You can add an alias in your &lt;strong&gt;.gitconfig&lt;/strong&gt; file, my complete configuration looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;user]
    name &lt;span class="o"&gt;=&lt;/span&gt; sespinoza
    email &lt;span class="o"&gt;=&lt;/span&gt; espinoza.jimenez@gmail.com

&lt;span class="o"&gt;[&lt;/span&gt;core]
    editor &lt;span class="o"&gt;=&lt;/span&gt; vim

&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;alias&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
   lg &lt;span class="o"&gt;=&lt;/span&gt; log &lt;span class="nt"&gt;--color&lt;/span&gt; &lt;span class="nt"&gt;--pretty&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;format:&lt;span class="s1"&gt;'%Cred%h%Creset - %Cblue%ad%Creset - %aN - %s'&lt;/span&gt; &lt;span class="nt"&gt;--date&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;short

&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;alias&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    changelog &lt;span class="o"&gt;=&lt;/span&gt; log &lt;span class="nt"&gt;--decorate&lt;/span&gt; &lt;span class="nt"&gt;--date&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;format:&lt;span class="s1"&gt;'%B %d, %Y'&lt;/span&gt; &lt;span class="nt"&gt;--pretty&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;format:&lt;span class="s1"&gt;'%ad: %s (%h)'&lt;/span&gt; &lt;span class="nt"&gt;--abbrev-commit&lt;/span&gt;

&lt;span class="o"&gt;[&lt;/span&gt;commit]
    template &lt;span class="o"&gt;=&lt;/span&gt; /home/sam/.git-commit-template.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Instead of writing &lt;strong&gt;git log --pretty=format:'%h - %ad - %aN - %s' --date=short&lt;/strong&gt; you can now just type &lt;strong&gt;git lg&lt;/strong&gt;. I add some colors here, you can customize this however you wish using this &lt;a href="https://git-scm.com/docs/pretty-formats"&gt;resource&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's it for today. I wish you a happy and productive life.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extra: vi
&lt;/h3&gt;

&lt;p&gt;If you are using Git for the first time it's probably that your default command editor is &lt;strong&gt;vi&lt;/strong&gt; of &lt;strong&gt;vim&lt;/strong&gt;. So here is the basic to use it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vi&lt;/strong&gt; uses &lt;em&gt;modes&lt;/em&gt;: One mode to read, another to write, and so on. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To use the edit mode press &lt;strong&gt;i&lt;/strong&gt; and start writing you commit.&lt;/li&gt;
&lt;li&gt;To save and exit press &lt;strong&gt;ESC&lt;/strong&gt; then type &lt;strong&gt;:wq&lt;/strong&gt; and hit Enter. This will &lt;em&gt;write&lt;/em&gt; and &lt;em&gt;quit&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://git-scm.com/docs/pretty-formats"&gt;Git format&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/unix/unix-vi-editor.htm"&gt;Vi editor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.nano-editor.org/"&gt;nano editor&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I’m a Software Engineer, writer, tech enthusiast, pianist, origami lover, amateur photographer. In my spare time, I go trekking, play the piano and learn history.&lt;/p&gt;

&lt;p&gt;My tech: JavaScript, Node.js, React, Ruby, Crystal, Bash, Docker.&lt;/p&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/sespinoza_dev"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/espinoza-jimenez/"&gt;LinkedIn&lt;/a&gt; or visit my page to contact &lt;a href="https://sespinoza.me/#/contact/"&gt;me&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Aknowlegments
&lt;/h2&gt;

&lt;p&gt;&lt;span&gt;Photo by &lt;a href="https://unsplash.com/@helloimnik?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Hello I'm Nik 🎞&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>beginners</category>
      <category>productivity</category>
      <category>development</category>
    </item>
    <item>
      <title>Git. A simple introduction</title>
      <dc:creator>sespinoza</dc:creator>
      <pubDate>Mon, 10 Aug 2020 14:35:56 +0000</pubDate>
      <link>https://dev.to/sespinozj/git-a-simple-introduction-11nm</link>
      <guid>https://dev.to/sespinozj/git-a-simple-introduction-11nm</guid>
      <description>&lt;p&gt;Git has a lot to offer, sometimes is overwhelming, for that reason it's good to have a practical approach and explanations along the way to grasp the essentials of this magnificent tool.&lt;/p&gt;

&lt;p&gt;This is the first part of a series of articles to introduce you to Git and then to level up your working development workflow using git.&lt;/p&gt;

&lt;p&gt;Content.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Git history (brief).&lt;/li&gt;
&lt;li&gt;Git workflow.&lt;/li&gt;
&lt;li&gt;Hands-on Git.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Git history (brief)
&lt;/h2&gt;

&lt;blockquote&gt;
    git - the stupid content tracker
    —&lt;cite&gt;man page&lt;/cite&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;The problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A software is a piece of code that grows, in every new feature added a tag is placed in it and you get new versions. Eventually, the owner needs to share that code and allow contributions from other people to make that software better (or worst...). How to achieve this? for the first part, the answer is: Version Control System (VCS) to track the changes. For the second part we have two possible answers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;having a centralized system that keeps track of all the changes in your code.&lt;/li&gt;
&lt;li&gt;having a distributed system where all the code is shared among contributors that work &lt;strong&gt;merging&lt;/strong&gt; changes.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, what Git is? is a Distributed Version Control System&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Brief History&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In April 2005, Linus Torvalds was working on the 2.6.12 version of his Linux operating system, not pleased with the current VCS available in the market to help him manage his code (&lt;a href="https://www.bitkeeper.org/"&gt;BitKeeper&lt;/a&gt; as the main one), he decided that he needed to create it's own to keep track of changes of the Linux core. This system had to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Distributed&lt;/li&gt;
&lt;li&gt;Fast&lt;/li&gt;
&lt;li&gt;Reliable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And... he did it. &lt;strong&gt;&lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt;&lt;/strong&gt; was created. Since then it has been growing almost universally to manage version control and team contributions.&lt;/p&gt;

&lt;p&gt;For the sake of brevity, these are the main things to know.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git, workflow
&lt;/h2&gt;

&lt;p&gt;So, how does git works? There are key concepts surrounding Git.&lt;/p&gt;

&lt;p&gt;Our files live in three areas or states, those are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Untracked or modified.&lt;/li&gt;
&lt;li&gt;Staging area.&lt;/li&gt;
&lt;li&gt;Committed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever we want to &lt;em&gt;commit&lt;/em&gt; our changes on certain files we &lt;em&gt;add&lt;/em&gt; those files to the &lt;em&gt;staging area&lt;/em&gt; and then &lt;em&gt;commit&lt;/em&gt; them. Once committed all your changes are saved in the Git history, so whatever you do you can come back to that point later if you need it.&lt;/p&gt;

&lt;p&gt;Scenario:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You are starting working in a project and you want to keep track of changes in your code, so what you do is creating a &lt;em&gt;local repository&lt;/em&gt; and &lt;em&gt;add&lt;/em&gt; your code to that repository. (&lt;strong&gt;git init&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;The second thing you want is to select the files that you want to keep track of and add them to the staging area for committing them later. (&lt;strong&gt;git add [file, ]&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Finally, after adding all the file that you changed you need to &lt;em&gt;commit&lt;/em&gt; your work to create a &lt;em&gt;commit&lt;/em&gt; in your Git history. (&lt;strong&gt;git commit -m "Initial commit"&lt;/strong&gt;). &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qj4xiomI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/git_areas.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qj4xiomI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/git_areas.png" alt="tree node one commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Up to this point, there is only one commit in Git history. This history is saved in the form of a tree, in this case, there is only one node. This node is the starting point of a mainline of development (&lt;em&gt;branch&lt;/em&gt;) that is being tracked by default, that is called the "&lt;em&gt;master branch&lt;/em&gt;". &lt;/p&gt;

&lt;h2&gt;
  
  
  Hands-on Git
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Initial configuration
&lt;/h3&gt;

&lt;p&gt;Open a terminal.&lt;/p&gt;

&lt;p&gt;Install git first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;git-all
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let us tell git who we are so our signature is in all our commits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"Your Name"&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"youremail@yourdomain.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great! now the basics.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basics
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Create a local repository
&lt;/h4&gt;

&lt;p&gt;In a project create a Git repository using &lt;strong&gt;git init&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;awesome-project &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;awesome-project
&lt;span class="nv"&gt;$ &lt;/span&gt;git init
Initialized empty Git repository &lt;span class="k"&gt;in&lt;/span&gt; /home/user/awesome-project/.git/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Congratulations! you have created your first repository. You can actually see it... in the &lt;strong&gt;.git&lt;/strong&gt; folder. If by any reason you want to get rid of it you just have to delete that folder (&lt;strong&gt;rm -rf .git&lt;/strong&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ls -a
.  ..  .git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At this point there is nothing in your history, you can check it out with &lt;strong&gt;git status&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git status
On branch master

No commits yet

nothing to commit &lt;span class="o"&gt;(&lt;/span&gt;create/copy files and use &lt;span class="s2"&gt;"git add"&lt;/span&gt; to track&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see you are in the &lt;em&gt;master branch&lt;/em&gt;. Let us create two files, put something on them and then make our first commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"console.log('hello')"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; hello.js
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"console.log('hello')"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; world.js
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;hello.js 
console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we check the status again we will see that our files are not being tracked yet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git status
On branch master

No commits yet

Untracked files:
 &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add &amp;lt;file&amp;gt;..."&lt;/span&gt; to include &lt;span class="k"&gt;in &lt;/span&gt;what will be committed&lt;span class="o"&gt;)&lt;/span&gt;

    hello.js
    world.js

nothing added to commit but untracked files present &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add"&lt;/span&gt; to track&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Staging your files
&lt;/h4&gt;

&lt;p&gt;Git is telling us is not tracking those files. Let us add only one of them to the staging area and check the status once more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git add hello.js 
&lt;span class="nv"&gt;$ &lt;/span&gt;git status
On branch master

No commits yet

Changes to be committed:
&lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git rm --cached &amp;lt;file&amp;gt;..."&lt;/span&gt; to unstage&lt;span class="o"&gt;)&lt;/span&gt;

        new file:   hello.js

Untracked files:
 &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add &amp;lt;file&amp;gt;..."&lt;/span&gt; to include &lt;span class="k"&gt;in &lt;/span&gt;what will be committed&lt;span class="o"&gt;)&lt;/span&gt;

    world.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Perfect, not we are ready for committing &lt;em&gt;index.js&lt;/em&gt; to the Git history.&lt;/p&gt;

&lt;h4&gt;
  
  
  Commiting
&lt;/h4&gt;

&lt;p&gt;As you can see now &lt;em&gt;hello.js&lt;/em&gt; is in the &lt;em&gt;staging area&lt;/em&gt; about to be committed and &lt;em&gt;world.js&lt;/em&gt; is being untracked. Now, let us &lt;em&gt;commit&lt;/em&gt; the &lt;em&gt;hello.js&lt;/em&gt; file and add it to the git history.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Initial commit, add hello.js"&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;master &lt;span class="o"&gt;(&lt;/span&gt;root-commit&lt;span class="o"&gt;)&lt;/span&gt; 15008ea] Initial commit, add hello.js
1 file changed, 1 insertion&lt;span class="o"&gt;(&lt;/span&gt;+&lt;span class="o"&gt;)&lt;/span&gt;
create mode 100644 hello.js

&lt;span class="nv"&gt;$ &lt;/span&gt;git status
On branch master
Untracked files:
&lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add &amp;lt;file&amp;gt;..."&lt;/span&gt; to include &lt;span class="k"&gt;in &lt;/span&gt;what will be committed&lt;span class="o"&gt;)&lt;/span&gt;

    world.js

nothing added to commit but untracked files present &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add"&lt;/span&gt; to track&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Congratulations, your first commit! The &lt;strong&gt;-m&lt;/strong&gt; option is short for &lt;strong&gt;--message=&lt;/strong&gt; to add a message while committing.&lt;/p&gt;

&lt;p&gt;As you can see the file &lt;strong&gt;hello.js&lt;/strong&gt; is not seen anymore when running &lt;strong&gt;git status&lt;/strong&gt;, this is because has already been committed. We can check the Git history using &lt;strong&gt;git log&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git log
commit 15008ea3f281f95a4b3649e7383eb0334565b415 &lt;span class="o"&gt;(&lt;/span&gt;HEAD -&amp;gt; master&lt;span class="o"&gt;)&lt;/span&gt;
Author: sespinoza &amp;lt;espinoza.jimenez@gmail.com&amp;gt;
Date:   Tue Aug 4 21:22:07 2020 &lt;span class="nt"&gt;-0400&lt;/span&gt;

    Initial commit, add hello.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And there it is, our &lt;em&gt;commit&lt;/em&gt;. Here you see plenty of information related to that &lt;em&gt;commit&lt;/em&gt;: Hash, Author, Date of commit and the Git message. The hash is an identifier of a commit and with it, you can come back to that state of your code whenever you need to, using the &lt;strong&gt;HEAD&lt;/strong&gt; pointer.&lt;/p&gt;

&lt;h4&gt;
  
  
  HEAD pointer
&lt;/h4&gt;

&lt;p&gt;To explain what &lt;em&gt;(HEAD -&amp;gt; master)&lt;/em&gt; means you have to understand how git saves history. It uses a tree structure, where every commit is a node. Let us add another commit and check how our history would look like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"console.log('la la la')"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; hello.js
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;hello.js 
console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'la la la'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;git status
On branch master
Changes not staged &lt;span class="k"&gt;for &lt;/span&gt;commit:
 &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add &amp;lt;file&amp;gt;..."&lt;/span&gt; to update what will be committed&lt;span class="o"&gt;)&lt;/span&gt;
 &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git checkout -- &amp;lt;file&amp;gt;..."&lt;/span&gt; to discard changes &lt;span class="k"&gt;in &lt;/span&gt;working directory&lt;span class="o"&gt;)&lt;/span&gt;

    modified:   hello.js

Untracked files:
 &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add &amp;lt;file&amp;gt;..."&lt;/span&gt; to include &lt;span class="k"&gt;in &lt;/span&gt;what will be committed&lt;span class="o"&gt;)&lt;/span&gt;

    world.js

no changes added to commit &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add"&lt;/span&gt; and/or &lt;span class="s2"&gt;"git commit -a"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;git add hello.js 
&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"[Update] index.js add another console log"&lt;/span&gt;
 &lt;span class="o"&gt;[&lt;/span&gt;master f99f2fe] &lt;span class="o"&gt;[&lt;/span&gt;Update] index.js add another console log
 1 file changed, 1 insertion&lt;span class="o"&gt;(&lt;/span&gt;+&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;git log
 commit f99f2fe2a3a4c783c6658a591cf69903666bca17 &lt;span class="o"&gt;(&lt;/span&gt;HEAD -&amp;gt; master&lt;span class="o"&gt;)&lt;/span&gt;
 Author: sespinoza &amp;lt;espinoza.jimenez@gmail.com&amp;gt;
 Date:   Tue Aug 4 22:02:37 2020 &lt;span class="nt"&gt;-0400&lt;/span&gt;

    &lt;span class="o"&gt;[&lt;/span&gt;Update] index.js add another console log

 commit 15008ea3f281f95a4b3649e7383eb0334565b415
 Author: sespinoza &amp;lt;espinoza.jimenez@gmail.com&amp;gt;
 Date:   Tue Aug 4 21:22:07 2020 &lt;span class="nt"&gt;-0400&lt;/span&gt;

    Initial commit, add hello.js

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



&lt;p&gt;Here we have two commits already, and our tree looks like the following diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nl_kMvZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/git_history_two_commits.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nl_kMvZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/git_history_two_commits.png" alt="tree node one commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;HEAD&lt;/strong&gt; is an alias for the tip of the current branch&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And you can see where is pointing using &lt;strong&gt;git branch&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git branch
&lt;span class="k"&gt;*&lt;/span&gt; master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;em&gt;HEAD&lt;/em&gt; is pointing to the master branch. Also, you can see what &lt;em&gt;HEAD&lt;/em&gt; actually looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; .git/HEAD 
ref: refs/heads/master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To show you a little how it works, we can go back in time in the Git history and see our first commit by passing the &lt;em&gt;hash&lt;/em&gt; of the commit to the &lt;strong&gt;checkout&lt;/strong&gt; option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;master&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout 15008ea3f281f95a4b3649e7383eb0334565b415
 Note: checking out &lt;span class="s1"&gt;'15008ea3f281f95a4b3649e7383eb0334565b415'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;

 You are &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s1"&gt;'detached HEAD'&lt;/span&gt; state. You can look around, make experimental
 changes and commit them, and you can discard any commits you make &lt;span class="k"&gt;in &lt;/span&gt;this
 state without impacting any branches by performing another checkout.

 If you want to create a new branch to retain commits you to create, you may
 &lt;span class="k"&gt;do &lt;/span&gt;so &lt;span class="o"&gt;(&lt;/span&gt;now or later&lt;span class="o"&gt;)&lt;/span&gt; by using &lt;span class="nt"&gt;-b&lt;/span&gt; with the checkout &lt;span class="nb"&gt;command &lt;/span&gt;again. Example:

  git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; &amp;lt;new-branch-name&amp;gt;

 HEAD is now at 15008ea Initial commit, add hello.js

&lt;span class="o"&gt;((&lt;/span&gt;HEAD detached at 15008ea&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;hello.js 
 console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;checkout&lt;/strong&gt; command helps us to navigate through commits. We now see that &lt;em&gt;HEAD&lt;/em&gt; is pointing to the first commit, so now our file only has the first change that we did, that is &lt;em&gt;console.log('hello')&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wymC_oKq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/git_history_two_commits_point_to_first_commit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wymC_oKq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sespinoza.me/files/images/git_history_two_commits_point_to_first_commit.png" alt="tree node one commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can come back to the latest state by referring to the &lt;em&gt;branch&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout master
 Previous HEAD position was 15008ea Initial commit, add hello.js
 Switched to branch &lt;span class="s1"&gt;'master'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To end this article we'll see how to undo the last commit keeping the changes in our working directory and also the option of removing the commit and the changes from our working directory.&lt;/p&gt;

&lt;h4&gt;
  
  
  Undo the last commit --soft
&lt;/h4&gt;

&lt;p&gt;If you want to remove the last commit but keep those changes in your working directory use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git reset &lt;span class="nt"&gt;--soft&lt;/span&gt; HEAD^1

&lt;span class="nv"&gt;$ &lt;/span&gt;git status
 On branch master
 Changes to be committed:
  &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git reset HEAD &amp;lt;file&amp;gt;..."&lt;/span&gt; to unstage&lt;span class="o"&gt;)&lt;/span&gt;

    modified:   hello.js

 Untracked files:
  &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add &amp;lt;file&amp;gt;..."&lt;/span&gt; to include &lt;span class="k"&gt;in &lt;/span&gt;what will be committed&lt;span class="o"&gt;)&lt;/span&gt;

    world.js

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



&lt;p&gt;As you can see your files of the last commit are now in your staging area, you can edit your file now and commit again later. Here &lt;em&gt;HEAD^1&lt;/em&gt; makes a reference to the immediate "parent" of the current commit. The current commit is the last commit, so here we are telling git "go and reset everything until the previous commit".&lt;/p&gt;

&lt;h4&gt;
  
  
  Undo the last commit --hard
&lt;/h4&gt;

&lt;p&gt;If you want to get rid of the last commit and all the changes introduced in the last commit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git reset &lt;span class="nt"&gt;--hard&lt;/span&gt; HEAD^1
 HEAD is now at 15008ea Initial commit, add hello.js

&lt;span class="nv"&gt;$ &lt;/span&gt;git status
 On branch master
 Untracked files:
  &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add &amp;lt;file&amp;gt;..."&lt;/span&gt; to include &lt;span class="k"&gt;in &lt;/span&gt;what will be committed&lt;span class="o"&gt;)&lt;/span&gt;

    world.js

 nothing added to commit but untracked files present &lt;span class="o"&gt;(&lt;/span&gt;use &lt;span class="s2"&gt;"git add"&lt;/span&gt; to track&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;hello.js 
 console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using &lt;strong&gt;--hard&lt;/strong&gt; removes the las commit and it's changes and place &lt;strong&gt;HEAD&lt;/strong&gt; pointing to the first commit.&lt;/p&gt;

&lt;p&gt;That's it for this Article, in the next one I'll talk about how to make better Git commit messages, the workflow for a single developer and more. I hope this helped you understanding and using git better.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Cat_%28Unix%29"&gt;cat&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=4XpnKHJAok8&amp;amp;t=2367s"&gt;Linus Torvalds on git&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I’m a Software Engineer, writer, tech enthusiast, pianist, origami lover, amateur photographer. In my spare time, I go trekking, play the piano and learn history.&lt;/p&gt;

&lt;p&gt;My tech: JavaScript, Node.js, React, Ruby, Crystal, Bash, Docker.&lt;/p&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/sespinoza_dev"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/espinoza-jimenez/"&gt;LinkedIn&lt;/a&gt; or visit my page to contact &lt;a href="https://sespinoza.me/#/contact/"&gt;me&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Originally posted in: &lt;a href="https://sespinoza.me/#/articles/5f2a2e23634b82eacb0f43c7"&gt;sespinoza.me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
    </item>
  </channel>
</rss>
