<?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: Nya</title>
    <description>The latest articles on DEV Community by Nya (@nyagarcia).</description>
    <link>https://dev.to/nyagarcia</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%2F140116%2Fc66be470-a64c-46d8-a45a-1132e19a58cf.jpeg</url>
      <title>DEV Community: Nya</title>
      <link>https://dev.to/nyagarcia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nyagarcia"/>
    <language>en</language>
    <item>
      <title>RxJS Lessons: Understanding Multicasting Operators</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Tue, 02 Feb 2021 13:17:07 +0000</pubDate>
      <link>https://dev.to/nyagarcia/rxjs-lessons-understanding-multicasting-operators-372g</link>
      <guid>https://dev.to/nyagarcia/rxjs-lessons-understanding-multicasting-operators-372g</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When it comes to RxJS, multicasting is probably one of its most daunting features. Multicasting requires a prior RxJS knowledge base and is, in itself, a feature which some programmers struggle to grasp. To this we add that there exist quite a few multicasting operators, and that knowing which one to pick can prove to be quite difficult if we don't understand how they each work, and which particular problem they solve.&lt;/p&gt;

&lt;p&gt;This is an introductory guide to the RxJS multicasting operators. By the end of this guide, you'll (hopefully) understand these operators a little better, and know when and why you need to use each one. We will start by taking a look at the hot/cold Observable concept, and we'll then make our way through all of the multicasting operators, discussing their particularities and looking at examples, until we understand them all. &lt;/p&gt;

&lt;p&gt;I've created a StackBlitz project for each and every one of the examples that we'll see in this guide, so that you can play around with the code snippets, since, in my humble opinion, this considerably helps to understand how the operators work. You can find the link to the  corresponding StackBlitz project in the caption below each example.&lt;br&gt;
Before we get started, here is a list of all the operators that we'll cover today:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/multicast"&gt;multicast&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/publish"&gt;publish&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/publishReplay"&gt;publishReplay&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/publishLast"&gt;publishLast&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/publishBehavior"&gt;publishBehavior&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/refCount"&gt;refCount&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/share"&gt;share&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/shareReplay"&gt;shareReplay&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Hot &amp;amp; cold Observables
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Note: If you're already familiar with these concepts, feel free to skip this section and move on to the next.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most Observables are cold by default. Each time we subscribe to a cold Observable, its producer is recreated. So what does this mean exactly? First, we must understand what a producer is: in a nutshell, it's the source of our Observable's values. It can be a DOM event, a callback, an HTTP request, an iterator and so on. In short, anything that can produce values and pass them to an observer. &lt;/p&gt;

&lt;p&gt;Now that we know what a producer is, it'll be easier to understand the meaning of the previous statement, which basically comes to say that our Observable's producer is being created over and over again with each subscription. Let's take a look at an example: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/cold-observable-example"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, because our Observable is cold, and its producer is being recreated with each subscription, the side effect is being executed twice, one time for each subscription. If the Observable were hot, the side effect would be performed only once, regardless of how many times we subscribe.&lt;/p&gt;

&lt;p&gt;Some may think, after seeing the previous code, that the consequences derived from this behavior aren't particularly important, which is why I always like to explain this concept, and its considerable importance, by using an HTTP request as an example.&lt;/p&gt;

&lt;p&gt;Let's imagine that we have an Ajax Observable, that gets some data for us. Because the Ajax Observable is cold, each time we subscribe to it, a new HTTP request is made. Yes, you read correctly, a new request is made for each subscription. 20 subscriptions = 20 HTTP requests. Let's take a look at some code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://stackblitz.com/edit/cold-observable-ajax-example?file=index.html"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upon seeing this, I believe that the importance of properly handling hot/cold Observables becomes exceedingly clear. Having the producer recreated with each subscription is something we most certainly don't want happening in our code. So, how do we fix this serious issue? By making our cold Observables hot. How do we do that? With multicasting operators! So, without further ado, let's get started on those operators.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: There is a wonderful article by Ben Lesh that treats the topic of hot/cold Observables in-depth. You can find it &lt;a href="https://medium.com/@benlesh/hot-vs-cold-observables-f8094ed53339"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  multicast
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;multicast&lt;/code&gt; shares the source Observable by using a Subject. Let's take a look at an example using multicast:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-multicast-no-connect?file=index.ts"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you tried the StackBlitz? Noticed anything strange? If we run the previous code, we won't receive any values at all! Why isn't our source Observable emitting anything? &lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;multicast&lt;/code&gt; returns a special kind of Observable: a &lt;code&gt;ConnectableObservable&lt;/code&gt;. This special type of Observable has a &lt;code&gt;connect()&lt;/code&gt; method, which, when called, is responsible for subscribing to the source Observable with the Subject that we provided. &lt;/p&gt;

&lt;p&gt;This means that if we don't call &lt;code&gt;connect()&lt;/code&gt;, the source will never be subscribed to, and will never begin emitting values. So, let's change our previous code, adding a call to &lt;code&gt;connect()&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-multicast?file=index.ts"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Et voilà! Our code is now working as it should. Since &lt;code&gt;multicast&lt;/code&gt; is sharing the source Observable, the side effect will only be executed once, even if we were to subscribe 1000 times.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unsubscribing
&lt;/h3&gt;

&lt;p&gt;As with all Observables, it's important to unsubscribe from our multicast Observables to avoid memory leaks. We need to bear in mind that, when dealing with multicasting operators that return a ConnectableObservable, we need to unsubscribe from the multicast subscription. &lt;/p&gt;

&lt;p&gt;Let's take our previous code snippet, remove the &lt;code&gt;take(2)&lt;/code&gt; from the source that was taking care of ending our Observable for us, and unsubscribe:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-multicast-unsubscribe?file=index.ts"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Memory leak successfully avoided!&lt;/p&gt;

&lt;h3&gt;
  
  
  What about late subscribers?
&lt;/h3&gt;

&lt;p&gt;In a perfect sandbox environment, all the subscriptions to the multicast Observable happen at the same time. However, what are the odds of finding this kind of behavior in real life? I can assure you that they are not very good. Let's take a look at a more realistic example, where we have different subscriptions occurring at different times:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-multicast"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, it seems that our late observer is missing out on the values that were emitted before it subscribed to the multicast Observable. This can lead to unexpected behavior and major headaches trying to discover the cause.&lt;/p&gt;

&lt;p&gt;So, how can we solve this problem? It's actually quite simple, all we have to do is use a &lt;a href="https://rxjs.dev/api/index/class/ReplaySubject"&gt;ReplaySuject&lt;/a&gt; instead of a regular Subject. Since ReplaySubjects replay old values to new subscribers, our issue is effectively  solved:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-multicast-replaysubject"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Et, voilà, our late observers now have access to the previously emitted values.&lt;/p&gt;


&lt;h2&gt;
  
  
  publish()
&lt;/h2&gt;

&lt;p&gt;We can all agree that &lt;code&gt;multicast&lt;/code&gt; is an amazing operator, but having to type &lt;code&gt;multicast(() =&amp;gt; new Subject())&lt;/code&gt; each time we want to multicast our streams can get a little verbose…&lt;br&gt;
&lt;code&gt;publish&lt;/code&gt; operator to the rescue! &lt;code&gt;publish&lt;/code&gt; basically uses &lt;code&gt;multicast&lt;/code&gt; plus a Subject under the hood, so that we don't have to go the trouble of typing it. Pretty cool, right? Let's take a look at an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-publish"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember, we still have to call connect() if we want our source Observable to be subscribed to!&lt;/p&gt;

&lt;h3&gt;
  
  
  The publish variants
&lt;/h3&gt;

&lt;p&gt;Remember the issue we had with late subscribers and &lt;code&gt;multicast&lt;/code&gt;? How do we deal with them in this case? Since publish is equivalent to using &lt;code&gt;multicast(() =&amp;gt; new Subject())&lt;/code&gt;, we can't just change the Subject for a ReplaySubject manually. Luckily for us, publish has several variants, one for every kind of Subject that there is. Let's take a look at them:&lt;/p&gt;

&lt;h3&gt;
  
  
  publishReplay()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;publishReplay&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new ReplaySubject())&lt;/code&gt;. Since we've already seen an example with &lt;code&gt;multicast&lt;/code&gt; + ReplaySubject, we know that it allows late subscribers to receive the emissions prior to their subscription:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-publishreplay"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  publishLast()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;publishLast&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new AsyncSubject())&lt;/code&gt;. It will wait until the source Observable has completed to emit the last value. Here's an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-publishlast"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  publishBehavior
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;publishBehavior&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new BehaviorSubject())&lt;/code&gt;. Since it uses a &lt;a href="https://rxjs.dev/api/index/class/BehaviorSubject"&gt;BehaviorSubject&lt;/a&gt;, &lt;code&gt;publishBehavior&lt;/code&gt; allows us to specify an initial value:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-publishbehavior?file=index.ts"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  refCount()
&lt;/h2&gt;

&lt;p&gt;We now know about several amazing operators to share our streams. However, having to call &lt;code&gt;connect()&lt;/code&gt; gets old quick. It's verbose and, should we forget to call it, we'd probably end up wasting time figuring out why our Observables aren't emitting. So, isn't there a better alternative?&lt;/p&gt;

&lt;p&gt;Of course there is! Allow me to introduce the &lt;code&gt;refCount&lt;/code&gt; operator. &lt;code&gt;refCount&lt;/code&gt; is in charge of counting the number of subscriptions to the source internally, which takes care of two crucial things for us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the number of subscriptions is larger than 0, a.k.a. there's at least one subscriber, &lt;code&gt;refCount&lt;/code&gt; subscribes (once only) to the source, calling &lt;code&gt;connect()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the number of subscriptions is smaller than 1, a.k.a. there aren't any subscribers, &lt;code&gt;refCount&lt;/code&gt; unsubscribes from the source.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's refactor our previous code to include &lt;code&gt;refCount&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-refcount"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, &lt;code&gt;refCount&lt;/code&gt; takes care of calling &lt;code&gt;connect()&lt;/code&gt; and of unsubscribing from the source Observable for us.&lt;/p&gt;


&lt;h2&gt;
  
  
  share()
&lt;/h2&gt;

&lt;p&gt;Last, but not least, we have the &lt;code&gt;share&lt;/code&gt; operator, which is equivalent to using &lt;code&gt;multicast(() =&amp;gt; new Subject())&lt;/code&gt; + &lt;code&gt;refCount&lt;/code&gt;. It's the easiest and most commonly used multicasting operator, since it takes care of everything under the hood. Here's our previous example, refactored to use &lt;code&gt;share&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-share?file=index.ts"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just for fun, let's take a look at a slightly more realistic example, featuring a shared Ajax Observable instead of a boring old interval:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/multicasting-share-ajax?file=index.ts"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  shareReplay()
&lt;/h2&gt;

&lt;p&gt;Once again, we must remember our late subscribers. In this case, share only has one variant, &lt;code&gt;shareReplay&lt;/code&gt;. As you can imagine, shareReplay is equivalent to &lt;code&gt;multicast(() =&amp;gt; new ReplaySubject())&lt;/code&gt; + &lt;code&gt;refCount&lt;/code&gt;. Here's an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://stackblitz.com/edit/sharereplay-multicasting"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;💡 &lt;code&gt;publish&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new Subject())&lt;/code&gt;.&lt;br&gt;
💡 &lt;code&gt;publishBehavior&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new BehaviorSubject())&lt;/code&gt;.&lt;br&gt;
💡 &lt;code&gt;publishLast&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new AsyncSubject())&lt;/code&gt;.&lt;br&gt;
💡 &lt;code&gt;publishReplay&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new ReplaySubject())&lt;/code&gt;.&lt;br&gt;
💡 With &lt;code&gt;refCount&lt;/code&gt;, we no longer have to manually call &lt;code&gt;connect()&lt;/code&gt; nor do we have to take care of unsubscribing.&lt;br&gt;
💡 &lt;code&gt;share&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new Subject())&lt;/code&gt;, &lt;code&gt;refCount()&lt;/code&gt;.&lt;br&gt;
💡 &lt;code&gt;shareReplay&lt;/code&gt; is equivalent to &lt;code&gt;multicast(() =&amp;gt; new ReplaySubject())&lt;/code&gt;, &lt;code&gt;refCount()&lt;/code&gt;.&lt;/p&gt;




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

&lt;p&gt;That's all folks! I hope this post helped you understand multicasting operators a little better, and realize that multicasting is not as hard as it initially seems.&lt;/p&gt;

&lt;p&gt;As I mentioned earlier, I've created a StackBlitz project for each and every example in this guide, so feel free to play around with the code, I promise that it really helps to better understand how the operators work. &lt;/p&gt;

&lt;p&gt;If you enjoyed this guide, feel free to leave a nice comment! If you have any questions, you can drop them in the comments and I'll try my best to answer.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>rxjs</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>RxJS Best Practices</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Sat, 28 Mar 2020 17:14:34 +0000</pubDate>
      <link>https://dev.to/nyagarcia/rxjs-best-practices-bhb</link>
      <guid>https://dev.to/nyagarcia/rxjs-best-practices-bhb</guid>
      <description>&lt;h3&gt;
  
  
  RxJS Best Practices
&lt;/h3&gt;

&lt;p&gt;RxJS is the most popular framework for reactive functional programming in&lt;br&gt;
JavaScript. This means that a lot of people are using RxJS daily in their&lt;br&gt;
projects. Most developers are aware of the common clean code practices, but…&lt;br&gt;
What about &lt;strong&gt;RxJS best practices&lt;/strong&gt;? Are you aware of the dos and don’ts when it&lt;br&gt;
comes to functional reactive programming? Are you applying them in your code? &lt;/p&gt;

&lt;p&gt;This tutorial will focus on several best practices that I use daily when writing&lt;br&gt;
code, with practical examples. We will be covering the following points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Avoid logic inside the subscribe function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using Subjects to force completion&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid duplicated logic&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid nesting — Use chaining instead&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sharing to avoid stream duplication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don’t expose Subjects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use marble diagrams for testing&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Without further ado, let’s get started:&lt;/p&gt;
&lt;h3&gt;
  
  
  Avoid logic inside the subscribe function
&lt;/h3&gt;

&lt;p&gt;This statement may seem pretty obvious to some of you, but it’s a common pitfall for RxJS beginners. Until you learn how to &lt;strong&gt;think reactively&lt;/strong&gt;, you may be tempted to do something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Our &lt;em&gt;pokemon$&lt;/em&gt; Observable emits Pokemon objects, and, in a very &lt;strong&gt;non-reactive way&lt;/strong&gt;, we are subscribing to it in order to access these objects and perform some actions, like returning early if the Pokemon type is Water, making a call to a &lt;em&gt;getStats()&lt;/em&gt; function, logging the stats that this function returns and finally saving the data to the &lt;em&gt;Pokedex&lt;/em&gt;. &lt;strong&gt;All our logic is inside the subscribe function.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;However, doesn’t this code look exactly like something we would see in the traditional &lt;strong&gt;imperative&lt;/strong&gt; programming paradigm? Since RxJS is a &lt;strong&gt;functional reactive&lt;/strong&gt; programming library, we have to say goodbye to our traditional way of thinking, and start &lt;strong&gt;thinking reactively&lt;/strong&gt; (streams! Pure functions!). &lt;/p&gt;

&lt;p&gt;So, how do we make our code &lt;strong&gt;reactive&lt;/strong&gt;? By using the &lt;strong&gt;pipeable operators&lt;/strong&gt; that RxJS provides us with:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Et voilá, our code has gone &lt;strong&gt;from imperative to reactive&lt;/strong&gt; with a few simple changes. It even looks cleaner, doesn’t it? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I am fully aware of the fact that a part of the logic (the&lt;br&gt;
saveToPokedex() function) still remains in the subscribe. I find that by keeping the last part of the logic inside the subscribe makes it easier for me to read the code. You are all free to choose whether or not to keep the subscribe completely empty :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The operators we have used are pretty straightforward: &lt;a href="https://www.learnrxjs.io/learn-rxjs/operators/filtering/filter" rel="noopener noreferrer"&gt;filter&lt;/a&gt; and &lt;a href="https://rxjs-dev.firebaseapp.com/api/operators/map" rel="noopener noreferrer"&gt;map&lt;/a&gt; work exactly the same as the Array operators that they share name with, and &lt;a href="https://rxjs-dev.firebaseapp.com/api/operators/tap" rel="noopener noreferrer"&gt;tap&lt;/a&gt; is used to perrform &lt;strong&gt;side effects.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Side_effect_(computer_science)" rel="noopener noreferrer"&gt;&lt;strong&gt;Wikipedia:&lt;/strong&gt;&lt;/a&gt; An operation, function or expression is said to have a side effect if it modifies some state variable value outside its local environment.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Using Subjects to force completion
&lt;/h3&gt;

&lt;p&gt;Memory leaks are a real danger when it comes to using Observables. Why? Because, once we subscribe to an Observable, it will &lt;strong&gt;keep emitting values indefinitely&lt;/strong&gt; until one of the following two conditions are met:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; We manually &lt;strong&gt;unsubscribe&lt;/strong&gt; from the Observable.&lt;/li&gt;
&lt;li&gt; It &lt;strong&gt;completes&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Seems simple enough, right? Let’s take a look at how to unsubscribe from an Observable:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see in the above example, we have to &lt;strong&gt;store the subscription&lt;/strong&gt; of our &lt;em&gt;pokemon$&lt;/em&gt; Observable in a variable, and then manually &lt;strong&gt;call unsubscribe&lt;/strong&gt; on that stored subscription. Doesn’t seem too difficult so far…&lt;/p&gt;

&lt;p&gt;But what happens if we have more Observables that we need to subscribe to?&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, as we add more Observables to our code, we need to keep track of &lt;strong&gt;more and more subscriptions&lt;/strong&gt;, and our code starts looking a bit crowded. Isn’t there a better way for us to tell our Observables to &lt;strong&gt;stop emitting values?&lt;/strong&gt; Luckily for us, there is, and it’s very, very simple:&lt;/p&gt;

&lt;p&gt;We can use a &lt;strong&gt;Subject&lt;/strong&gt;, together with the &lt;strong&gt;takeUntil()&lt;/strong&gt; operator, to force our Observables to &lt;strong&gt;complete&lt;/strong&gt;. How? Here’s an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let’s understand what’s going on above. We’ve created a &lt;em&gt;stop$&lt;/em&gt; Subject, and we’ve piped our three Observables with the takeUntil operator. This operator is used for an Observable to &lt;strong&gt;keep emitting&lt;/strong&gt; values, &lt;strong&gt;until a notifier Observable emits&lt;/strong&gt;. Which means that our three Observables will &lt;strong&gt;stop emitting values when the stop$ Subject emits.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So how do we make our &lt;em&gt;stop$&lt;/em&gt; Observable emit? By &lt;strong&gt;calling the next()&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;function&lt;/strong&gt; on it, which is exactly what we are doing inside our&lt;br&gt;
&lt;em&gt;stopObservables()&lt;/em&gt; function. Therefore, whenever we call our&lt;br&gt;
&lt;em&gt;stopObservables()&lt;/em&gt; function, our &lt;em&gt;stop$&lt;/em&gt; Observable will emit and all our Observables &lt;strong&gt;will automatically complete&lt;/strong&gt;. Sounds cool, doesn’t it?&lt;/p&gt;

&lt;p&gt;No more having to store any subscriptions and call unsubscribe, no more messing around with arrays? All hail the takeUntil operator!&lt;/p&gt;


&lt;h3&gt;
  
  
  Avoid duplicated logic
&lt;/h3&gt;

&lt;p&gt;We all know that  duplicated code is a bad sign, and something that should be&lt;br&gt;
avoided. (If you didn’t know that, I would recommend that you go and read&lt;br&gt;
&lt;a href="https://medium.com/better-programming/the-art-of-refactoring-5-tips-to-write-better-code-3bc1f6f7689" rel="noopener noreferrer"&gt;this&lt;/a&gt;,&lt;br&gt;
and then come back.) You may be wondering which scenarios could lead to having duplicate RxJS logic. Let’s take a look at the following example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, we have a number$ Observable, which emits every second. We subscribe twice to this Observable: Once to keep score with &lt;em&gt;scan()&lt;/em&gt; and once to call the &lt;em&gt;getPokemonByID()&lt;/em&gt; function every ten seconds. Seems quite straightforward, but…&lt;/p&gt;

&lt;p&gt;Notice how we &lt;strong&gt;have duplicated the takeUntil() logic in both Observables?&lt;/strong&gt; This should be avoided, as long as our code allows it. How? By attaching this logic to the &lt;strong&gt;source observable&lt;/strong&gt;, like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Less code &amp;amp;&amp;amp; no duplication === Cleaner code. Awesome!&lt;/p&gt;




&lt;h3&gt;
  
  
  Avoid nesting — Use chaining instead
&lt;/h3&gt;

&lt;p&gt;Nested subscriptions should be avoided at all cost. They make our code complex, dirty, difficult to test and can cause some pretty nasty bugs. What is a nested subscription, you may ask? It’s when we subscribe to an Observable in the subscribe block of another Observable. Let’s take a look at the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Doesn’t look very neat, does it? The above code is confusing, complex, and, should we need to call more functions that return Observables, we’re going to have to keep adding more and more subscriptions. This is starting to sound suspiciously like a ‘&lt;strong&gt;subscription hell&lt;/strong&gt;’. So, what can we do to avoid nested subscriptions?&lt;/p&gt;

&lt;p&gt;The answer is to use &lt;strong&gt;higher order mapping operators&lt;/strong&gt;. Some of these operators are: &lt;a href="https://www.learnrxjs.io/learn-rxjs/operators/transformation/switchmap" rel="noopener noreferrer"&gt;switchMap&lt;/a&gt;, &lt;a href="https://www.learnrxjs.io/learn-rxjs/operators/transformation/mergemap" rel="noopener noreferrer"&gt;mergeMap&lt;/a&gt; etc.&lt;/p&gt;

&lt;p&gt;To fix our example, we are going to make use of the &lt;em&gt;switchMap&lt;/em&gt; operator. Why? Because &lt;em&gt;switchMap&lt;/em&gt; unsubscribes from the previous Observable, and &lt;strong&gt;switches&lt;/strong&gt; (easy to remember, right?) to the inner Observable, which, in our case, is the perfect solution. However, please note that &lt;strong&gt;depending on which behavior&lt;/strong&gt; you need, you may need to use a different higher order mapping operator.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Just look at how lovely our code looks now.&lt;/p&gt;




&lt;h3&gt;
  
  
  Sharing to avoid stream duplication
&lt;/h3&gt;

&lt;p&gt;Ever had your angular code make duplicate HTTP requests and wondered why? Read on and you will discover the reason behind this widespread bug:&lt;/p&gt;

&lt;p&gt;Most Observables are &lt;strong&gt;cold&lt;/strong&gt;. This means that their &lt;strong&gt;producer is created and activated when we subscribe to them&lt;/strong&gt;. This might sound a bit confusing, but is simple enough to understand. With cold Observables, every time we subscribe to them, a &lt;strong&gt;new producer is created&lt;/strong&gt;. So if we subscribe to a cold Observable five times, five producers will be created. &lt;/p&gt;

&lt;p&gt;So, what is a producer exactly? It’s basically the &lt;strong&gt;source of our Observable’s values&lt;/strong&gt; (for example, a DOM event, an HTTP request, an array etc.) What does this imply for us reactive programmers? Well, if we, for example, subscribe twice to an observable that makes an HTTP request, &lt;strong&gt;two HTTP requests will be made.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sounds like trouble.&lt;/p&gt;

&lt;p&gt;The following example (borrowing Angular’s HttpClient) would trigger two&lt;br&gt;
different HTTP requests, because &lt;em&gt;pokemon$&lt;/em&gt; is a cold Observable, and we are subscribing to it twice:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can imagine, this behavior can lead to nasty bugs, so, how can we avoid it? Isn’t there  a way to subscribe multiple times to an Observable &lt;strong&gt;without triggering duplicated logic&lt;/strong&gt; as its source its created over and over again? Of course there is, allow me to introduce: &lt;strong&gt;The share() operator.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This operator is used to allow &lt;strong&gt;multiple subscriptions&lt;/strong&gt; to an Observable, &lt;strong&gt;without recreating its source&lt;/strong&gt;. In other words, it turns an Observable from cold, to &lt;strong&gt;hot.&lt;/strong&gt; Let’s see how it’s used:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Yes, that is really all we need to do, and our problem is ‘magically solved’. By adding the &lt;em&gt;share()&lt;/em&gt;  operator, our previously cold &lt;em&gt;pokemon$&lt;/em&gt; Observable now &lt;strong&gt;behaves as if it were hot,&lt;/strong&gt; and only one HTTP request will be made, even though we subscribe to it twice. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A word of caution:&lt;/strong&gt; Since hot Observables don’t replicate the source, if we subscribe late to a stream, we won’t be able to access previously emitted values.  The &lt;a href="https://www.learnrxjs.io/learn-rxjs/operators/multicasting/sharereplay" rel="noopener noreferrer"&gt;shareReplay()&lt;/a&gt; operator can be a solution for this. &lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Don’t expose Subjects
&lt;/h3&gt;

&lt;p&gt;It’s a common practice to use services to contain Observables that we reuse in our application. It’s also common to have Subjects inside such services. A common mistake many developers make is &lt;strong&gt;exposing these Subjects&lt;/strong&gt; directly to the ‘outside world’, by doing something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Don’t do this.&lt;/strong&gt; By exposing a Subject, we’re allowing anyone to push data into it, not to mention that we are completely &lt;strong&gt;breaking the encapsulation&lt;/strong&gt; of our &lt;em&gt;DataService&lt;/em&gt; class. Instead of &lt;strong&gt;exposing our Subject&lt;/strong&gt;, we should &lt;strong&gt;expose our Subject’s data.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Isn’t it the same thing, you may be wondering? The answer is &lt;strong&gt;no.&lt;/strong&gt; If we expose a Subject, we are making all of its methods available, including the &lt;em&gt;next()&lt;/em&gt; function, which is used to make the Subject &lt;strong&gt;emit&lt;/strong&gt; a new value. On the other hand, if we just expose its  data, we won’t make our Subject’s methods available, &lt;strong&gt;just the values that it emits.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, how can we expose our Subject’s data but not it’s methods? By using the &lt;em&gt;asObservable&lt;/em&gt;() operator, which transforms a Subject into an Observable. Since Observables &lt;strong&gt;do not have the next() function&lt;/strong&gt;, our Subject’s data will be &lt;strong&gt;safe from tampering&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We have four different things going on in the above code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Both our &lt;em&gt;pokemonLevel&lt;/em&gt; and &lt;em&gt;stop$&lt;/em&gt; Subjects are now &lt;strong&gt;private,&lt;/strong&gt; and therefore &lt;strong&gt;not accessible&lt;/strong&gt; from outside our &lt;em&gt;DataService&lt;/em&gt; class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We now have a &lt;em&gt;pokemonLevel$&lt;/em&gt; Observable, that has been created by calling the &lt;em&gt;asObservable()&lt;/em&gt; operator on our &lt;em&gt;pokemonLevel&lt;/em&gt; Subject. This way, we can access the &lt;em&gt;pokemonLevel&lt;/em&gt; data from outside the class, while keeping the Subject &lt;strong&gt;safe from manipulation&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You may have noticed that, for the &lt;em&gt;stop$&lt;/em&gt; Subject, we &lt;strong&gt;did not create an Observable&lt;/strong&gt;. This is because we don’t need to access stop$’s data from outside the class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We now have two public methods, named &lt;em&gt;increaseLevel()&lt;/em&gt; and &lt;em&gt;stop()&lt;/em&gt;. The latter is simple enough to understand. It allows us to make the &lt;strong&gt;private&lt;/strong&gt; stop$ Subject &lt;strong&gt;emit from outside the class&lt;/strong&gt;, thus completing all Observables that have piped &lt;em&gt;takeUntil(stop$)&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;increaseLevel()&lt;/em&gt; acts as a filter and only allows us to pass &lt;strong&gt;certain values&lt;/strong&gt; to the &lt;em&gt;pokemonLevel()&lt;/em&gt; Subject.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This way, no arbitrary data will be able to find its way to into our Subjects,which are nicely protected inside the class.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Please bear in mind that Observables have complete() and error() methods, which can still be used to mess with the Subject.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Remember everyone, &lt;strong&gt;encapsulation&lt;/strong&gt; is the key.&lt;/p&gt;




&lt;h3&gt;
  
  
  Use marble diagrams for testing
&lt;/h3&gt;

&lt;p&gt;As we all (should) know, &lt;strong&gt;writing tests is as important as writing code itself.&lt;/strong&gt; However, if the thought of writing RxJS tests sounds a bit daunting to you… Fear not, from RxJS 6+, the RxJS marble testing utils will make life very, very easy for us. Are you familiar with &lt;strong&gt;marble diagrams&lt;/strong&gt;? If not, here’s an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A2uN5I_Qj68MF3Ofd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A2uN5I_Qj68MF3Ofd.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even if you’re a newbie to RxJS, you should more or less understand these&lt;br&gt;
diagrams. They’re all over the place, are quite intuitive, and make it quite easy to understand how some of the more complicated RxJS operators work. The RxJS testing utils allows us to use these marble diagrams to write simple, intuitive and visual tests. All you have to do is import &lt;em&gt;TestScheduler&lt;/em&gt; from the rxjs/testing module, and start writing tests!&lt;/p&gt;

&lt;p&gt;Let’s take a look at how it’s done, by testing our number$ Observable:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Since deep diving into marble testing isn’t the goal of this tutorial, I will only briefly cover the key concepts that appear in the above code, so we have a basic understanding of what’s going on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;TestScheduler:&lt;/strong&gt; Is used to virtualize time. It receives a callback, which can be called with &lt;strong&gt;helper&lt;/strong&gt; objects (in our case, the &lt;em&gt;cold()&lt;/em&gt; and &lt;em&gt;expectObservable()&lt;/em&gt; helpers.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Run():&lt;/strong&gt; Automatically calls &lt;em&gt;flush()&lt;/em&gt; when the callback returns. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;-&lt;/strong&gt; : Each - represents 10ms of time. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cold():&lt;/strong&gt; Creates a cold Observable whose subscription starts when the test begins. In our case, we are creating a cold Observable that will emit a value very 10ms, and complete. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;|&lt;/strong&gt;: Represents the completion of an Observable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Therefore, our &lt;em&gt;expectedMarbleDiagram&lt;/em&gt;, expects for a to be emitted at 20ms.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;expectedValues&lt;/em&gt; variable contains the expected values of every item that is emitted by our Observable. In our case, &lt;em&gt;a&lt;/em&gt; is the only value that will be emitted, and equals 10.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ExpectObservable():&lt;/strong&gt; Schedules an assertion that will be executed when the &lt;em&gt;testScheduler&lt;/em&gt; flushes. In our case, our assertion expects the number$ Observable to be like the &lt;em&gt;expectedMarbleDiagram&lt;/em&gt;, with the values contained in the &lt;em&gt;expectedValues&lt;/em&gt; variable.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find more information about helpers etc. in the &lt;a href="https://rxjs-dev.firebaseapp.com/guide/testing/marble-testing" rel="noopener noreferrer"&gt;official RxJS&lt;br&gt;
docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Advantages of using RxJS marble testing utils:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You avoid &lt;strong&gt;a lot&lt;/strong&gt; of boilerplate code. (Jasmine-marbles users will be able to appreciate this.)&lt;/li&gt;
&lt;li&gt;It’s very &lt;strong&gt;easy&lt;/strong&gt; and &lt;strong&gt;intuitive&lt;/strong&gt; to use. &lt;/li&gt;
&lt;li&gt;It’s &lt;strong&gt;fun&lt;/strong&gt;! Even if you aren’t a big fan of writing tests, I can guarantee you’ll enjoy marble testing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since I enjoy making all my code examples Pokemon-themed, I’ll throw in another spec, this time featuring a pokemon$ Observable test:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





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

&lt;p&gt;That’s all folks! Today we discussed a few of the RxJS best practices that I always take careful care to apply in my code. I hope that you found them useful, if you didn’t already know about them. &lt;/p&gt;

&lt;p&gt;Do you know any more RxJS best practices? If you do, let me know in the comments below. This way we can all contribute to writing better and cleaner reactive code! &lt;/p&gt;

&lt;p&gt;If you enjoyed this post, don’t forget to share with your friends/colleagues, and maybe give me a lil’ clap :) If you have any questions, don’t hesitate to ask, either in the comments, or by reaching out to me via &lt;a href="https://twitter.com/nyablk" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;. See you all in the next tutorial!&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>bestpractices</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Contingency tables in R</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Mon, 23 Mar 2020 19:07:14 +0000</pubDate>
      <link>https://dev.to/nyagarcia/contingency-tables-in-r-15k1</link>
      <guid>https://dev.to/nyagarcia/contingency-tables-in-r-15k1</guid>
      <description>

&lt;h1&gt;
  
  
  Contingency tables in R
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Contingency tables are very useful when we need to &lt;strong&gt;condense&lt;/strong&gt; a large amount of data into a smaller format, to make it easier to gain an &lt;strong&gt;overall view&lt;/strong&gt; of our original data.&lt;/p&gt;

&lt;p&gt;To create contingency tables in R we will be making use of the &lt;em&gt;table()&lt;/em&gt; function. Since &lt;em&gt;table()&lt;/em&gt; is so versatile, we can build contingency tables from something as simple as character strings, to something as complex as data frame objects. &lt;/p&gt;

&lt;p&gt;Something that we have to keep in mind is the fact that as we add more data, or as the complexity of our data increases, our contingency table will grow more complex too.&lt;/p&gt;

&lt;p&gt;As well as the creation of contingency tables, we will cover the calculation of &lt;strong&gt;marginal distributions&lt;/strong&gt;, creating a &lt;strong&gt;frequency or percentage&lt;/strong&gt; contingency table, and selecting fragments of our table. We will also learn three of the most popular techniques that are used to &lt;strong&gt;test for independence between variables&lt;/strong&gt;, and how to calculate the &lt;strong&gt;measures of association&lt;/strong&gt; between these same variables.&lt;/p&gt;

&lt;p&gt;Here is the index for this tutorial:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting started - Loading data&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Creating a table object&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Contingency tables&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Selecting parts of a contingency table&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Frequencies in contingency tables&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Calculating marginal distributions&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Testing for Independence&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Chi-Square test&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fisher's Exact test&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The G-Test&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Measures of Association&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Phi Coefficient&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cramer's V and Tschuprow's T&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Getting started - Loading data
&lt;/h2&gt;

&lt;p&gt;Before we begin with contingency tables, I would like to briefly discuss creating table objects in R. This way, we will be able to truly appreciate the advantages that contingency tables have (when our goal is overall legibility) over traditional tables. &lt;/p&gt;

&lt;p&gt;In order for us to be on the same page, I would propose using the following built-in R dataset: &lt;strong&gt;mtcars&lt;/strong&gt;, although you can use a different one if you wish to. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; To load a built-in R dataset, you can use data(), as follows: data(mtcars). To check if the data has been loaded correctly, simply type the name of the dataset, and you should see a preview on your console.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Once we have loaded our data, we can start building tables.&lt;/p&gt;


&lt;h2&gt;
  
  
  Creating a table object
&lt;/h2&gt;

&lt;p&gt;Let us begin by taking a look at the number of gears that each car model has. To do this, we will use the $ syntax:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It's a bit difficult to read, isn't it? If we use &lt;em&gt;table()&lt;/em&gt;, the information will be much more readable:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It looks a lot better this way, doesn't it? We can now tell, at a glance, that there are 15 cars with 3 gears, 12 cars with 4 gears and 5 cars with 5 gears. &lt;/p&gt;

&lt;p&gt;Now let's take a look at the number of cylinders for each car. As previously, we will first show all the data at once, and then use &lt;em&gt;table()&lt;/em&gt; to summarize it:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Simple enough, right? Let's take it one step further. What if we want to see the number of gears that each car has, &lt;strong&gt;in relation to the number of cylinders?&lt;/strong&gt; Here is where contingency tables come in. &lt;/p&gt;




&lt;h2&gt;
  
  
  Contingency tables
&lt;/h2&gt;

&lt;p&gt;Following the previous examples, we will create a contingency table showing the number of gears of each car, related to their number of cylinders. How do we do this? By once again, using &lt;em&gt;table()&lt;/em&gt;, this time with two arguments: The first argument will correspond to the &lt;strong&gt;rows&lt;/strong&gt;, and the second to the &lt;strong&gt;columns&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, each &lt;strong&gt;row&lt;/strong&gt; corresponds to each of the &lt;em&gt;gear&lt;/em&gt; values, which are 3,4 and 5. Each &lt;strong&gt;column&lt;/strong&gt; corresponds to each of the &lt;em&gt;cyl&lt;/em&gt; values, which are 4, 6 and 8. We can now easily know that there are 12 cars with 3 gears and 8 cylinders, or that there are no cars that have 4 gears and 8 cylinders (this can lead us to ask ourselves if perhaps this combination is mechanically incompatible?)&lt;/p&gt;

&lt;h2&gt;
  
  
  Selecting parts of a contingency table
&lt;/h2&gt;

&lt;p&gt;Have you ever tried to select a table property by using the $ notation? Then you'll be familiar with the &lt;strong&gt;$ operator is invalid for atomic vectors&lt;/strong&gt; error. Why does this happen? Well, objects of class table  are essentially vectors (arrays) which means that we can't use the $ syntax. This syntax &lt;strong&gt;is only valid for recursive objects&lt;/strong&gt;, and vectors are &lt;strong&gt;atomic objects&lt;/strong&gt;, hence the error.&lt;/p&gt;

&lt;p&gt;How do we interact with tables then? Here are several useful techniques to select and interact with tables:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;str()&lt;/strong&gt; - Examines and outputs the structure of a table object. The only drawback to this useful little function is that the output it provides us with is hard to interpret when you see it for the first time, which is why we are going to analyze what is going on with &lt;em&gt;str()&lt;/em&gt; output. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's break down that nasty output, to be able to understand what's going on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the first line, &lt;em&gt;str()&lt;/em&gt; is telling us that we have an object of type table: &lt;em&gt;'table'&lt;/em&gt;, which is a matrix of integers, with three rows and three columns: &lt;em&gt;int [1:3, 1:3]&lt;/em&gt;, and finally shows us all the values contained in our matrix, grouped by columns &lt;em&gt;1 8 2 2 4...&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The strange-looking &lt;em&gt;attr(&lt;/em&gt;, "dimnames")* code fragment in the second line is there because R stores the names of the columns and rows in an attribute called &lt;em&gt;dimnames&lt;/em&gt; (dimension names). It also tells us that this &lt;em&gt;dimnames&lt;/em&gt; attribute is a list of two components: &lt;em&gt;= List of 2&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;..$ :&lt;/em&gt; in the beginning of the fourth line indicates the beginning of a component, which is a character vector containing three values: &lt;em&gt;chr [1:3]&lt;/em&gt;, and ends with a list of the three values contained in the vector: &lt;em&gt;"3" "4" "5"&lt;/em&gt;, which you may have noticed, are the names of the three rows in our table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The fifth row is exactly the same as the fourth, save for the values contained in the vector, which are &lt;em&gt;"4" "6" "8"&lt;/em&gt; and correspond with the names of the three columns of our table.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;length()&lt;/strong&gt; - Returns the length of the table object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;table[x,]&lt;/strong&gt; - Returns the xᵗʰ row:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;table[1:x,]&lt;/strong&gt; - Returns the first x rows of the table:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;table[1:x,1:y]&lt;/strong&gt; - Returns the first x rows of the first y columns:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;table["3",]&lt;/strong&gt; - Returns the row named 3:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;table[,"8"]&lt;/strong&gt; - Returns the column named 8:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Frequencies in contingency tables
&lt;/h2&gt;

&lt;p&gt;Sometimes, we would prefer to display frequencies. By using &lt;em&gt;prop.table()&lt;/em&gt; together with &lt;em&gt;table()&lt;/em&gt;, we can create a contingency table of &lt;strong&gt;frequencies&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Would you like to display percentages instead of frequencies? It's as simple as multiplying by 100, like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Calculating marginal distributions
&lt;/h2&gt;

&lt;p&gt;Here are several useful functions for calculating marginal distributions in contingency tables:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;addmargins()&lt;/strong&gt; -  Will calculate the total values of both columns and rows:  &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;rowSums()&lt;/strong&gt; -  Will calculate the total values of each row:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;colSums()&lt;/strong&gt; -  Will calculate the total values of each column:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h2&gt;
  
  
  Three-way contingency tables
&lt;/h2&gt;

&lt;p&gt;So far we've worked with a two-way contingency table, but, what if we need to work with three variables? Is it possible? The answer is yes, it is. Let's create a &lt;strong&gt;three-way table&lt;/strong&gt; based on the &lt;em&gt;cyl&lt;/em&gt; and &lt;em&gt;carb&lt;/em&gt; and &lt;em&gt;gear&lt;/em&gt; variables:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, what is really going on is that we have &lt;strong&gt;three separate two-way contingency tables&lt;/strong&gt;, one for each of the gear values. This third &lt;em&gt;gear&lt;/em&gt; variable is known as a &lt;strong&gt;layer variable&lt;/strong&gt;, or as a &lt;strong&gt;control variable&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt; Instead of using &lt;em&gt;table()&lt;/em&gt;, we could use &lt;em&gt;ftable()&lt;/em&gt;, for a more concise view of our data.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, at the leftmost of our three-way contingency table, we have our &lt;em&gt;cyl&lt;/em&gt; rows, followed by the &lt;em&gt;carb&lt;/em&gt; rows, and on top, we have our &lt;em&gt;gear&lt;/em&gt; columns. Is it possible to create contingency tables with more variables? The answer is yes, but the complexity will increase as we add more and more variables. Here is an example of a four-way contingency table:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, it grows more &lt;strong&gt;complex&lt;/strong&gt; as we increase the number of variables, and loses legibility in the process. &lt;/p&gt;




&lt;h2&gt;
  
  
  Testing for independence
&lt;/h2&gt;

&lt;p&gt;We may want to test the independence of the row and column variables of our contingency table. To achieve this, there are several tests we can perform on our table:&lt;/p&gt;

&lt;h3&gt;
  
  
  The Chi-Square test
&lt;/h3&gt;

&lt;p&gt;One of the simplest ways to test for independence is to use the Chi-Square test function: &lt;em&gt;chisq.test()&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The important value of this test is the &lt;strong&gt;p-value&lt;/strong&gt;, which, as you can see, equals 0.001214. Since p-value is below the significance level (which is 5%, or 0.05), it looks like the variables are &lt;strong&gt;not independent&lt;/strong&gt;. However…&lt;/p&gt;

&lt;p&gt;You may have noticed the warning. This is because the value of at least one of the &lt;strong&gt;expected values&lt;/strong&gt; in the contingency table &lt;strong&gt;is below 5&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; please bear in mind that expected values &lt;strong&gt;are not&lt;/strong&gt; the values that appear in each cell, they are the result of calculating (row total*column total)/overall total). You can have low cell values in your table, yet have perfectly fine expected values, if you have a large enough sample. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therefore, we &lt;strong&gt;shouldn't trust the result of this test&lt;/strong&gt;, and should look for a different way to test for independence: in this case, we should use the &lt;strong&gt;Fisher test instead of the Chi-Square test&lt;/strong&gt;. Before diving into the Fisher test, I'd like to introduce:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Monte Carlo simulation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What happens if the expected values of our contingency table are below 5, but &lt;strong&gt;we can't use a different test&lt;/strong&gt; and have to stick with the Chi-Square test? In these cases, the Monte Carlo simulation is a great alternative. It allows us to perform the Chi-Square test with &lt;strong&gt;simulated values&lt;/strong&gt;, that attempt to make up for the low expected values of our table.To achieve this, all we need to do is set the simulate.p.value to true when calling &lt;em&gt;chisq.test()&lt;/em&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, the warning no longer appears, and the test has been performed with simulated p-value based on 2000 replicates, which is the default. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Fisher Exact test
&lt;/h3&gt;

&lt;p&gt;The general rule of thumb is, when at least one expected value is lower than five, use the Fisher test rather than the Chi-Square test. There are some who think that it may perhaps be time to retire this rule of 'no expected values lower than five' and use some other method to calculate whether or not a sample size is too small for the Chi-Square test.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; You can find a great post talking about small numbers in Chi-square tests &lt;a href="http://www.biostathandbook.com/small.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Regardless of the debate, our &lt;em&gt;mtcars&lt;/em&gt; sample is definitely &lt;strong&gt;too small&lt;/strong&gt; for the Chi-Square test. Therefore, we will use the Fisher test. To perform this test in R, we can use the &lt;em&gt;fisher.test()&lt;/em&gt; function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once again, the p-value is below the significance level (0.05). Therefore, we can conclude that the variables are &lt;strong&gt;not independent.&lt;/strong&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  The G-test 
&lt;/h3&gt;

&lt;p&gt;There is yet another method we can use to test for independence, and that is the G-test. Once again, just like the Chi-Square test, it is only reliable when used with &lt;strong&gt;large samples&lt;/strong&gt;. To be able to use the G-Test function in R, we must first import the &lt;em&gt;DescTools&lt;/em&gt; package, and then use the &lt;em&gt;GTest()&lt;/em&gt; function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Remember, for small samples, use the Fisher test. For large samples, use the Chi-Square test or the G-Test&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Measures of association
&lt;/h2&gt;

&lt;p&gt;We now know how to test for independence between contingency table variables. However, how can we know how strong this independence is? &lt;strong&gt;By calculating association measures.&lt;/strong&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  Phi Coefficient
&lt;/h3&gt;

&lt;p&gt;The simplest way to calculate the strength of association in a &lt;strong&gt;2x2&lt;/strong&gt; contingency table is by using the &lt;strong&gt;Phi Coefficient&lt;/strong&gt;. This measure is related to the Chi-Square test for a two-way contingency table in the following way:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhyv0lh9hwn4n3ay273gt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhyv0lh9hwn4n3ay273gt.png" alt="Phi Coefficient "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ϕ&lt;/strong&gt; is the Phi Coefficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;x²&lt;/strong&gt; is the chi-squared statistic from the Chi-Square test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;n&lt;/strong&gt; is the total number of observations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The range of the Phi Coefficient is [-1, 1], and its interpretation is the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 means there is &lt;strong&gt;no association&lt;/strong&gt; between variables. They are &lt;strong&gt;independent.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;1 means a perfect positive association. This is when most of the data &lt;strong&gt;falls along the diagonal cells.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;-1 means a perfect negative relationship. This is when most of the data &lt;strong&gt;falls away from the diagonal cells.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since we need a 2x2 table to perform this test, we will calculate the Phi Coefficient for the following contingency table:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fffq9ho8ewc1yrl3exe14.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fffq9ho8ewc1yrl3exe14.png" alt="2x2 contingency table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To calculate the Phi Coefficient in R, we can use the &lt;em&gt;phi()&lt;/em&gt; function in the &lt;em&gt;psych&lt;/em&gt; package:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Since the result is negative, we know that we have a &lt;strong&gt;negative relationship.&lt;/strong&gt; Since the result is close to 0, we know that the &lt;strong&gt;strength of the relationship is very weak&lt;/strong&gt;, which translates to there being &lt;strong&gt;next to no relationship.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Remember that the Phi Coefficient can only be used for &lt;strong&gt;2x2&lt;/strong&gt; contingency tables. For larger tables, we should use &lt;em&gt;Cramer's V&lt;/em&gt; or &lt;em&gt;Tschuprow's T&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Cramer's V and Tschuprow's T
&lt;/h3&gt;

&lt;p&gt;Both &lt;em&gt;Cramer's V&lt;/em&gt; and &lt;em&gt;Tschuprow's T&lt;/em&gt; are closely related to the Chi-Square test. Because the latter is dependent on the &lt;strong&gt;size of the sample&lt;/strong&gt;, it's a &lt;strong&gt;poor reflection&lt;/strong&gt; of the strength of association. Here is where &lt;em&gt;Cramer's V&lt;/em&gt; and &lt;em&gt;Tschuprow's&lt;/em&gt; T come in:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxmo2w5ds2fwealg7n84n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxmo2w5ds2fwealg7n84n.png" alt="Cramer's V"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;φ&lt;/strong&gt; is the Phi Coefficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;x²&lt;/strong&gt; is the chi-squared statistic from the Chi-Square test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;n&lt;/strong&gt; is the total number of observations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;k&lt;/strong&gt; is the number of columns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;r&lt;/strong&gt; is the number of rows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And here is Tschuprow's T:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2g87w8h8q9qye3bou1od.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2g87w8h8q9qye3bou1od.png" alt="Tschuprow's T"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;x²&lt;/strong&gt; is the chi-squared statistic from the Chi-Square test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;n&lt;/strong&gt; is the total number of observations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;k&lt;/strong&gt; is the number of rows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;c&lt;/strong&gt; is the number of columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both &lt;em&gt;Cramer's V&lt;/em&gt; and &lt;em&gt;Tschuprow's T&lt;/em&gt;, produce a value in the [0,1] range. The interpretation of this value is the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;0 means there is no association between variables. They are &lt;strong&gt;independent.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;1&amp;gt;x&amp;gt;0 (any number larger than 0, smaller than 1) means that &lt;strong&gt;there exists a relationship of association.&lt;/strong&gt; The only problem with both &lt;em&gt;Cramer's V&lt;/em&gt; and &lt;em&gt;Tschuprow's T&lt;/em&gt; is that we have no way of knowing whether the obtained value represents a weak, intermediate or strong association.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;1 means &lt;strong&gt;perfect dependency&lt;/strong&gt; in the case of &lt;em&gt;Tschuprow's T&lt;/em&gt;, but not in the case of &lt;em&gt;Cramer's V&lt;/em&gt;, where it only means that there exists a &lt;strong&gt;high dependency.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To calculate &lt;em&gt;Cramer's V&lt;/em&gt; in R, we need to use the &lt;em&gt;CramerV()&lt;/em&gt; function from the &lt;em&gt;DescTools&lt;/em&gt; package (since we already imported &lt;em&gt;DescTools&lt;/em&gt; previously, for the G-Test, we don't need to do so again):&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Since the value is larger than 0, we know that the variables are &lt;strong&gt;associated&lt;/strong&gt;. However, we can't know whether or not that association is weak or strong…&lt;br&gt;
To calculate Tschuprow's T in R, we can use the TschuprowT() function, once again from the DescTools package:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Bias correction&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.sciencedirect.com/science/article/abs/pii/S1226319212001032?via%3Dihub" rel="noopener noreferrer"&gt;From A bias-correction for Cramér's V and Tschuprow's T:&lt;/a&gt;&lt;/strong&gt; It's important to know that both &lt;em&gt;Cramer's V&lt;/em&gt; and &lt;em&gt;Tschuprow's T&lt;/em&gt; are considerably biased, and tend to overestimate the strength of association.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How do we avoid this? By applying a &lt;strong&gt;bias correction&lt;/strong&gt;. Here is an example of &lt;em&gt;Cramer's V&lt;/em&gt; with a bias correction applied in R:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And an example of &lt;em&gt;Tschuprow's T&lt;/em&gt; with an applied bias correction in R:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





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

&lt;p&gt;And that's all for today, folks. We covered everything from the concept of a contingency table to independence tests and calculations of association measures. There are some widely used tests, like the Cochran-Mantel-Haenszel test for conditional independence or the Breslow-Day test for homogeneous association that we didn't mention, perhaps we could talk about them in another tutorial. &lt;/p&gt;

&lt;p&gt;As always, I hope you enjoyed this tutorial and/or found it useful. Don't forget to let me know your thoughts in the comments below! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt; &lt;em&gt;I am a full stack web developer with an interest in R. I initially wrote most of this material as part of my own learning process, and decided to publish it in case it helped others who are also learning R. Comments, tips and advice are welcome!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>r</category>
      <category>statistics</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Art of Refactoring: 5 tips to Write Better Code</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Thu, 10 Oct 2019 20:16:16 +0000</pubDate>
      <link>https://dev.to/nyagarcia/the-art-of-refactoring-5-tips-to-write-better-code-12if</link>
      <guid>https://dev.to/nyagarcia/the-art-of-refactoring-5-tips-to-write-better-code-12if</guid>
      <description>

&lt;p&gt;Bad code works. We all know this. Developers have been writing code for years without giving a single thought to whether they were doing it right or not. It's understandable, isn't it? After all, we already have to deal with the stress of keeping up with the industry and the demands of our job… &lt;/p&gt;

&lt;p&gt;The answer is &lt;strong&gt;no&lt;/strong&gt;. Writing bad code comes at a &lt;strong&gt;price&lt;/strong&gt;. Have you ever faced the issue of &lt;strong&gt;not understanding your own code&lt;/strong&gt; after a couple of weeks, and having to spend hours, or even days figuring out what was going on? &lt;/p&gt;

&lt;p&gt;The solution to this (extremely) common problem is to make your code as &lt;strong&gt;clear&lt;/strong&gt; and &lt;strong&gt;informative&lt;/strong&gt; as possible. I will go as far as to say that your code should be understood even by a non-technical person. It's time to drop our excuses, and improve the quality of your code. &lt;/p&gt;

&lt;p&gt;Writing clean code isn't that complicated. This tutorial will show you 5 simple techniques to improve your code, with practical examples:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get rid of switch statements&lt;/li&gt;
&lt;li&gt;Make your conditionals descriptive&lt;/li&gt;
&lt;li&gt;Use guard clauses to avoid nested if statements&lt;/li&gt;
&lt;li&gt;Avoid code duplication&lt;/li&gt;
&lt;li&gt;Functions should only do one thing&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Get rid of switch statements
&lt;/h1&gt;

&lt;p&gt;We normally use switch statements to avoid large if else if statements. However, switch statements are very &lt;strong&gt;verbose&lt;/strong&gt;, hard to &lt;strong&gt;maintain&lt;/strong&gt; and even harder to &lt;strong&gt;debug&lt;/strong&gt;. They clutter up our code, and, in my humble opinion, have an odd, uncomfortable syntax. When adding more cases, we have to &lt;strong&gt;manually add each case and break statement&lt;/strong&gt;, which is quite error-prone. &lt;/p&gt;

&lt;p&gt;Let's take a look at an example of a switch statement:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Imagine that we need to add more cases to the switch statement. The amount of code that we would have to write is &lt;strong&gt;considerable&lt;/strong&gt;. We would probably end up copy-pasting code, and we all know how that ends. &lt;/p&gt;

&lt;p&gt;So, how do we avoid switch statements? By using an &lt;strong&gt;object literal&lt;/strong&gt;. Object literals are simple, easy to write, read and maintain. We are all used to handling objects in JavaScript, and the syntax is a lot fresher than that of the switch statement. Here is an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;As you can see, we can add a &lt;strong&gt;default&lt;/strong&gt; value by using the &lt;strong&gt;|| operator&lt;/strong&gt;. If the type isn't found in the &lt;em&gt;pokemon object&lt;/em&gt;, the &lt;em&gt;getPokemon&lt;/em&gt; function will return 'Mew' as a default value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; As you will probably have noticed, we declare the pokemon object &lt;strong&gt;outside the function&lt;/strong&gt;, instead of inside. We do this to prevent it from being created each time we execute the function. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can also use a &lt;strong&gt;map&lt;/strong&gt; to achieve the same result. A map is a collection of key value pairs, just like an object. The difference is that map allows &lt;strong&gt;keys of any type&lt;/strong&gt;, while objects only allow strings as keys. Also, map has an interesting series of properties and methods. You can read more about the map structure &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here's how to use map:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;As you can see, our code looks a lot cleaner and straightforward when replacing switch statements with an object literal or map. &lt;/p&gt;


&lt;h1&gt;
  
  
  Make your conditionals descriptive
&lt;/h1&gt;

&lt;p&gt;Conditional statements are an absolute necessity when writing code. However, they can quickly get out of hand, and end up being impossible to understand. This leads to either having to &lt;strong&gt;write comments&lt;/strong&gt; explaining what the statement does, or having to spend valuable time tracing back our own code to understand what's going on. &lt;strong&gt;This is bad.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Take a look at the following statement:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;If we only look at the code inside the if statement in the previous function, it's difficult to understand what's going on. Our code isn't clear, and &lt;strong&gt;unclear code leads only to technical debt, bugs,&lt;/strong&gt; and &lt;strong&gt;significant headaches.&lt;/strong&gt;&lt;br&gt;
 &lt;br&gt;
How can we improve our conditional? By &lt;strong&gt;extracting it into a function.&lt;/strong&gt; Here's how:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;By extracting the conditional into a function with a descriptive name: isGameLost(), our checkGameStatus function is now understandable at a glance. Why? Because our code is informative, it tells us what is going on, which is what we should always strive for. &lt;/p&gt;


&lt;h1&gt;
  
  
  Use guard clauses to avoid nested if statements
&lt;/h1&gt;

&lt;p&gt;Nested if statements are one of the worst things we can encounter in code. I've seen nested ifs 10 levels deep… Believe me when I tell you that it was an absolute nightmare to be able to fully grasp what was going on in that code. Here's an example of a nested if statement (only three levels deep though, I'm not a monster):&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;You probably have to take a couple of minutes, and read up and down to follow the flow of the function. Nested if statements are hard to both read and understand. So, how do we get rid of the nasty nested if statement? By reversing the logic and using what we call a &lt;strong&gt;guard clause.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In computer programming, a &lt;strong&gt;guard&lt;/strong&gt; is a boolean expression that must evaluate to true if the program execution is to continue in the branch in question. - &lt;strong&gt;Wikipedia&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By reversing the logic of our function, and placing the conditions that cause an &lt;strong&gt;early exit&lt;/strong&gt; in the &lt;strong&gt;beginning&lt;/strong&gt; of the function, they will act as &lt;strong&gt;guards&lt;/strong&gt;, and only allow our function to continue executing if all the &lt;strong&gt;conditions are met&lt;/strong&gt;. This way, we can avoid else statements. Here's how to refactor our previous function to use guard clauses:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, the code is a lot cleaner and easier to understand. We can see what the function does simply by reading down, following the &lt;strong&gt;natural flow&lt;/strong&gt; of the function, unlike before, where we had to read up and down. &lt;/p&gt;




&lt;h1&gt;
  
  
  Avoid code duplication
&lt;/h1&gt;

&lt;p&gt;Duplicating code &lt;strong&gt;always&lt;/strong&gt; ends badly. It leads to situations such as: "I fixed this bug here, but forgot to do it there" or "I need to make a change/add a new feature, and have to do it in five different places". &lt;br&gt;
Just as the &lt;a href="https://en.wikipedia.org/wiki/Don't_repeat_yourself"&gt;DRY&lt;/a&gt; (don't repeat yourself) principle states: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Every piece of knowledge or logic must have a single, unambiguous representation within a system.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Therefore, having &lt;strong&gt;less code is good&lt;/strong&gt;: It saves us both &lt;strong&gt;time&lt;/strong&gt; and &lt;strong&gt;effort&lt;/strong&gt;, is easier to maintain, and reduces the chances of bugs appearing.&lt;/p&gt;

&lt;p&gt;So, how do we get rid of duplicated code? The answer is not always simple, but &lt;strong&gt;extracting logic to functions/variables&lt;/strong&gt; usually works just fine. Let's take a look at the following code, which I ran across when refactoring an application:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You'll probably have noticed that the for loop is &lt;strong&gt;exactly the same in both functions&lt;/strong&gt;, except for one little detail: the type of news that we want, which is either &lt;em&gt;JavaScript&lt;/em&gt; or &lt;em&gt;Rust&lt;/em&gt; news. To avoid this duplication, we can &lt;strong&gt;extract the for loop into a function&lt;/strong&gt;, which we then call from the &lt;em&gt;getJavascriptNews&lt;/em&gt;, &lt;em&gt;getRustNews&lt;/em&gt; and &lt;em&gt;getGolangNews&lt;/em&gt; functions. Here's how:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After extracting the for loop into the getNewsContent function, our &lt;em&gt;getJavascriptNews&lt;/em&gt;, &lt;em&gt;getRustNews&lt;/em&gt; and &lt;em&gt;getGolangNews&lt;/em&gt; functions have turned into &lt;strong&gt;simple, clear one liners&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further refactoring
&lt;/h3&gt;

&lt;p&gt;However, have you realized that, once again, both functions are &lt;strong&gt;exactly the same except for the type string&lt;/strong&gt; that we pass into the &lt;em&gt;getNewsContent&lt;/em&gt; function? This is something that usually happens when we refactor our code. More often than not, one change leads to another change, and so on, until our refactored code ends up being half the size of the original. &lt;strong&gt;Let your code tell you what it needs:&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Further refactoringWhere did our &lt;em&gt;getJavascriptNews&lt;/em&gt;, &lt;em&gt;getRustNews&lt;/em&gt; and &lt;em&gt;getGolangNews&lt;/em&gt; functions go? We substituted them for a getNews function, which &lt;strong&gt;receives the type of news&lt;/strong&gt; as an argument. This way, no matter how many more types of news we add, we always use the same function. This is called &lt;strong&gt;abstraction&lt;/strong&gt;, and allows us to &lt;strong&gt;reuse functions&lt;/strong&gt;, thus being incredibly useful. Abstraction is  one of the techniques I use most frequently in my code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus: Make the for loop more readable with ES6 features
&lt;/h3&gt;

&lt;p&gt;This is the last refactoring, I swear. &lt;br&gt;
For loops aren't precisely readable. With the introduction of ES6 Array functions, we can avoid using them 95% of the time. In our case, we can use &lt;strong&gt;Array.filter&lt;/strong&gt; combined with &lt;strong&gt;Array.map&lt;/strong&gt; to substitute the original loop:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;With Array.filter we return only the elements whose &lt;strong&gt;type equals the type passed as an argument.&lt;/strong&gt;
 &lt;/li&gt;
&lt;li&gt;With Array.map, we return only the &lt;strong&gt;content&lt;/strong&gt; property of the item object, instead of the whole item. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Congratulations, after three simple refactorings, our initial three functions have been reduced to two, which are much easier to understand and maintain. Also, by using abstraction, we made the &lt;em&gt;getNews&lt;/em&gt; function &lt;strong&gt;reusable.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Functions should only do one thing
&lt;/h1&gt;

&lt;p&gt;Functions should only do one thing, and one thing only. Functions that do more than one thing are the &lt;strong&gt;root of all evil&lt;/strong&gt;, and one of the worst things we can encounter in code (together with nested ifs). They are &lt;strong&gt;messy&lt;/strong&gt;, and make our code &lt;strong&gt;hard to understand&lt;/strong&gt;. Here's an example of a complex function from a real application:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Since the handlers for the event listeners were not needed for this example, I chose to remove them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you can see, it's confusing, and hard to understand what's going on in there. If any bugs come up, it will be quite difficult to find and fix them. How can we improve our &lt;em&gt;startProgram&lt;/em&gt; function? By &lt;strong&gt;extracting common logic into functions.&lt;/strong&gt; Here's how:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's go through the changes made to the &lt;em&gt;startProgram&lt;/em&gt; function:&lt;/p&gt;

&lt;p&gt;First, we got rid of the if else statement by using a &lt;strong&gt;guard clause&lt;/strong&gt;. Then, we extracted the logic needed to &lt;strong&gt;start the database&lt;/strong&gt; into an &lt;em&gt;initDatabase&lt;/em&gt; function and  the logic to &lt;strong&gt;add event listeners&lt;/strong&gt; to a &lt;em&gt;setListeners&lt;/em&gt; function.&lt;/p&gt;

&lt;p&gt;The logic for printing the employee list is slightly more complex, so we created three functions: &lt;em&gt;printEmployeeList&lt;/em&gt;, &lt;em&gt;formatEmployeeList&lt;/em&gt;, and &lt;em&gt;getEmployeeList&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;getEmployeeList&lt;/em&gt; is responsible for making a &lt;strong&gt;GET request&lt;/strong&gt; to &lt;em&gt;employeeList.json&lt;/em&gt;, and returning the response in json format. &lt;/p&gt;

&lt;p&gt;It is then called by the &lt;em&gt;printEmployeeList&lt;/em&gt; function, which takes the list of employees, and passes it to the &lt;em&gt;formatEmployeeList&lt;/em&gt; function, which formats and returns it. Then, the list is printed. &lt;/p&gt;

&lt;p&gt;As you can see, every function is responsible &lt;strong&gt;for doing only one thing.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We could still make a few more changes to the function, and honestly, the application is begging for the &lt;strong&gt;separation of the view from the controller&lt;/strong&gt;, but on the whole, our &lt;em&gt;startProgram&lt;/em&gt; function is now more &lt;strong&gt;informative&lt;/strong&gt;, and there is absolutely no difficulty in understanding what it does. We would have no problem at all if we had to come back to this code after a couple of months.&lt;/p&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Programmers are the only ones responsible for writing good, quality code. We should all make it a habit to write good code from the very first line. Writing clean code &lt;strong&gt;isn't complicated&lt;/strong&gt;, and doing so will help both you and your colleagues.&lt;/p&gt;

&lt;p&gt;By applying the 5 simple techniques shown in this tutorial, your code quality should &lt;strong&gt;improve considerably&lt;/strong&gt;, and so will your productivity. &lt;/p&gt;

&lt;p&gt;If you have any questions, don't hesitate to ask. Thank you for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding the JavaScript Spread Operator - From Beginner to Expert Part 2</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Wed, 25 Sep 2019 17:18:11 +0000</pubDate>
      <link>https://dev.to/nyagarcia/understanding-the-javascript-spread-operator-from-beginner-to-expert-part-2-24ff</link>
      <guid>https://dev.to/nyagarcia/understanding-the-javascript-spread-operator-from-beginner-to-expert-part-2-24ff</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;The spread operator, …, was  first introduced in ES6. It quickly became one of the most popular  features. So much so that despite the fact it only worked on arrays, a  proposal was made to extend its functionalities to objects. This feature  was finally introduced in ES9.&lt;/p&gt;

&lt;p&gt;The goal of this tutorial, which is divided into two parts, is to show you why the spread operator should be used, how it works, and to deep dive into its uses, from the most basic to the most advanced. If you haven't read the first part of this tutorial, I encourage you to do so! Here is the link:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/understanding-the-javascript-spread-operator-from-beginner-to-expert-5bdb"&gt;Understanding the JavaScript Spread Operator - From Beginner to Expert&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a short summary of the contents of this tutorial:&lt;/p&gt;

&lt;h3&gt;
  
  
  Part 1
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Why the spread operator should be used&lt;/li&gt;
&lt;li&gt;Cloning arrays/objects&lt;/li&gt;
&lt;li&gt;Converting array-like structures to array&lt;/li&gt;
&lt;li&gt;The spread operator as an argument&lt;/li&gt;
&lt;li&gt;Adding elements to arrays/objects&lt;/li&gt;
&lt;li&gt;Merging arrays/objects&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Part 2
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Destructuring nested elements&lt;/li&gt;
&lt;li&gt;Adding conditional properties&lt;/li&gt;
&lt;li&gt;Short circuiting&lt;/li&gt;
&lt;li&gt;The rest parameter (…)&lt;/li&gt;
&lt;li&gt;Default destructuring values&lt;/li&gt;
&lt;li&gt;Default properties&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Cloning arrays/objects with nested elements
&lt;/h1&gt;

&lt;p&gt;In the first part of this article, we learnt about reference data types, accidental variable mutation, and how we could solve this problem by cloning arrays/objects &lt;strong&gt;immutably&lt;/strong&gt;, with the spread operator. &lt;/p&gt;

&lt;p&gt;However, there's a slight problem with this approach, when it comes to nested reference data types: The spread operator only performs a &lt;strong&gt;shallow clone&lt;/strong&gt;. What does this mean? If we attempt to clone an object that contains an array, for example, the array inside the cloned object will contain a reference to the memory address where the original array is stored… This means that, while our object is immutable, the &lt;strong&gt;array inside it isn't&lt;/strong&gt;. Here's an example to illustrate this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, our &lt;em&gt;squirtleClone&lt;/em&gt; has been cloned &lt;strong&gt;immutably&lt;/strong&gt;. When we change the name property of the original &lt;em&gt;pokemon&lt;/em&gt; object to 'Charmander', our &lt;em&gt;squirtleClone&lt;/em&gt; isn't affected, its &lt;em&gt;name&lt;/em&gt; property &lt;strong&gt;isn't mutated&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;However, when we add a new ability to the &lt;em&gt;abilities&lt;/em&gt; property of the original &lt;em&gt;pokemon&lt;/em&gt; object… Our &lt;em&gt;squirtleClone&lt;/em&gt;'s abilities &lt;strong&gt;are affected by the change&lt;/strong&gt;. Because the abilities property is a &lt;strong&gt;reference data type&lt;/strong&gt;, it isn't cloned immutably.  Welcome to the reality of JavaScript :)&lt;/p&gt;

&lt;p&gt;One of the solutions to this problem would be to use the spread operator to clone the nested properties, as shown in the following example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For obvious reasons, this &lt;strong&gt;isn't an ideal approach&lt;/strong&gt; to solving our problem. We would need to use the spread operator for every single reference type property, which is why this approach is only valid for small objects. So, which is the optimal solution? &lt;strong&gt;Deep cloning&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Since there is plenty to say about deep cloning, I won't be going into too much detail. I'd just like to say that the correct of deep cloning is either using an external library (for example, &lt;a href="https://lodash.com"&gt;Lodash&lt;/a&gt;), or writing ourselves a function that does it. &lt;/p&gt;




&lt;h1&gt;
  
  
  Adding conditional properties
&lt;/h1&gt;

&lt;p&gt;Sometimes we need to add properties to an object, but we don't know whether or not those properties exist. This doesn't pose much of a problem, we can always check if the property exists with an if statement:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There is, however, a much simpler way of achieving the same result, by using &lt;strong&gt;short circuiting conditionals with the &amp;amp;&amp;amp; operator&lt;/strong&gt;. A brief explanation:&lt;/p&gt;

&lt;h3&gt;
  
  
  Short circuiting
&lt;/h3&gt;

&lt;p&gt;When we evaluate an expression with &amp;amp;&amp;amp;, if the first operand &lt;strong&gt;is false&lt;/strong&gt;, JavaScript will short-circuit and &lt;strong&gt;ignore the second operand&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Let's take a look at the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If &lt;em&gt;starterPokemon.length &amp;gt; 0&lt;/em&gt; is false (the array is empty), the statement will short circuit, and our &lt;em&gt;choosePokemon&lt;/em&gt; function &lt;strong&gt;will never be executed&lt;/strong&gt;. This is why the previous code is equivalent to using the traditional if statement.&lt;/p&gt;

&lt;p&gt;Going back to our original problem, we can take advantage of the logical AND operator to add conditional properties to an object. Here's how:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;What's going on here? Allow me to explain:&lt;/p&gt;

&lt;p&gt;As we already know, by using the &amp;amp;&amp;amp; operator, the second part of the statement will only be executed if the &lt;strong&gt;first operand is true&lt;/strong&gt;. Therefore, only if the abilities variable is true (if the variable exists), will the second half of the statement be executed. What does this second half do? It &lt;strong&gt;creates an object containing the abilities variable&lt;/strong&gt;, which is then &lt;strong&gt;destructured with the spread operator placed in front of the statement&lt;/strong&gt;, thus adding the existent abilities variable into our fullPokemon object &lt;strong&gt;immutably&lt;/strong&gt;. &lt;/p&gt;




&lt;p&gt;Before we can introduce our final advanced spreading use, adding default properties to objects, we must first dive into two new concepts: &lt;strong&gt;default destructuring values&lt;/strong&gt;, and the &lt;strong&gt;rest parameter&lt;/strong&gt;. Once we are familiar with these techniques, we will be able to combine them to &lt;strong&gt;add default properties to objects&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Default destructuring values
&lt;/h1&gt;

&lt;p&gt;If we try to destructure an array element or object property that doesn't exist, we will get an undefined variable. How can we avoid undefined values? By using &lt;strong&gt;defaults&lt;/strong&gt;. How does this work?&lt;/p&gt;

&lt;p&gt;We can assign default values to the variables we destructure, inside the actual destructuring statement. Here's an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, by adding the default value 'Water' to the &lt;em&gt;type&lt;/em&gt; variable in the destructuring statement, we avoid an undefined variable in the case of the &lt;em&gt;pokemon&lt;/em&gt; object not having the &lt;em&gt;type&lt;/em&gt; property.&lt;/p&gt;

&lt;h1&gt;
  
  
  The rest parameter (…)
&lt;/h1&gt;

&lt;p&gt;You may be surprised to hear that the spread operator is &lt;strong&gt;overloaded&lt;/strong&gt;. This means that it has more than one function. It's second function is to act as the &lt;strong&gt;rest parameter&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;From the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters"&gt;MDN docs&lt;/a&gt;:&lt;/strong&gt; A function's last parameter can be prefixed with ... which  will cause all remaining (user supplied) arguments to be placed within a  "standard" javascript array. Only the last parameter can be a "rest  parameter".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Simply put, the rest operator takes all remaining elements (this is the reason it's named rest, as in the &lt;strong&gt;rest of the elements :p&lt;/strong&gt; ) and places them into an array. Here's an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, we can pass as many abilities as we want to the &lt;em&gt;printPokemon&lt;/em&gt; function. Every single value we introduce after the type parameter (the &lt;strong&gt;rest&lt;/strong&gt; of the parameters) will be &lt;strong&gt;collected into an array&lt;/strong&gt;, which we then turn into a string with the join function, and print out.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Remember that the rest parameter &lt;strong&gt;must be the last parameter&lt;/strong&gt;, or an error will occur.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The rest parameter can also be used when &lt;strong&gt;destructuring&lt;/strong&gt;, which is the part that interests us. It allows us to obtain the &lt;strong&gt;remaining properties in an object&lt;/strong&gt;, and store them in an array. Here's an example of the rest parameter used in a destructuring assignment:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As shown above, we can use the rest operator to destructure the remaining properties in the &lt;em&gt;pokemon&lt;/em&gt; object. Like in the previous example, our &lt;em&gt;pokemon&lt;/em&gt; object can have as many properties as we want defined after the &lt;em&gt;id&lt;/em&gt; property, they will all be collected by the rest parameter.&lt;/p&gt;

&lt;p&gt;Now that we know how the rest parameter works, and how to apply it in destructuring assignments, let's return to dealing with &lt;strong&gt;default properties&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Adding default properties
&lt;/h1&gt;

&lt;p&gt;Sometimes, we have a large amount of similar objects, that aren't quite exactly the same. Some of them lack properties that the other objects do have. However, we need all of our objects to &lt;strong&gt;have the same properties&lt;/strong&gt;, simply for the sake of order and coherence. How can we achieve this? &lt;/p&gt;

&lt;p&gt;By setting &lt;strong&gt;default properties&lt;/strong&gt;. These are properties with a &lt;strong&gt;default value&lt;/strong&gt; that will be added to our object, if it doesn't already have that property. By using the &lt;strong&gt;rest parameter&lt;/strong&gt; combined with &lt;strong&gt;default values&lt;/strong&gt; and the &lt;strong&gt;spread operator&lt;/strong&gt;, we can add default properties to an object.  It may sound a bit daunting, but it's actually quite simple. Here's an example of how to do it:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;What's going on in the previous code fragment? Let's break it down:&lt;/p&gt;

&lt;p&gt;As you can see, when we destructure the abilities property, we are adding a default value (&lt;strong&gt;[]&lt;/strong&gt;). As we already know, the default value &lt;strong&gt;will only be assigned to the abilities variable if it doesn't exist in the pokemon object&lt;/strong&gt;. In the same line, we are gathering the &lt;strong&gt;remaining properties&lt;/strong&gt; (name and type) of the &lt;em&gt;pokemon&lt;/em&gt; object into a variable named rest, by making use of the awesome &lt;strong&gt;rest parameter&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;On line 7, we are &lt;strong&gt;spreading&lt;/strong&gt; the rest variable (which, as you can see, is an object containing the name and type properties) inside an object literal, to generate a new object. We are also adding the &lt;em&gt;abilities&lt;/em&gt; variable, which in this case, is an &lt;strong&gt;empty array&lt;/strong&gt;, since that is what we specified as its default value on the previous line.&lt;/p&gt;

&lt;p&gt;In the case of our original &lt;em&gt;pokemon&lt;/em&gt; object already having an &lt;em&gt;abilities&lt;/em&gt; property, the previous code &lt;strong&gt;would not have modified it&lt;/strong&gt;, and it would &lt;strong&gt;maintain its original value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, this is how we add default properties to an object. Let's place the previous code into a function, and apply it to a large collection of objects:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, all the pokemon in the array now have an abilities property. In the case of &lt;em&gt;charmander&lt;/em&gt; and &lt;em&gt;bulbasur&lt;/em&gt;, they have an empty array, since that is the default value we assigned. However, the &lt;em&gt;squirtle&lt;/em&gt; object &lt;strong&gt;maintains its original array of abilities&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;There are, of course, other ways of adding default properties to an object, mainly by using if statements. However, I wanted to show an interesting new way of doing it, by using a combination of default values, the rest parameter, and the spread operator. You can then choose which approach suits you best :)&lt;/p&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This is the second and final part of the &lt;strong&gt;Understanding the JavaScript Spread Operator - From Beginner to Expert&lt;/strong&gt; tutorial. Here's a link to the &lt;a href="https://dev.to/nyagarcia/understanding-the-javascript-spread-operator-from-beginner-to-expert-5bdb"&gt;first part&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this second part of the tutorial we have learnt some more advanced uses of the spread operator, which include destructuring nested elements, adding conditional properties and adding default properties. We have also learnt three interesting JS concepts: short-circuiting, default destructuring values and the rest parameter. &lt;/p&gt;

&lt;p&gt;I sincerely hope you've found this piece useful, thank you for reading :) If you can think of any more uses of the spread operator or would like to comment something, don't hesitate to reach out, here's a link to my &lt;a href="https://twitter.com/nyablk"&gt;Twitter page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Understanding the JavaScript Spread Operator - From Beginner to Expert</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Mon, 16 Sep 2019 19:54:00 +0000</pubDate>
      <link>https://dev.to/nyagarcia/understanding-the-javascript-spread-operator-from-beginner-to-expert-5bdb</link>
      <guid>https://dev.to/nyagarcia/understanding-the-javascript-spread-operator-from-beginner-to-expert-5bdb</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;The spread operator '…' was first introduced in ES6. It quickly became one of the most popular features. So much so, that despite the fact that it only worked on Arrays, a proposal was made to extend its functionalities to Objects. This feature was finally introduced in ES9.&lt;/p&gt;

&lt;p&gt;The goal of this tutorial, which is divided into two parts, is to show you why the spread operator should be used, how it works, and deep dive into its uses, from the most basic to the most advanced. &lt;/p&gt;

&lt;p&gt;Here is a short summary of the contents of this tutorial:&lt;/p&gt;

&lt;h1&gt;
  
  
  What we are going to learn
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Part 1
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Why the spread operator should be used&lt;/li&gt;
&lt;li&gt;Cloning Arrays/Objects&lt;/li&gt;
&lt;li&gt;Converting Array-like structures to Array&lt;/li&gt;
&lt;li&gt;The spread operator as an argument&lt;/li&gt;
&lt;li&gt;Adding elements to Arrays/Objects&lt;/li&gt;
&lt;li&gt;Merging Arrays/Objects&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Part 2
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Destructuring nested elements&lt;/li&gt;
&lt;li&gt;Adding conditional properties&lt;/li&gt;
&lt;li&gt;Short circuiting&lt;/li&gt;
&lt;li&gt;The rest parameter (…)&lt;/li&gt;
&lt;li&gt;Default destructuring values&lt;/li&gt;
&lt;li&gt;Default properties&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Why you should use the spread operator
&lt;/h1&gt;

&lt;p&gt;After reading the previous list, you may be thinking something along these lines: &lt;em&gt;"But JavaScript has functions to do all those things… Why would I use the spread operator?"&lt;/em&gt; Allow me to introduce you to &lt;strong&gt;immutability&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;From Oxford Lexico:&lt;/strong&gt; Immutability -  unchanging over time or unable to be changed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In software development, we use the term immutable to refer to values whose state &lt;strong&gt;cannot change over time&lt;/strong&gt;. In fact, most of the values that we normally use (Primitive values, such as Strings , Integers etc.) are immutable. &lt;/p&gt;

&lt;p&gt;However, JavaScript has a peculiar behavior when it comes to Arrays and Objects; they are, in fact, &lt;strong&gt;mutable&lt;/strong&gt;. This can become a &lt;strong&gt;big&lt;/strong&gt; problem. Here's an example, illustrating why:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see in the previous code fragment, we have a Squirtle. Our Squirtle has a hp of 100, since we just visited the Pokemon Center. &lt;/p&gt;

&lt;p&gt;Since we want another Squirtle, we declare the variable &lt;em&gt;anotherSquirtle&lt;/em&gt;, assigning our original Squirtle as its value. After an arduous battle, &lt;em&gt;anotherSquirtle&lt;/em&gt; is defeated. We therefore access &lt;em&gt;anotherSquirtle&lt;/em&gt;'s hp, and change it to 0. The next step is to check on our original Squirtle. We console.log and…&lt;/p&gt;

&lt;p&gt;Wait, what? Our original Squirtle's hp is down to 0! How can this be? What happened to our poor Squirtle? &lt;strong&gt;JavaScript mutation happened&lt;/strong&gt;. Let me explain what's going on. &lt;/p&gt;

&lt;p&gt;When we created the &lt;strong&gt;anotherSquirtle variable&lt;/strong&gt;, and assigned our original Squirtle as its value, what we really did was &lt;strong&gt;assign a reference to the memory location of the original Squirtle Object&lt;/strong&gt;. This is because JavaScript Arrays and Objects are reference data types. Unlike Primitive data types, they &lt;strong&gt;point to the memory address where the actual Object/Array is stored&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To make it easier to understand, you can imagine reference data types as pointers to a global variable. By changing a reference data type's value, what we are really doing is changing the value of the global variable. &lt;/p&gt;

&lt;p&gt;This means that, when we changed the &lt;strong&gt;anotherSquirtle&lt;/strong&gt;'s hp value to 0, &lt;strong&gt;we were really changing the hp value of the Squirtle Object stored in memory to 0&lt;/strong&gt;. This is why &lt;em&gt;mySquirtle&lt;/em&gt;'s hp value is 0, because &lt;em&gt;mySquirtle&lt;/em&gt; holds a reference to the Object stored in memory, which we changed via the &lt;em&gt;anotherSquirtle variable&lt;/em&gt;. Thank you JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do we solve this problem?
&lt;/h2&gt;

&lt;p&gt;To avoid the accidental mutation of variables, what we have to do is &lt;strong&gt;create a new instance of our Array/Object&lt;/strong&gt; whenever we want to copy an Array/Object. How do we achieve this?&lt;br&gt;
 &lt;br&gt;
With the spread operator!! :)&lt;/p&gt;
&lt;h1&gt;
  
  
  How does the spread operator work?
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;From the MDN docs: Spread syntax&lt;/strong&gt; allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected&lt;br&gt;
.&lt;br&gt;
To put it simply, the spread operator '…' &lt;strong&gt;spreads&lt;/strong&gt; the items that are contained in an &lt;strong&gt;iterable&lt;/strong&gt; (an iterable is anything that can be looped over, like Strings, Arrays, Sets…) inside a &lt;strong&gt;receiver&lt;/strong&gt; (A receiver is something that receives the spread values). Here are several simple examples with Arrays that will allow you to understand it better:&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, when we use the spread operator on an Array, we obtain &lt;strong&gt;each individual item&lt;/strong&gt; contained in the Array. In all the previous cases, the receiver was a function, the &lt;strong&gt;console.log&lt;/strong&gt; function. Easy enough, right?&lt;/p&gt;

&lt;h1&gt;
  
  
  Cloning Arrays and Objects
&lt;/h1&gt;

&lt;p&gt;Now that we now how the spread operator works, we can make use of it to copy Arrays and Objects immutably. How? By &lt;strong&gt;spreading&lt;/strong&gt; the contents, and then using either the Array or Object literals (&lt;strong&gt;[]&lt;/strong&gt; and &lt;strong&gt;{}&lt;/strong&gt; respectively) to generate a &lt;strong&gt;new instance&lt;/strong&gt; of the Array/Object. Let's take the previous Squirtle example, and fix it, by cloning the *mySquirtle * variable immutably:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;By &lt;strong&gt;destructuring&lt;/strong&gt; the contents of the &lt;em&gt;mySquirtle&lt;/em&gt; variable with the spread operator, and using the &lt;strong&gt;Object literal&lt;/strong&gt;, we are creating a new instance of the Squirtle Object. This way, we &lt;strong&gt;prevent accidental variable mutation&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;To copy an Array, we use exactly the same syntax:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Bear in mind the fact that the spread operator only performs &lt;strong&gt;shallow&lt;/strong&gt; copies. This means that if you have a reference data type stored inside your Array/Object, when you make a copy with the spread operator, the nested Array/Object will contain a &lt;strong&gt;reference to the original&lt;/strong&gt;, and will thus be &lt;strong&gt;mutable&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Converting Array-like Objects to Arrays
&lt;/h1&gt;

&lt;p&gt;Array-like Objects are very similar to Arrays. They usually have numbered elements and a length property. However, they have one &lt;strong&gt;crucial&lt;/strong&gt; difference: Array-like Objects &lt;strong&gt;do not have any of the Array functions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Among the Array-like Objects are the HTML node lists returned by most DOM methods, the arguments variable generated automatically in every JS function and a few others.&lt;/p&gt;

&lt;p&gt;With the same syntax as when cloning arrays, we can use the spread operator to transform Array-like structures to Array, as an alternative to using Array.from. Here's an example, converting a &lt;strong&gt;NodeList&lt;/strong&gt; to an Array: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With this technique, we can transform any Array-like structure to Array, &lt;strong&gt;and thus have access to all the Array functions&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  The spread operator as an argument
&lt;/h1&gt;

&lt;p&gt;Some functions accept a &lt;strong&gt;variable number of parameters&lt;/strong&gt;. A great example of these types of functions are the ones in the Math collection. For our example, let's pick the Math.max() function, which accepts &lt;strong&gt;n numeric parameters&lt;/strong&gt;, and returns the largest one. Imagine we have an Array of numbers, which we want to pass to the Math.max() function. How do we do it? &lt;/p&gt;

&lt;p&gt;We could do something like this (don't hate me for the following code):&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;But, of course, doing this would be suicide. What if we had 20 values? Or 1000? Are we really going to access each value by index? The answer is no. As we already know, the spread operator takes an Array and extracts each individual value. This is just what we're looking for! Therefore, we can do this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Spread operator to the rescue!&lt;/p&gt;

&lt;h1&gt;
  
  
  Adding new elements 
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Adding items to an Array
&lt;/h2&gt;

&lt;p&gt;To add new elements to an array, we first spread the Array's contents, and use the Array literal [] to create a new instance of the Array, containing the original array's contents, plus the values we want to add :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, we can add as many new items as we want. &lt;/p&gt;

&lt;h2&gt;
  
  
  Adding properties to an Object
&lt;/h2&gt;

&lt;p&gt;By using the same syntax as with Arrays, we can easily add new properties when cloning an Object. To switch it up a little, here's a different syntax to add properties to an Object (it can also be used with Arrays):&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, we can declare and initialize new variables directly inside the Object literal, instead of doing so outside. &lt;/p&gt;

&lt;h1&gt;
  
  
  Merging Arrays/Objects
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Arrays
&lt;/h2&gt;

&lt;p&gt;We can merge two arrays, by spreading them and using the Array literal, like in the previous examples. However, instead of simply adding a new element, we're going to add another (spread) array:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It also works if we have an Array of Objects:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Objects
&lt;/h2&gt;

&lt;p&gt;We can merge two (or more) Objects into a single Object, by using the same syntax as before (you may have noticed by now, that the spread operator is used in a very similar same way, both for Arrays and Objects):&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In this first part of the tutorial we have learnt why we should use the spread operator (&lt;strong&gt;immutability!&lt;/strong&gt;), how it works, and several basic uses of said operator. In the second part of the tutorial we will deepen our knowledge this operator with several advanced techniques and use cases. Here's the link to the &lt;a href="https://dev.to/nyagarcia/understanding-the-javascript-spread-operator-from-beginner-to-expert-part-2-24ff"&gt;second part&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you very much for reading :) If you have any doubts or comments don't hesitate to reach out to me, here's a link to my &lt;a href="https://twitter.com/nyablk"&gt;Twitter page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Using variables in Docker-Compose</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Thu, 05 Sep 2019 15:45:15 +0000</pubDate>
      <link>https://dev.to/nyagarcia/using-variables-in-docker-compose-11mf</link>
      <guid>https://dev.to/nyagarcia/using-variables-in-docker-compose-11mf</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Sooner or later, we all have to deal with environment variables in our Compose files. They can become a pain, especially if we don't know how to use them properly. This piece is a recollection of everything I've learned about environment variables, and  aims to make using these variables easy, and above all, &lt;strong&gt;secure&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  First of all, how do we use environment variables?
&lt;/h2&gt;

&lt;p&gt;Docker Compose allows us to pass environment variables in via command line, or to define them in our shell. However, it's recommended to keep these values inside the actual Compose file, and out of the command line. &lt;/p&gt;

&lt;p&gt;Why, you may ask? &lt;/p&gt;

&lt;p&gt;Because this way, we don't have to remember all the environment variables we use every time we deploy our container. By storing them in the Compose file, we are able to &lt;strong&gt;maintain consistency during our builds&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;There are several ways to do this:&lt;/p&gt;

&lt;h1&gt;
  
  
  Using the environment option
&lt;/h1&gt;

&lt;p&gt;Using the Compose environment option allows us to declare environment variables and their values inside our Compose file, like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This is the easiest, quickest way of storing environment variables inside Compose files. However, it has a (in my opinion) &lt;strong&gt;huge&lt;/strong&gt; drawback. It is related to security. Can you guess what it is?&lt;/p&gt;

&lt;p&gt;That's right. &lt;/p&gt;

&lt;p&gt;Storing the values of your environment variables in the Compose file, which, 9 and a half times out of ten will go to straight to source control, is a huge security risk. Luckily, we have an alternative: using an external file to store our environment variables.&lt;/p&gt;

&lt;h1&gt;
  
  
  Using a .env file
&lt;/h1&gt;

&lt;p&gt;The main advantage of using an external file for your environment variables is the fact that you can keep said file &lt;strong&gt;out of your source control&lt;/strong&gt;. After all, no one enjoys their passwords/API keys/other super secret information to be displayed all over the internet, for anyone to see :p &lt;/p&gt;

&lt;p&gt;&lt;em&gt;.env&lt;/em&gt; files are plain text files. You don't need to name them, the extension is the actual name of the file. They must be created at the &lt;strong&gt;root of your project&lt;/strong&gt;, which is also where your &lt;em&gt;docker-compose.yml&lt;/em&gt; file should be. &lt;/p&gt;

&lt;p&gt;We declare and assign variables in our &lt;em&gt;.env&lt;/em&gt; file. You can name the variables however you want, since we will only be accessing their values. Here's my &lt;em&gt;.env&lt;/em&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fn7q50pf3enfpcvfdqzk1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fn7q50pf3enfpcvfdqzk1.png" alt=".env file contents"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also create and populate your &lt;em&gt;.env&lt;/em&gt; file from the command line, by using the linux cat command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fezy6yxzs80lomp764r0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fezy6yxzs80lomp764r0h.png" alt="Creating .env file from command line"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;: Remember not to leave any spaces between the "=" sign and the value assigned to your variable, as they will be added to the string.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, now that we have our variables stored in our &lt;em&gt;.env&lt;/em&gt; file, let's use them in our Compose file. Now's the time to use string interpolation (that's a fancy name for using this notation: &lt;strong&gt;${string}&lt;/strong&gt; ) to assign the &lt;strong&gt;values&lt;/strong&gt; of our &lt;em&gt;.env&lt;/em&gt; variables to the environment variables in the Compose file, like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, we maintain the environment option, and simply assign our external values to the Compose environment variables.&lt;/p&gt;

&lt;p&gt;To check that everything is working properly, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;: You can check which values are assigned to the environment variables by running the following command (in a different terminal):&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Environment variable priority
&lt;/h2&gt;

&lt;p&gt;Something very important we must keep in mind is the priority used by Compose to choose which environment value it uses. What does this mean? &lt;/p&gt;

&lt;p&gt;If we declare the same environment variable in several files, for example, both in the Compose file and in the external &lt;em&gt;.env&lt;/em&gt; file, with different values, Compose will use the value of the variable declared in the Compose file. Why? &lt;/p&gt;

&lt;p&gt;Because depending on where the variable is declared, it is given a higher or lower priority by Compose. Here's the order, ranking from highest priority, to lowest:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Compose file&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Shell environment variables&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Environment file&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Dockerfile&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;5. Variable is not defined&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If for some reason, Compose is picking up and assigning a value you weren't expecting, this is probably the cause. Make sure to have all your variables declared exactly where you want them to be. &lt;/p&gt;
&lt;h1&gt;
  
  
  Using the env-file option
&lt;/h1&gt;

&lt;p&gt;In the previous section, we talked about &lt;em&gt;.env&lt;/em&gt; files, and we said that these files aren't named. However, if for whatever reason we do want our &lt;em&gt;.env&lt;/em&gt; file to have a name, a name like &lt;em&gt;secret-stuff.env&lt;/em&gt;, Compose has a nifty little option named &lt;strong&gt;env-file&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This option allows us to tell Compose which &lt;em&gt;.env&lt;/em&gt; file it has to look for, as opposed to its default behavior, which is to look for an unnamed &lt;em&gt;.env&lt;/em&gt; file. This is how we use the &lt;em&gt;env-file&lt;/em&gt; option:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;As you can see, we added the env_file option, which points to a file named &lt;em&gt;secret-stuff.env&lt;/em&gt; file. All that is left is to rename our previous &lt;em&gt;.env&lt;/em&gt; file to &lt;em&gt;secret-stuff.env&lt;/em&gt; .&lt;/p&gt;

&lt;p&gt;You may have noticed that the environment option is no longer present in our Compose file. This is because using the &lt;em&gt;env-file&lt;/em&gt; option raises a problem (that gave me quite a headache). Allow me to explain:&lt;/p&gt;

&lt;p&gt;To be able to assign values declared in an external &lt;strong&gt;named&lt;/strong&gt; &lt;em&gt;.env&lt;/em&gt; file to Compose variables requires said file to be defined in &lt;strong&gt;the primary Compose service.&lt;/strong&gt; This is related to the order that Compose follows when performing operations. However, we have no primary service, since we are only using a &lt;strong&gt;db service&lt;/strong&gt;. Therefore, if we try to deploy our Compose file, it will complain about our variables not being defined, and substitute them with blank strings.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Comment&lt;/strong&gt;: You don't have to take my word for it, go ahead and try it! Run docker-compose up and see what happens :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, how do we solve this problem? Here's what I figured out:&lt;/p&gt;

&lt;p&gt;If we remove the environment option from the Compose file, upon deployment, Compose will search for the specified &lt;strong&gt;secret-stuff.env file&lt;/strong&gt;, something it doesn't do when the environment option is present. Problem solved!&lt;/p&gt;

&lt;p&gt;Bear in mind though, since we don't have the environment option any longer, we must declare the environment variables directly in the &lt;em&gt;secret-stuff.env&lt;/em&gt; file, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fw39pbiig95mcqa79r3qk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fw39pbiig95mcqa79r3qk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once again, to check everything works properly, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;That's all folks! By now you've (hopefully!) learnt the different ways of securely dealing with environment variables in Compose files. If you have any doubts or would like to discuss anything, don't hesitate to reach out to me. Here's a link to my &lt;a href="https://twitter.com/NyaBlk" rel="noopener noreferrer"&gt;Twitter page&lt;/a&gt;.&lt;br&gt;
 &lt;br&gt;
Thank you for reading :)&lt;/p&gt;

</description>
      <category>docker</category>
      <category>dockercompose</category>
      <category>beginners</category>
      <category>devops</category>
    </item>
    <item>
      <title>PokeAPI REST in NodeJS with Express, Typescript, MongoDB and Docker — Part 3</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Thu, 11 Jul 2019 11:26:03 +0000</pubDate>
      <link>https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-3-494a</link>
      <guid>https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-3-494a</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj0gnabvx489t3xha8hfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj0gnabvx489t3xha8hfa.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Foreword
&lt;/h2&gt;

&lt;p&gt;This is part 3 of a series of posts which will show you how to create a RESTful API in NodeJS. For further reading please check out the following links:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-1-5f8g"&gt;PokeAPI REST in NodeJS with Express, TypeScript, MongoDB and Docker — Part 1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-2-b56"&gt;PokeAPI REST in NodeJS with Express, TypeScript, MongoDB and Docker — Part 2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you prefer to check out the full code, you can find the full PokeApi project &lt;a href="https://github.com/NyaGarcia/pokeAPI" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the previous post we deployed an instance of MongoDB with docker-compose, and connected our application to it. We also created our Mongoose Schema and data Model.&lt;/p&gt;

&lt;p&gt;In this post we are going to implement the rest of the routes that are necessary to create a basic CRUD, as well as their respective database query functions. These functions will make use of the Model we created previously to query our MongoDB database.&lt;/p&gt;

&lt;h2&gt;
  
  
  The coding begins
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Preview
&lt;/h3&gt;

&lt;p&gt;As always, we will begin with a preview of how our directory will look by the end of this post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fcas94h463z0tel1vkcbi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fcas94h463z0tel1vkcbi.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may note that, directory-wise, nothing has changed since the previous post. Content-wise, however, there are quite a few changes.&lt;/p&gt;

&lt;p&gt;Just as a reminder, to run our project we are currently using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To start our dockerized MongoDB instance, use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This said, let us begin.&lt;/p&gt;

&lt;h3&gt;
  
  
  PokeService: Querying the database
&lt;/h3&gt;

&lt;p&gt;It is now time to create our database query functions. For this, as previously mentioned, we are going to make use of our Pokemon Model. Since our goal is to implement the four basic CRUD operations, the first function we are going to implement is one for reading the contents of the db. Open up the pokemon.service.ts file, and type in the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/services/pokemon.service.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&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;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pokemon&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;../models/pokemon.model&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WELCOME_MESSAGE&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;../constants/pokeApi.constants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;welcomeMessage&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//Getting data from the db&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getAllPokemon&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we’ve created a new function, named “getAllPokemon”. It uses the Pokemon Model to interact with MongoDB and find all the Pokemon in the db.&lt;/p&gt;

&lt;p&gt;Since Mongoose’s helper functions are extensively documented in the &lt;a href="https://mongoosejs.com/docs/queries.html" rel="noopener noreferrer"&gt;Mongoose docs&lt;/a&gt;, I don’t think it’s necessary to break them down here. I will, however, comment on the &lt;strong&gt;guard clause&lt;/strong&gt; inside the callback:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Guard clauses are, put simply, a check that exits the function, either with a return statement or an exception, in case of an error or fulfilled condition. They allow us to avoid unnecessary complexity (if else if structures) in our code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is our guard clause:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By reversing the logic, and checking first for errors, we can avoid an “else” statement. If any errors are encountered, we exit the function by sending the error. If we find no errors, then the pokemon result is sent. We will make use of this technique throughout the rest of this post.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing GET routing
&lt;/h3&gt;

&lt;p&gt;We now have our getAllPokemon function in our PokeService. To be able to interact with this function, we must create another GET route. Let’s open our controller, and add a new route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/main.controller.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./services/pokemon.service&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&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;Application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;welcomeMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;//Our new route&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAllPokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;As you can see, the endpoint to access this new route is “/pokemons”. (Excuse the glaring grammatical error, it’s to avoid confusion further on.)&lt;/p&gt;

&lt;p&gt;From here on, I recommend using Postman to test our routes. You can find more information about Postman &lt;a href="https://www.getpostman.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and install it &lt;a href="https://www.getpostman.com/downloads/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If all goes well, you should be obtaining output like the following from Postman:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fd17qwbhch9hioz00277q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fd17qwbhch9hioz00277q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we haven’t introduced any data into our db, we are receiving an empty array. We have now completed our first db query successfully!&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding a new Pokemon
&lt;/h3&gt;

&lt;p&gt;Let’s implement a function to add a new pokemon to our db. Go back to the PokemonService and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/services/pokemon.service.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&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;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pokemon&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;../models/pokemon.model&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WELCOME_MESSAGE&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;../constants/pokeApi.constants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;welcomeMessage&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getAllPokemon&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&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="c1"&gt;//Adding a new pokemon&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;addNewPokemon&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;Request&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;Response&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;newPokemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Pokemon&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;newPokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To explain briefly, we create a Mongoose Document (newPokemon) from the request body, and we save it into the db.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Just as a reminder, Mongoose Documents are instances of Mongoose Models&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s create the route to interact with our function. In our controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/main.controller.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./services/pokemon.service&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&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;Application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;welcomeMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAllPokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;//Our new route&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addNewPokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Note that our new route is accessed through a POST request. Head over to Postman, and let’s add a new Pokemon to our db:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpygqsfzu49c48mtlmycu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpygqsfzu49c48mtlmycu.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If everything goes well, you should be receiving the Pokemon you just added as output. To double-check, we can make use of our GET route:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5hdr2y0zl6sbr71kes5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5hdr2y0zl6sbr71kes5t.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we now have a Squirtle in our db. Don’t worry about the “_id” and “__v” fields. They are generated automatically by Mongooose, and we will deal with them later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deleting a Pokemon
&lt;/h3&gt;

&lt;p&gt;To implement a function to delete a Pokemon, open up the PokeService, and type the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/services/pokemon.service.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&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;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pokemon&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;../models/pokemon.model&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WELCOME_MESSAGE&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;../constants/pokeApi.constants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;welcomeMessage&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getAllPokemon&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&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="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;addNewPokemon&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;Request&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;Response&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;newPokemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Pokemon&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;newPokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&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="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;deletePokemon&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;Request&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;Response&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;pokemonID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdAndDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemonID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deleted&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deleted successfully&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;Pokemon not found :(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We obtain the ID of the Pokemon to delete from the request parameters, this is to say, the parameters in the query string in the URL to which we make the GET request. It would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localhost:9001/pokemon/123pokemonId
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mongoose has a very useful findByIdAndDelete helper function, which allows us to delete a document (in our case, a Pokemon) by said document’s “_id” field. This function is shorthand for findOneAndDelete({_id: pokemonId}).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you have the need of deleting a document by any field other than _id, you may use the findOneAndDelete function aforementioned. More information about it &lt;a href="https://mongoosejs.com/docs/api.html#model_Model.findOneAndDelete" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I’d now like to draw your attention to the following line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deleted&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Deleted successfully&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;Pokemon not found :(&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;Here we have a ternary expression, which assigns a different value to the “message” variable, depending on the value of the second parameter (“deleted”) passed to the callback.&lt;/p&gt;

&lt;p&gt;This is because Mongoose’s &lt;em&gt;findByIdAndDelete&lt;/em&gt; function finds a matching document, deletes it, and then passes the found document (if any) to the callback. Therefore, if Mongoose finds a document, it will be deleted, in which case we return the “Deleted successfully” message. If not, we return the “Pokemon not found” message.&lt;/p&gt;

&lt;p&gt;Once we have our function ready, let’s create our route. In our controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/main.controller.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./services/pokemon.service&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&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;Application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;welcomeMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAllPokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addNewPokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;//Our new route&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemon/:id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deletePokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the route we have just created, we are indicating that we will receive a request parameter in the URL, a parameter we have named “id”. This is the parameter we previously used in the Pokemon Service to obtain the id.&lt;/p&gt;

&lt;p&gt;Note that this route is accessed through a DELETE request.&lt;/p&gt;

&lt;p&gt;Once again, we open Postman, and test our new route by deleting the Squirtle (or whichever Pokemon you chose) we added to our db earlier:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5fwlgihbwzu2q47doyzr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5fwlgihbwzu2q47doyzr.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we receive the “Deleted successfully” message. If no Pokemon with the id we specified were to be found, we would receive the “Pokemon not found” message instead.&lt;/p&gt;

&lt;p&gt;We can double check that our squirtle has been deleted correctly by obtaining all Pokemon from the db:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdg6rg2w0s19q38ffk3fw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdg6rg2w0s19q38ffk3fw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Empty array = no Pokemon = Squirtle has been deleted successfully.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating a Pokemon
&lt;/h3&gt;

&lt;p&gt;In our Pokemon Service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/services/pokemon.service.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&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;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pokemon&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;../models/pokemon.model&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WELCOME_MESSAGE&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;../constants/pokeApi.constants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;welcomeMessage&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getAllPokemon&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&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="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;addNewPokemon&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;Request&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;Response&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;newPokemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Pokemon&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;newPokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MongooseDocument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&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="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;deletePokemon&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;Request&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;Response&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;pokemonID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdAndDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemonID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deleted&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deleted successfully&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;Pokemon not found :(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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="c1"&gt;//Updating a pokemon&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;updatePokemon&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;Request&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;Response&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;pokemonId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdAndUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;pokemonId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;
          &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Updated successfully&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;Pokemon not found :(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Note that we have used precisely the same technique as in the deletePokemon function. Obtaining the ID as a request parameter, using Mongoose’s findByIdAndUpdate helper function, and returning a message according to the value of the second callback parameter.&lt;/p&gt;

&lt;p&gt;In our controller, let’s create the final route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/main.controller.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./services/pokemon.service&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&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;Application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;welcomeMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAllPokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addNewPokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;//Chaining our route&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/pokemon/: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="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deletePokemon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;updatePokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Considering that both the delete and put routes have exactly the same endpoint, we can chain them as shown above. This way, we don’t have to declare the same route twice, one for each verb.&lt;/p&gt;

&lt;p&gt;Let’s head over to Postman, and test our final route. Don’t forget to add a new Pokemon, or you won’t have any data to update! I chose to add another Squirtle, which I will now update:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flzev91n0fpykb2r25cf4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flzev91n0fpykb2r25cf4.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s obtain all our Pokemon to check on our Squirtle:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzq23mv37sspwryd6lv0j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzq23mv37sspwryd6lv0j.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! Your Squirtle has evolved into a Wartortle, and you have successfully implemented all the basic CRUD functions and their respective routes.&lt;/p&gt;

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

&lt;p&gt;In this post we’ve learnt how to query MongoDB by means of Mongoose Models and Documents. We have also implemented routes to access our CRUD functions.&lt;/p&gt;

&lt;p&gt;If you’d like to see the full code for this post, you can do so &lt;a href="https://github.com/NyaGarcia/pokeAPI/tree/part3" rel="noopener noreferrer"&gt;here&lt;/a&gt; (branch “part3” of the pokeAPI project).&lt;/p&gt;

&lt;p&gt;Thank you so much for reading, I hope you both enjoyed and found this post useful. Feel free to share with your friends and/or colleagues, and if you have any comments, don’t hesitate to reach out to me! Here’s a link to my &lt;a href="https://twitter.com/NyaBlk" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; page.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>docker</category>
      <category>express</category>
    </item>
    <item>
      <title>PokeAPI REST in NodeJS with Express, Typescript, MongoDB and Docker — Part 2</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Thu, 11 Jul 2019 11:25:06 +0000</pubDate>
      <link>https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-2-b56</link>
      <guid>https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-2-b56</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj0gnabvx489t3xha8hfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj0gnabvx489t3xha8hfa.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Foreword
&lt;/h2&gt;

&lt;p&gt;This is part 2 of a series of posts which will show you how to create a RESTful API in NodeJS. For further reading please check out the following links:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-1-5f8g"&gt;PokeAPI REST in NodeJS with Express, TypeScript, MongoDB and Docker — Part 1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-3-494a"&gt;PokeAPI REST in NodeJS with Express, TypeScript, MongoDB and Docker — Part 3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you prefer to check out the full code, you can find the full PokeApi project &lt;a href="https://github.com/NyaGarcia/pokeAPI" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the previous post we set up our server, and implemented our first GET route, which returned a lovely welcome message. Since our goal is to perform basic CRUD operations on our Pokemon data, we need to have a database to store our information.&lt;/p&gt;

&lt;p&gt;In this post we are going to create and deploy a docker container for our MongoDB database. We are also going to define our Pokemon data model using Mongoose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Preview
&lt;/h3&gt;

&lt;p&gt;Once again, before we start, a little preview of how our directory tree will look by the end of this post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fltafch175na4b9axgjb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fltafch175na4b9axgjb7.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just as a reminder, to run our project we are currently using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This said, let us begin.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating our docker-compose file
&lt;/h3&gt;

&lt;p&gt;The first thing we are going to do is create a docker-compose.yml file, on the same level of our “src” directory, which is to say, outside the “src” directory. Once this is done, copy and paste the following code into the newly created file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let’s explain briefly what all of these configuration options mean:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;version:&lt;/strong&gt; Specifies the docker-compose version we are going to use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;services:&lt;/strong&gt; We can specify a list of services which will be deployed with our container. In our case, we want a database, which is why we use the following attribute:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;db:&lt;/strong&gt; We indicate that we are going to deploy a database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;container_name:&lt;/strong&gt; This is optional, it allows us to specify a custom container name. If we omit this option, a default container name will be generated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;image:&lt;/strong&gt; Specifies the image that the container will be built from. In our case, the latest MongoDB image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;restart:&lt;/strong&gt; Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;volumes:&lt;/strong&gt; This is a very interesting option. It allows us to have data persistence. What does this mean? All of our data is going to be stored in a docker container. However, docker containers can be stopped, restarted etc. In such cases, what happens to our data? Does it disappear? The answer is, it won’t disappear if we use the volumes option. We can specify a directory in our local machine where our data will be stored. In our case, this directory is named “pokeData”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;environment:&lt;/strong&gt; We can specify environment variables. In our case, we are creating a database named “Pokemon” when the container starts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ports:&lt;/strong&gt; Specifies the ports that will be exposed (Host port: Container port). In our case, we are mapping our local port 27017 to the container port 27017 (27017 is the default port for MongoDB).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: For more information about docker-compose files, you can check out this &lt;a href="https://docs.docker.com/compose/compose-file/" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we have our docker-compose file ready, let’s start the container. Fire up your terminal, and type this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’ve done everything correctly, you should be seeing output similar to this on your terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F72rd5oby4i7o1k0mx3n7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F72rd5oby4i7o1k0mx3n7.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should also see that a new directory named &lt;strong&gt;pokeData&lt;/strong&gt; has appeared in your directory tree. This is the directory we specified earlier in our docker-compose file, by using the “volumes” attribute. Our pokeData directory will store all our database data (once we insert some), and keep it safe and sound.&lt;/p&gt;

&lt;p&gt;Isn’t docker awesome and easy to use? A simple, intuitive, extensively documented configuration file and one command are all we need to have our database instance up and running. Beautiful.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Troubleshooting: If you’re getting the following output after executing the docker-compose up command:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ERROR: Couldn’t connect to Docker daemon at http+docker://localhost — is it running?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It means that your docker daemon isn’t running. Execute this command to start the docker daemon:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;And try the docker-compose up command again. The error should be gone.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Connecting our app to our dockerized MongoDB database
&lt;/h3&gt;

&lt;p&gt;We have our database container deployed and running, so we now need to connect our application to it. Open the app.ts file, and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/app.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&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;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&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;./main.controller&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&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;body-parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cors&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;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&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;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&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;Application&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;pokeController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setConfig&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setMongoConfig&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;setConfig&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;50mb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;50mb&lt;/span&gt;&lt;span class="dl"&gt;'&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;//Connecting to our MongoDB database&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;setMongoConfig&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongodb://localhost:27017/Pokemon&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;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may have noticed that once again, we are hard coding a variable: the mongoose connection string. To avoid this, let’s open our constants file, and store it there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/constants/pokeAPI.constants.ts&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9001&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome to pokeAPI REST by Nya ^^&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MONGO_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongodb://localhost:27017/Pokemon&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;Back in our app.ts, we can now change the hard coded String for our newly defined constant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/app.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&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;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&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;./main.controller&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//importing our MONGO_URL constant&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MONGO_URL&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;./constants/pokeApi.constants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&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;body-parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cors&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;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&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;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&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;Application&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;pokeController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setConfig&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setMongoConfig&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;setConfig&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;50mb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;50mb&lt;/span&gt;&lt;span class="dl"&gt;'&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;setMongoConfig&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//using our constant instead of the hard coded String&lt;/span&gt;
    &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MONGO_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we’ve done everything correctly, we should now be seeing the following output in our terminal where we ran our “docker-compose up” command (if, for any reason, you stopped docker-compose previously, run the command again):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxw1t2s5jdsbcqzuwhqlt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxw1t2s5jdsbcqzuwhqlt.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, our docker container has accepted the connection we made from our application. So far, so good.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating our data model
&lt;/h3&gt;

&lt;p&gt;Now that we are connected to our database, we need a way to interact with it. To achieve this, we are going to use Mongoose, which provides us with several data modelling tools, such as Schemas and Models. Mongoose makes interacting with MongoDB exceedingly easy and simple.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;From the mongoose docs: &lt;em&gt;Models are fancy constructors compiled from Schema definitions. An instance of a model is called a document. Models are responsible for creating and reading documents from the underlying MongoDB database.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To store our data models, we are going to create a models directory in src, which will contain a file named “pokemon.model.ts”. Inside this file, we are going to import Mongoose and create our data model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/models/pokemon.model.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PokemonSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;photo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we’ve created our pokemon Schema, we need to create a Mongoose model. To do this, we will part from our newly created Schema. Therefore, in the same file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/models/pokemon.model.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PokemonSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;photo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;//Creating our model&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Pokemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pokemon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PokemonSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: I am fully aware of the fact that we are defining both our schema and model in the same file; a file named “pokemon.&lt;strong&gt;model&lt;/strong&gt;”. Despite being a control freak, I refuse to create two separate files for 10 lines of code. If you are even more obsessed than I, you may, of course, create a separate file for your schema. Don’t count me in though :p&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With our Pokemon model just created, it is now time to import it in the PokeService:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/services/pokemon.service.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../constants/pokeAPI.constants&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//importing our model&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pokemon&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../models/pokemon.model&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;welcomeMessage&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Pokemon model will later be used to query our MongoDB database, once we create the CRUD routes and their respective db query functions. This, however, we will leave for the following post.&lt;/p&gt;

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

&lt;p&gt;In this post we’ve learnt how to deploy an instance of MongoDB with docker-compose, and how to connect our application to it. We’ve also used Mongoose to create both a Schema and a Model for our database.&lt;/p&gt;

&lt;p&gt;If you’d like to see the full code for this post, you can do so &lt;a href="https://github.com/NyaGarcia/pokeAPI/tree/part2" rel="noopener noreferrer"&gt;here&lt;/a&gt; (branch “part2” of the pokeAPI project).&lt;/p&gt;

&lt;p&gt;Thank you so much for reading, I hope you both enjoyed and found this post useful. Feel free to share with your friends and/or colleagues, and if you have any comments, don’t hesitate to reach out to me! Here’s a link to my &lt;a href="https://twitter.com/NyaBlk" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; page.&lt;/p&gt;

&lt;p&gt;In the following post we will be implementing the rest of the routes that are necessary to create a basic CRUD, as well as their respective database query functions.&lt;/p&gt;

&lt;p&gt;Here is the link to the next post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-3-494a"&gt;PokeAPI REST in NodeJS with Express, TypeScript, MongoDB and Docker — Part 3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>express</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>PokeAPI REST in NodeJS with Express, Typescript, MongoDB and Docker — Part 1</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Thu, 11 Jul 2019 11:24:00 +0000</pubDate>
      <link>https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-1-5f8g</link>
      <guid>https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-1-5f8g</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj0gnabvx489t3xha8hfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj0gnabvx489t3xha8hfa.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Foreword
&lt;/h2&gt;

&lt;p&gt;This is part 1 of a series of posts which will show you how to create a RESTful API in NodeJS. For further reading please check out the following links:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-2-b56"&gt;PokeAPI REST in NodeJS with Express, TypeScript, MongoDB and Docker — Part 2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-3-494a"&gt;PokeAPI REST in NodeJS with Express, TypeScript, MongoDB and Docker — Part 3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you prefer to check out the full code, you can find the full PokeApi project &lt;a href="https://github.com/NyaGarcia/pokeAPI" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In these series of posts we will be learning how to create our own RESTful API in NodeJS, using an excellent web framework named &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express&lt;/a&gt;. However, before we start, a little theory:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Representational_state_transfer" rel="noopener noreferrer"&gt;REST&lt;/a&gt; = Representational State Transfer. An awesome style of software architecture designed by Roy Fielding for his doctoral dissertation. REST allows us to design loosely coupled applications by using the HTTP protocol.&lt;/p&gt;

&lt;p&gt;HTTP provides us with the following verbs, or methods: GET, POST, PUT and DELETE, which correspond to Read, Create, Update and Delete (CRUD operations) respectively. There are a few other verbs, but they aren’t used as frequently. We will be using these verbs to make requests, which will perform various operations on our data.&lt;/p&gt;

&lt;p&gt;Since (in my humble opinion) &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt; is the best thing since sliced bread, this is the language that we will be using. For those unfamiliar with it, TypeScript is a typed superset of JavaScript, that we compile to plain ol’ JavaScript, and, among many other things, allows us to add types to JavaScript (TS FTW).&lt;/p&gt;

&lt;p&gt;Since we need a database to store our data, we will be using a dockerized instance of MongoDB, together with &lt;a href="https://mongoosejs.com/" rel="noopener noreferrer"&gt;Mongoose&lt;/a&gt;, an ODM which makes interacting with MongoDB that much easier.&lt;/p&gt;

&lt;p&gt;Now that we know what we are going to be working on, and how, let us get down and start coding.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Preview
&lt;/h3&gt;

&lt;p&gt;Before we start, I’d like to show you a preview of how our directory tree will look by the end of this post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fe2j2sqopbbc74cy5xj4v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fe2j2sqopbbc74cy5xj4v.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In case anyone is curious about how I generated the directory tree image, I used the linux “tree” command, and snapped a screenshot of my terminal. Pretty simple.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing NodeJS
&lt;/h3&gt;

&lt;p&gt;Since we will be using NodeJS for our project, the first thing to do is make sure it is installed on our machine.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: Open up your terminal and type this command to check if Node is installed: &lt;strong&gt;node -v&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If not, you can install it &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Docker and Docker-compose
&lt;/h3&gt;

&lt;p&gt;Docker is a fantastic tool that allows us to create, deploy and run applications (or pretty much anything we want) by using containers. We can deploy a MongoDB database (or any other database, you name it) in a few minutes, with a couple of simple commands.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: Run this command to see if you have docker on your machine: &lt;strong&gt;docker -v&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If not, you can install it &lt;a href="https://docs.docker.com/install/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As I’ve just mentioned, Docker is awesome. However, and this is purely personal taste, I prefer to deploy my containers using Docker Compose. This is a tool offered by Docker, which allows us to create a .yml configuration file, where we can specify all the details of our container, and deploy said container with a just one simple command.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: Run this command to check if you have docker-compose: &lt;strong&gt;docker-compose -v&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If not, you can install it &lt;a href="https://docs.docker.com/compose/install/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let the coding Begin
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Getting started
&lt;/h3&gt;

&lt;p&gt;With all our pre-requisites out of the way, we can now get down and start coding for real. Let us begin:&lt;/p&gt;

&lt;p&gt;The first step is to create the file where our project is going to live. I’m going to name our project file “pokeApi”. Open up your terminal and type this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir pokeApi
cd pokeApi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once inside our project file, we want to create our package.json file. Again, type the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;After running this command, we will be asked a series of questions, and upon answering them, our package.json file will be created.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: If you don’t feel like answering npm’s questions, use this command instead: &lt;strong&gt;npm init -y&lt;/strong&gt; &lt;br&gt;
    You can always go back to the package.json file and edit it later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Installing dependencies
&lt;/h3&gt;

&lt;p&gt;To be able to use express, mongoose, TypeScript etc. we must install a few dependencies. To do so, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i body-parser cors express mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need to install several dependencies needed only for development. Type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D @types/body-parser @types/cors @types/express @types/mongoose @types/node nodemon ts-node typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Tip: By adding -D or --save-dev to npm i command, the dependencies installed will be listed under “devDependencies” in the package.json file&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Adding npm scripts
&lt;/h3&gt;

&lt;p&gt;To be able to run our projects, we must create the following script in our package.json file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
"start": "nodemon"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring nodemon
&lt;/h3&gt;

&lt;p&gt;Nodemon is a neat tool for developing nodeJS applications. It automatically restarts the application whenever it detects changes in the code (basically, whenever you save).&lt;/p&gt;

&lt;p&gt;Create a file named nodemon.json, and type in the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
"watch": ["src"],
"ext": "ts",
"exec": "ts-node ./src/server.ts"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells nodemon which files to watch and execute.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring TypeScript
&lt;/h3&gt;

&lt;p&gt;To generate our tsconfig.json file, run the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Note that this file contains many, many configuration options. You may, of course, configure TypeScript according to your preferences. If not, here’s the configuration I use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"resolveJsonModule": true,
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},                         
"exclude": ["node_modules"], 
"include": ["src/**/*.ts"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a .gitignore file
&lt;/h3&gt;

&lt;p&gt;In this file we can list all the files/directories that we want git to ignore, this meaning that when we add and commit the changes made to our project, these files while remain “invisible” to git.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: It is very, very, very important to add node_modules to our .gitignore. We most definitely don’t want this huge file pushed to our repository!!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To create our .gitignore file, type this (while in the root of the directory, of course):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch .gitignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, add the following lines inside the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//.gitignore

node_modules
package-lock.json
dist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Enough with the config, where’s the real coding?
&lt;/h2&gt;

&lt;p&gt;It begins now, I swear. Let’s go:&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up our server
&lt;/h3&gt;

&lt;p&gt;The first thing we are going to do is create our basic directory structure. We are going to create a directory named src, which will contain all of our project files (aside from config):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir src
cd src 
mkdir constants
touch server.ts
touch app.ts 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us open the app.ts file we just created, which will contain our basic express configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/app.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&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;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&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;body-parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cors&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;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&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;Application&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setConfig&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;setConfig&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//Allows us to receive requests with data in json format&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;50mb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;

    &lt;span class="c1"&gt;//Allows us to receive requests with data in x-www-form-urlencoded format&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;50mb&lt;/span&gt;&lt;span class="dl"&gt;'&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;//Enables cors   &lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will go over Express’ config quickly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Body parser allows us to receive requests with data in different formats, such as json, or x-www-form-urlencoded.&lt;/li&gt;
&lt;li&gt;CORS (Cross-Origin Resource Sharing) uses additional HTTP headers let our browser know that is has to allow a web application running at one domain to access resources from a server at a different origin.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once this is done, we’re going to create a file to store our app’s constants. Why? Because this way we only have to declare each constant once. Whenever we need to make use of it, we just have to import it.&lt;/p&gt;

&lt;p&gt;Furthermore, if the value of our constant changes (yes, even though it’s a constant, sometimes we need to change its value), it will change everywhere in our project, since it’s only declared in one place. All of this said, let’s create our constants file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd constants
touch pokeApi.constants.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first constant we are going to declare is our PORT, which will store the number of the port we are going to open for our server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/constants/pokeApi.constants.ts&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9001&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, head over to our server.ts file, where we will set up our server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/server.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./constants/pokeApi.constants.ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Listening on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Tip: If anyone is unfamiliar with the syntax I’m using in the console.log, it’s a technique named template literals, where you type everything inside grave quotes (also known as backticks), and use interpolation (&lt;strong&gt;${}&lt;/strong&gt;) to embed variables. More about this technique &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note that we are importing both the app we created previously, and our PORT constant.&lt;/p&gt;

&lt;p&gt;And with just these three lil’ files, we’ve created our very own server! Fire up your terminal and execute the npm start script we created previously. You can do this by typing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Tip: Since we are using nodemon to watch our project files, we only need to execute the previous command once. Every time we save our changes, nodemon will automatically restart our app for us.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After executing the command, you should be seeing the “Listening on port 9001” message on your terminal. Yeah! We now have our server up and running.&lt;/p&gt;

&lt;p&gt;You can also head over to your favorite browser to check it out. Type this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localhost:9001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should be seeing a message similar to this: “Cannot GET /”. I know, not very exciting… But if you’re seeing this message, it works! If not, go back and re-check your code to make sure nothing is missing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating our first GET route
&lt;/h3&gt;

&lt;p&gt;Since we now have our server up and running, we are going to create the first GET route and display a nice welcome message. After all, “Cannot GET /” isn’t very welcoming…&lt;/p&gt;

&lt;p&gt;To do this, create a file named “main.controller.ts”, and type in the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/main.controller.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&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;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&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;Application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you may have noted, our Controller is going to act as a router; it is where we will define all of our routes for this project. Each route will execute a different action, which will be defined in a service file.&lt;/p&gt;

&lt;p&gt;Why are we going to separate our actions in a different file? Say you defined all of the functions that interact with the database in your controller. For this project, we are going to use MongoDB as our database. Now imagine you want to change the database, and use MySQL instead. You would have to go back to your controller, and change &lt;strong&gt;everything&lt;/strong&gt;, to adapt it to a MySQL database. If, however, you’ve declared all of your database functions in a different file, you wouldn’t need to change the controller at all. You could just swap the file with MongoDB query functions for one with MySQL query functions. By using a service, we keep our code &lt;strong&gt;loosely coupled&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, we will now create a file named “pokeApi.service.ts”, in a directory named “services”, and type in the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/services/pokemon.service.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;welcomeMessage&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome to pokeAPI REST by Nya ^^&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;A very simple function, which returns our cute welcome message. Now, head over to our controller, and import the service we have just created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/main.controller.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&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;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PokeService&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;./services/pokemon.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&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;Application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;welcomeMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, our main GET route will call the welcomeMessage function we have just created in our pokemon service.&lt;/p&gt;

&lt;p&gt;So far, so good. It’s time to import our Controller into our app.ts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/app.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&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;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//importing our controller&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&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;./main.controller&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&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;body-parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cors&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;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&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;Application&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;//declaring our controller&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;pokeController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setConfig&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;//Creating and assigning a new instance of our controller&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pokeController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;setConfig&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;50mb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;50mb&lt;/span&gt;&lt;span class="dl"&gt;'&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we’re done! Head over to your browser, and if you’ve done everything correctly, you should be seeing your welcome message displayed, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F8xrqnpsm2klda6f6yd2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F8xrqnpsm2klda6f6yd2t.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  A little bit of refactoring
&lt;/h3&gt;

&lt;p&gt;Remember we created a file which would store all of our constants? You may have realized that in our welcomeMessage function (in our pokemon service), we were returning a String containing the message, which we “hard coded” into our service. Not very neat, right? What if we want to change the message? I’d have to modify the service. Not good.&lt;/p&gt;

&lt;p&gt;Therefore, we are going to take the message, and declare it in our constants file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/constants/pokeApi.constants.ts&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9001&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome to pokeAPI REST by Nya ^^&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;One we’ve done this, we are going to import the constants file in our service, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//src/services/pokemon.service.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../constants/pokeApi.constants&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PokeService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;welcomeMessage&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;Request&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;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WELCOME_MESSAGE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If you go back to your browser, you should still be seeing the welcome message.&lt;/p&gt;

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

&lt;p&gt;In this post we’ve covered everything from setting up our project’s configuration, to defining our first route and successfully making our first GET request.&lt;/p&gt;

&lt;p&gt;If you want to check out the full code for this post, you can find it &lt;a href="https://github.com/NyaGarcia/pokeAPI/tree/part1" rel="noopener noreferrer"&gt;here&lt;/a&gt; (branch “part1” of the pokeAPI project).&lt;/p&gt;

&lt;p&gt;Thank you so much for reading, I hope you both enjoyed and found this post useful. Feel free to share with your friends and/or colleagues, and if you have any comments, don’t hesitate to reach out to me! Here’s a link to my &lt;a href="https://twitter.com/NyaBlk" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; page.&lt;/p&gt;

&lt;p&gt;In the following post we will be connecting our application to a dockerized instance of MongoDB, deployed with docker-compose. We will also be using Mongoose to create a data Model and Schema.&lt;/p&gt;

&lt;p&gt;Last, but not least, here is the link to the following post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nyagarcia/pokeapi-rest-in-nodejs-with-express-typescript-mongodb-and-docker-part-2-b56"&gt;PokeAPI REST in NodeJS with Express, TypeScript, MongoDB and Docker — Part 2&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>docker</category>
      <category>express</category>
    </item>
    <item>
      <title>Array.map() + async/await</title>
      <dc:creator>Nya</dc:creator>
      <pubDate>Thu, 02 May 2019 15:06:25 +0000</pubDate>
      <link>https://dev.to/nyagarcia/array-map-async-await-2cif</link>
      <guid>https://dev.to/nyagarcia/array-map-async-await-2cif</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhp6u8vwbnmxa949s43r3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhp6u8vwbnmxa949s43r3.png" alt="Meow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;MDN Web Docs: The map() method creates a new array with the results of calling a provided function on every element in the calling array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Today I found myself in a situation where I had to use an asynchronous function inside of an Array.map. Needless to say, Array.map is a synchronous function. Therefore, I had a slight problem in my hands.&lt;/p&gt;

&lt;p&gt;I came across this problem while refactoring my code to get rid of a nasty “for..of”. Basically, I had an Array of objects upon which I had to make several calculations. One of these calculations involved making a call to an API which, of course, is asynchronous. Before my refactoring, I had no problem with using async/await inside my for..of loop. However, after making the switch to Array.map, I realized it wouldn’t be as simple.&lt;/p&gt;

&lt;p&gt;Here’s what I did:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
Asynchronous Array.map()
 



&lt;p&gt;As you can see, my getDistance function simulates a call to an API, and returns a Promise.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The return value of async functions is always a Promise&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that our Array.map function will return an Array of unresolved promises. Therefore, outside of the Array.map, we have to wait for all the promises to be resolved before we can make use of the resulting Array. This is why we use Promise.all().&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you aren’t comfortable with async/await syntax, you can always resort to good ol’ “.then”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can check out and play with my solution in the following CodePen:&lt;/p&gt;

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

&lt;p&gt;Pretty simple, right? It took me a while to figure it out…&lt;br&gt;
Which is why I decided to share my solution, in case someone else encounters this issue. I hope it helps! Thank you for reading, feel free to share or leave a comment :)&lt;/p&gt;

&lt;p&gt;P.S. You can check out my Github and Twitter pages if you like...&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>async</category>
      <category>typescript</category>
      <category>map</category>
    </item>
  </channel>
</rss>
