<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Parables</title>
    <description>The latest articles on Forem by Parables (@parables).</description>
    <link>https://forem.com/parables</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%2F264886%2Ff7e77011-3d3f-489c-9a33-acd547c48174.jpeg</url>
      <title>Forem: Parables</title>
      <link>https://forem.com/parables</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/parables"/>
    <language>en</language>
    <item>
      <title>What's new in `svelte-kit, 1.0.0-next.445`: (group) layout</title>
      <dc:creator>Parables</dc:creator>
      <pubDate>Mon, 29 Aug 2022 16:58:00 +0000</pubDate>
      <link>https://forem.com/parables/whats-new-in-svelte-kit-100-next445-group-layout-1ld5</link>
      <guid>https://forem.com/parables/whats-new-in-svelte-kit-100-next445-group-layout-1ld5</guid>
      <description>&lt;p&gt;SvelteKit is moving at the speed of sound to reach v1.0.0 and hence the team is juggling between pressure from the community to publish features proposed in the RFC while early adopters rant about how the team is not giving them enough time to soak in the new changes.&lt;/p&gt;

&lt;p&gt;However, these changes are aimed at making the framework more expressive and simple and that's what we all want and love about Svelte&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[Tip] Please follow the &lt;a href="https://github.com/sveltejs/kit/discussions/"&gt;Discussions&lt;/a&gt; and read the &lt;a href="https://github.com/sveltejs/kit/blob/05e8240150da446f6cd3c1f0feb4447408b2b43b/packages/kit/CHANGELOG.md"&gt;CHANGELOG.md&lt;/a&gt; to stay updated on the breaking changes&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What's Changed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;(group) layout replaces named-layout &lt;a href="https://github.com/sveltejs/kit/discussions/6124"&gt;#6124&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/sveltejs/kit/discussions/6124"&gt;#6124&lt;/a&gt; in particular makes it very easy to avoid hacking named-layouts to break out of the inherited layout that was released in &lt;code&gt;svelte-kit, 1.0.0-next.306&lt;/code&gt; &lt;a href="https://github.com/sveltejs/kit/pull/4388"&gt;#4388&lt;/a&gt;. See &lt;a href="https://github.com/sveltejs/kit/blob/05e8240150da446f6cd3c1f0feb4447408b2b43b/packages/kit/CHANGELOG.md#100-next306"&gt;CHANGELOG.md&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Problem with Named Layouts
&lt;/h2&gt;

&lt;p&gt;Named Layout allowed you to specify exactly which layout your &lt;code&gt;+page.svelte&lt;/code&gt; and &lt;code&gt;+layout.svelte&lt;/code&gt; files should inherit by appending &lt;code&gt;@name&lt;/code&gt; where &lt;code&gt;@name&lt;/code&gt; is the part of the layout file name between the &lt;code&gt;+layout-&lt;/code&gt; and &lt;code&gt;.svelte&lt;/code&gt; extension.&lt;/p&gt;

&lt;p&gt;Example: a &lt;code&gt;+page.svelte&lt;/code&gt; or &lt;code&gt;+layout.svelte&lt;/code&gt; file that wishes to inherit from a layout with the name &lt;code&gt;+layout-settings&lt;/code&gt;, would have to be renamed as &lt;code&gt;+page@settings.svelte&lt;/code&gt; or &lt;code&gt;+layout@settings.svelte&lt;/code&gt; respectively&lt;/p&gt;

&lt;p&gt;Using folder structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/routes/
├ courses/
│ ├ +page.svelte
│ └ +page.ts
├ lecturers/
│ ├ +page.svelte
│ └ +page.ts
├ schedules/
│ ├ +page.svelte
│ └ +page.ts
├ settings/
│ ├ +page@settings.svelte
│ └ +page.ts
├ login/
│ ├ +page@auth.svelte
│ └ +page.ts
├ register/
│ ├ +page@auth.svelte
│ └ +page.ts
├ [deeplink=action]/
│ ├ +page.svelte
│ ├ +page.ts
│ └ +layout@root.svelte
├ +layout.svelte
├ +layout-auth.svelte
├ +layout-settings@default.svelte
└ +layout-root.svelte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will give us the following routes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/
/courses
/lecturers
/schedules
/settings
/login
/register
/reset-password?email=parables95@gmail.com - deeplink
/verify-email?email=parables95@gmail.com - deeplink
/success?onboarding=done$redirectTo=home - deeplink
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the folder structure above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;/courses&lt;/code&gt;, &lt;code&gt;/lecturers&lt;/code&gt; and &lt;code&gt;/schedules&lt;/code&gt; routes will inherit from the layout from &lt;code&gt;src/routes/+layout.svelte&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;/settings&lt;/code&gt; route will inherit from both the default parent layout &lt;code&gt;src/routes/+layout.svelte&lt;/code&gt; and
the &lt;code&gt;src/routes/+layout-settings.svelte&lt;/code&gt; layout&lt;/li&gt;
&lt;li&gt;likewise, the &lt;code&gt;/login&lt;/code&gt; and &lt;code&gt;/register&lt;/code&gt; routes will break out from the default parent layout &lt;code&gt;src/routes/+layout.svelte&lt;/code&gt; and will rather inherit from the &lt;code&gt;src/routes/+layout-auth.svelte&lt;/code&gt; layout&lt;/li&gt;
&lt;li&gt;the deeplink routes will inherit from the immediate layout file &lt;code&gt;src/routes/[deeplink=action]/+layout@root.svelte&lt;/code&gt; which in turn will also inherit from the named layout &lt;code&gt;src/routes/layout-root.svelte&lt;/code&gt; since it explicitly specifies the &lt;code&gt;@root&lt;/code&gt; in the layout name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This layout hacking method will it makes it easy to infer the routes using the folder structure makes it a bit harder to understand where routes belongs to.&lt;/p&gt;

&lt;p&gt;For example I would want to group &lt;code&gt;/courses&lt;/code&gt;, &lt;code&gt;/lecturers&lt;/code&gt; and &lt;code&gt;/schedules&lt;/code&gt; under dashboard but if I create a new directory &lt;code&gt;src/routes/dashboard&lt;/code&gt; and the routes into the dashboard directory, my routes becomes &lt;code&gt;/dashboard/courses&lt;/code&gt;, &lt;code&gt;/dashboard/lecturers&lt;/code&gt; and &lt;code&gt;/dashboard/schedules&lt;/code&gt; and that's is not what I want. I want to keep them on the index level and not nest them in the &lt;code&gt;/dashboard&lt;/code&gt; level.&lt;/p&gt;

&lt;h2&gt;
  
  
  (group) layouts to the rescue
&lt;/h2&gt;

&lt;p&gt;To address the problems posed by named-layouts, (group) layouts allows you to group your routes in a a special directory which is named in parenthesis &lt;code&gt;(dashboard)&lt;/code&gt; to indicate that this directory is only meant for group layouts and hence it doesn't appear in the route's URL&lt;/p&gt;

&lt;p&gt;Our new folder structure now becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/routes/
├ (dashboard)/
│ ├ courses/
│ │ ├ +page.svelte
│ │ └ +page.ts
│ ├ lecturers/
│ │ ├ +page.svelte
│ │ └ +page.ts
│ ├ schedules/
│ │ ├ +page.svelte
│ │ └ +page.ts
│ ├ settings/
│ │ ├ +page.svelte
│ │ ├ +page.ts
│ │ └ +layout.svelte
│ ├ +page.svelte
│ ├ +page.ts
│ └ +layout.svelte
├ (auth)
│ ├ login/
│ │ ├ +page.svelte
│ │ └ +page.ts
│ ├ register/
│ │ ├ +page.svelte
│ │ └ +page.ts
│ └ +layout.svelte
├ (deeplink)
│ ├ [deeplink=action]/
│ │ ├ +page.svelte
│ │ └ +page.ts
└ +layout.svelte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our directory now looks cleaner, and it is more easy to reason out where the layout belongs. Knowing that  any folder in parenthesis &lt;code&gt;()&lt;/code&gt; wont, appear in the URL, one can easily read out the the route structure, by ignoring any (group) named folder in the hierarchy.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the &lt;code&gt;/courses&lt;/code&gt;, &lt;code&gt;/lecturers&lt;/code&gt;, &lt;code&gt;/schedules&lt;/code&gt; and the &lt;code&gt;/settings&lt;/code&gt; routes will all inherit from the layout from &lt;code&gt;src/routes/(dashboard)/+layout.svelte&lt;/code&gt; as well as the &lt;code&gt;src/routes/+layout.svelte&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/login&lt;/code&gt; and &lt;code&gt;/register&lt;/code&gt; will also inherit from &lt;code&gt;src/routes/(auth)/+layout.svelte&lt;/code&gt; as well as the &lt;code&gt;src/routes/+layout.svelte&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While &lt;code&gt;src/routes/(dashboard)/+page.svelte&lt;/code&gt; will be the index/homepage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remember, (dashboard) is ignored in the route hierarchy making  &lt;code&gt;src/routes/(dashboard)/+page.svelte&lt;/code&gt; at the same level as &lt;code&gt;src/routes&lt;/code&gt; so please keep that in mind before you throw another &lt;code&gt;+page.svelte&lt;/code&gt; at the same level or else SvelteKit will crash and throw back at you a nice error message&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error:  and (dashbaord) occupy the same route
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pages and layouts inside groups — as in any other directory — will inherit layouts above them, unless they &lt;em&gt;break out&lt;/em&gt; of the layout hierarchy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A page can break out of a layout by specifying the name of the folder that contains the layout file you want to inherit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For example to beak the &lt;code&gt;/settings&lt;/code&gt; route from inheriting from the &lt;code&gt;src/routes/(dashboard)/+layout.svelte&lt;/code&gt; to rather inherit a parent layout above it like the root layout in &lt;code&gt;src/routes/+layout.svelte&lt;/code&gt;, you can rename  &lt;code&gt;src/routes/(dashboard)/settings/+page.svelte&lt;/code&gt; to &lt;code&gt;src/routes/(dashboard)/settings/+page@.svelte&lt;/code&gt; where &lt;code&gt;@&lt;/code&gt; represents the root layout in the hierarchy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also, if there are parent layouts in the hierarchy above that you want a page to break out of its current inheritance and inherit from parent layout, specify &lt;code&gt;@(parent-group)&lt;/code&gt; or &lt;code&gt;@parent-folder&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To break out of the layout uses a similar convention as the page. Simply use &lt;code&gt;@&lt;/code&gt; followed by the name of a folder above in the hierarchy where the layout you want to inherit is like &lt;code&gt;@(parent-group)&lt;/code&gt; or &lt;code&gt;@parent-folder&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A  &lt;code&gt;+layout@.svelte&lt;/code&gt;  component would reset the hierarchy for all its child routes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Migrate your SvelteKit app to use (group) layouts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Delete the &lt;code&gt;node_modules&lt;/code&gt; folder and the &lt;code&gt;.sveltekit&lt;/code&gt; and the &lt;code&gt;package-lock.json&lt;/code&gt; or the &lt;code&gt;-lock&lt;/code&gt; file for any package manager you are using
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; node_modules .sveltekit
&lt;span class="nb"&gt;rm &lt;/span&gt;package-lock.json &lt;span class="c"&gt;# change it as per your package manager `pnpm` or `yarn`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;And reinstall the packages again using your preferred  package manager
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is necessary in order to ensure that you have the latest versions of the dependencies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/npm-check-updates"&gt;npm-check-updates&lt;/a&gt; &lt;code&gt;ncu&lt;/code&gt; may not be helpful here so play it safe and reinstall all your dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is the cost you've to pay for rolling on the bleeding-edge&lt;/p&gt;

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

&lt;p&gt;SvelteKit is amazing and Rich-Harris and the team are making great improvements to make it more awesome.&lt;br&gt;
We really appreciate them for everything they are doing to make out lives easier and simple as developers.&lt;/p&gt;

&lt;p&gt;Seen a typo, noticed something wrong in the article, still confused, share your thoughts in the comment section below or find me on &lt;a href="https://svelte.dev/chat"&gt;Svelte's Discord channel&lt;/a&gt; &lt;code&gt;@parables_boltnoel&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's all for this post. If this post has helped you, spread the love and help others by sharing it.&lt;/p&gt;

&lt;p&gt;See you in the next update&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>sveltekit</category>
      <category>javascript</category>
      <category>news</category>
    </item>
    <item>
      <title>Alpine data methods and binding `this` keyword</title>
      <dc:creator>Parables</dc:creator>
      <pubDate>Thu, 30 Jun 2022 20:05:07 +0000</pubDate>
      <link>https://forem.com/parables/alpine-data-methods-and-binding-this-keyword-2hgk</link>
      <guid>https://forem.com/parables/alpine-data-methods-and-binding-this-keyword-2hgk</guid>
      <description>&lt;p&gt;Alpine docs says that you can access magic properties with the this keyword. But how do you access the this keyword from a external nested method/function.&lt;/p&gt;

&lt;p&gt;The solution: Use Arrow syntax&lt;/p&gt;

&lt;p&gt;check out this simple demo for plupload &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/black-cache-sytlzk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Auto dismissing alert</title>
      <dc:creator>Parables</dc:creator>
      <pubDate>Tue, 21 Jun 2022 00:04:00 +0000</pubDate>
      <link>https://forem.com/parables/how-to-alpinejs-auto-dismissing-alert-3ff3</link>
      <guid>https://forem.com/parables/how-to-alpinejs-auto-dismissing-alert-3ff3</guid>
      <description>&lt;p&gt;This is part on my mini series(&lt;a href="https://dev.to/parables/how-to-alpinejs-i91"&gt;"How to AlpineJS"&lt;/a&gt;) on how to build reusable component that can be used in any web framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Auto dismissing alert
&lt;/h2&gt;


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


&lt;blockquote&gt;
&lt;p&gt;[!Tip]&lt;br&gt;
Any arguments you pass after the duration for &lt;code&gt;setTimeout&lt;/code&gt; will be passed to the callback function. In the code snippet above, we passed &lt;a href="https://alpinejs.dev/magics/root"&gt;&lt;code&gt;$this.root&lt;/code&gt;&lt;/a&gt; magic property so that we can auto dismiss it after 5000ms(5 seconds)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next: &lt;a href=""&gt;Multipurpose TextField&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>html</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to AlpineJS - Introduction</title>
      <dc:creator>Parables</dc:creator>
      <pubDate>Mon, 20 Jun 2022 21:16:41 +0000</pubDate>
      <link>https://forem.com/parables/how-to-alpinejs-i91</link>
      <guid>https://forem.com/parables/how-to-alpinejs-i91</guid>
      <description>&lt;p&gt;If you haven't tried &lt;a href="https://alpinejs.dev/"&gt;Alpine.js&lt;/a&gt; yet you are missing out on the fun side of Javascript.&lt;/p&gt;

&lt;p&gt;Alpine is a rugged, minimal tool for composing behavior directly in your markup. Think of it like jQuery for the modern web. Plop in a script tag and get going.&lt;/p&gt;

&lt;p&gt;This post was inspired by &lt;a href="https://github.com/parables/svelta"&gt;Svelta&lt;/a&gt;, a copy-paste component library I made for Svelte(worth checking it out if you are using Svelte/SvleteKit). &lt;/p&gt;

&lt;p&gt;I am in the process of rewriting those components and so instead of depending on Svelte for certain functionality, I decided to try Alpine.js. This way, you can still use the components in any framework.&lt;/p&gt;

&lt;p&gt;In this series, I will share some tips on how to build simple components using this tiny but powerful framework and hopefully you may find some of it handy.&lt;/p&gt;

&lt;p&gt;Wait there is more...&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>html</category>
      <category>design</category>
      <category>javascript</category>
    </item>
    <item>
      <title>API with NodeJS &amp; ExpressJS in just 5 lines of code😉</title>
      <dc:creator>Parables</dc:creator>
      <pubDate>Thu, 16 Dec 2021 09:35:05 +0000</pubDate>
      <link>https://forem.com/parables/api-with-nodejs-expressjs-in-just-5-lines-of-code-kpm</link>
      <guid>https://forem.com/parables/api-with-nodejs-expressjs-in-just-5-lines-of-code-kpm</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Intro&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In this tutorial we will create a simple API with NodeJs and ExpressJS,&lt;br&gt;
connect to MongoDB and perform CRUD operations with the API.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step by Step Process&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a NodeJs project in a new folder by running the command:
&amp;gt; &lt;code&gt;npm init -y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Install the following packages in the project by running the command:
&amp;gt;    &lt;code&gt;npm install express mongoose dotenv cors nodemon&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a new file in your project folder with the name &lt;code&gt;index.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;index.js&lt;/code&gt; file, create a simple server with ExpressJS
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
   &lt;span class="c1"&gt;//index.js&lt;/span&gt;

   &lt;span class="c1"&gt;// import the packages we need&lt;/span&gt;
   &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&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="nx"&gt;dotenv&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;dotenv&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;// loads the env file content into the process.env&lt;/span&gt;
   &lt;span class="c1"&gt;// Use process.env.VARIABLE_NAME to access the value of the variable in our .env file&lt;/span&gt;
   &lt;span class="nx"&gt;dotenv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// create an instance of express&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// store the PORT number from the .env file into a constant variable&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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// create an index route to test the server&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="c1"&gt;// start the server to listen to incoming request&lt;/span&gt;
    &lt;span class="c1"&gt;// on the specified PORT&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is up and running 🚀🚀🚀 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;ol&gt;
&lt;li&gt;Create a new file in your project folder with the name &lt;code&gt;.env&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;.env&lt;/code&gt;  file, add an environment variable for the &lt;code&gt;PORT&lt;/code&gt; number
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    PORT=5000

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

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Test the server by running g the command &lt;code&gt;node index.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Open your browser and type &lt;code&gt;http://localhost:5000/&lt;/code&gt; to make a GET request to your server and see your first response.&lt;/li&gt;
&lt;li&gt;If you get a "Hello World" response, you have created your first Server that is ready to respond whenever a request comes in.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Congratulations 🎊🎊🎉🎉🥳🥳&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You can end here but an API can do more than just response with a "Hello World".&lt;br&gt;
In the next step we will connect to mongoDB Atlas using a connection string and add more routes to  Create, Read, Update &amp;amp; Delete resources.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;CRUD operations&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Just like how we created the index route on line 13, we will create more routes to handle different requests.&lt;/p&gt;

&lt;p&gt;A request is made with an HTTP verb/method that specifies the type of operation the request wants to perform on the server. Some of the common HTTP methods we will look at in this tutorial are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;POST - used to create a new resource on the server&lt;/li&gt;
&lt;li&gt;GET - used to read a resources on the server&lt;/li&gt;
&lt;li&gt;PATCH - used to update a resource on the server by &lt;strong&gt;merging&lt;/strong&gt; the existing resource with the incoming data&lt;/li&gt;
&lt;li&gt;PUT - used to update a resource on the server by &lt;strong&gt;replacing&lt;/strong&gt; the existing resource with the incoming data&lt;/li&gt;
&lt;li&gt;DELETE - used to delete a resource on the server.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Connecting to MongoDB Atlas&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Search for MongoDB Atlas on the browser and signUp for an account. If you already have an account, then please login.&lt;/p&gt;

&lt;p&gt;Follow the welcome guide to create a new project and build your first database cluster.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note the password you set for your user account on your cluster as you will need it to connect to the Cluster later on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please make sure to allow access from every I.P address&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to the network menu on your MongoDB Atlas dashboard, add a new IP address and click on &lt;code&gt;ALLOW ACCESS FROM EVERYWHERE&lt;/code&gt; button.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Click the CONNECT button on the Overview page. Then select Connect your application from the options.&lt;br&gt;
Finally, make sure NodeJS is selected and the version is 4.0 upwards.&lt;/p&gt;

&lt;p&gt;Copy the connection string below and add an environment variable for the &lt;code&gt;MONGO_DB_CON_STRING&lt;/code&gt; in the &lt;code&gt;.env&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    PORT=5000

    MONGO_DB_CON_STRING=mongodb+srv://admin:&amp;lt;password&amp;gt;@cluster0.nvren.mongodb.net/myFirstDatabase?retryWrites=true&amp;amp;w=majority

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Make sure to replace &amp;lt;password&amp;gt; with the password you choose during the welcome guide process and replace myFirstDatabase in the connection string with a name for your database. You can give it any name&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To connect our API with MongoDB, we will use the mongoose package we installed earlier on. Add this line to your import statements at the top of the index.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since our server depends on the database to handle requests, its essential to attempt a connection to MongoDB Atlas first. If the connection is successfully, we  then start our server to listen to incoming requests on the specified &lt;code&gt;PORT&lt;/code&gt; number.&lt;br&gt;
If the connection fails, we simply log out the error to the console.&lt;/p&gt;

&lt;p&gt;But feel free to separate the connection to mongoDB from starting the server if your server directly doesn't directly depend on the database.&lt;/p&gt;

&lt;p&gt;Replace this part of 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;// start the server to listen to incoming request&lt;/span&gt;
   &lt;span class="c1"&gt;// on the specified PORT&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is up and running 🚀🚀🚀 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;p&gt;with this:&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;// connect to MongoDBAtlas first&lt;/span&gt;
&lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGO_DB_CON_STRING&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="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="c1"&gt;// if we get an error, log it out to the console&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Failed to connect to MongDB &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// if connection is successful... start the server&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MongoDB is connected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// start the server to listen to incoming request&lt;/span&gt;
    &lt;span class="c1"&gt;// on the specified PORT&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is up and running 🚀🚀🚀 on PORT: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Model your data&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;MongoDB is a NoSQL database.&lt;/p&gt;

&lt;p&gt;In comparison to the relational SQL database:&lt;br&gt;
 a &lt;code&gt;TABLE&lt;/code&gt; in SQL is a &lt;code&gt;COLLECTION&lt;/code&gt; in NoSQL,&lt;br&gt;
 a &lt;code&gt;COLUMN&lt;/code&gt; in SQL is a &lt;code&gt;FIELD&lt;/code&gt; in NoSQL,&lt;br&gt;
 and &lt;code&gt;ROW&lt;/code&gt; in SQL is a &lt;code&gt;DOCUMENT&lt;/code&gt; in NoSQL,&lt;/p&gt;

&lt;p&gt;Meaning, you can structure your document in the Collection any how you want it. To help make storing and retrieving documents, we will create a model to represent how data will be stored and retrieved.&lt;/p&gt;

&lt;p&gt;Inside your project, create a new folder called &lt;code&gt;models&lt;/code&gt; and inside the &lt;code&gt;models&lt;/code&gt; folder create a new file called &lt;code&gt;todo.js&lt;/code&gt; and paste in the following code snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//import the mongoose package&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="c1"&gt;// unpack Schema &amp;amp; model from mongoose&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&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="c1"&gt;// the Schema defines the structure of our Collection(Table) in MongoDB&lt;/span&gt;
&lt;span class="c1"&gt;// the model defines how data will modeled for our collection&lt;/span&gt;
&lt;span class="c1"&gt;// and comes along with built in features  to manipulate the data&lt;/span&gt;

&lt;span class="c1"&gt;// create a new Schema instance specifying which&lt;/span&gt;
&lt;span class="c1"&gt;// fields(columns) we want in our Collection(Table)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&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;date&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;time&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;isCompleted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// then make a model&lt;/span&gt;
&lt;span class="c1"&gt;// by passing in the name and a schema for our model&lt;/span&gt;
&lt;span class="c1"&gt;// and export it to be imported used in other files&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;Todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;todo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;todoSchema&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Adding more routes to perform CRUD operations&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Inside the &lt;code&gt;index.js&lt;/code&gt; file, import the Todo model we 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;// import Todo model to perform crud operations&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;Todo&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/todo.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;To extract the body of requests made with &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt; &amp;amp; &lt;code&gt;PATCH&lt;/code&gt;, we need to add line this after we created the server instance in &lt;code&gt;index.js&lt;/code&gt; file;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// create an instance of express&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// use the json middleware to&lt;/span&gt;
&lt;span class="c1"&gt;// extract the data stored in the request body&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Importing your files like this will crash your Server. To fix it, inside the &lt;code&gt;package.json&lt;/code&gt; file, add a comma after the "main": "index.js" line and paste this there.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;"type": "module",&lt;/code&gt; helps us to use the modern import statement. Read more on javascript.info about Modules&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below the index route, add the following code snippets.&lt;/p&gt;

&lt;p&gt;Each snippet is well commented to explain what the code is doing so be sure to read them.&lt;/p&gt;

&lt;p&gt;The async/await keywords you see simply means our server will keep on listening to more request while it await the results of the asynchronous operation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CREATE a todo&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// create routes to perform CRUD operations with the Todo model&lt;/span&gt;

&lt;span class="c1"&gt;// CREATE a new Todo resource in the database by making a POST request to /todo &lt;/span&gt;
&lt;span class="c1"&gt;// the data to be saved must be in your request's body in json format &lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// extract the necessary fields from the body of the request&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isCompleted&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// create a Todo model with the necessary fields&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;isCompleted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;isCompleted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="c1"&gt;// save the Todo model and await the result&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="c1"&gt;// send back a response with the result in a json format&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;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;blockquote&gt;
&lt;p&gt;READ all todos&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// READ all the Todos from the database by making a GET request to /todos&lt;/span&gt;
&lt;span class="c1"&gt;// the Model.find({}) method takes in a object as parameter that will be used to filter the documents we retrieve. E.g: Retrieve all documents where title is "Learn API with NodeJs &amp;amp; Express" will be written as:&lt;/span&gt;
&lt;span class="c1"&gt;// await Todo.find({"title": "Learn API with NodeJs &amp;amp; Express"});&lt;/span&gt;
&lt;span class="c1"&gt;// an empty object {} means no filter is applied so retrieve all the documents&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&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;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;blockquote&gt;
&lt;p&gt;UPDATE a todo by &lt;strong&gt;merging&lt;/strong&gt; existing data with incoming data&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// UPDATE a Todo resource in the database by making a PATCH request to /todo/:todoID&lt;/span&gt;
&lt;span class="c1"&gt;// a PATCH request should merge the previous resource with incoming data&lt;/span&gt;
&lt;span class="c1"&gt;// :todoID is a request parameter and can be used by req.params.todoID&lt;/span&gt;
&lt;span class="c1"&gt;// the data to be saved must be in your request's body in json format &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;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todo/:todoID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//find and update a model by&lt;/span&gt;
  &lt;span class="c1"&gt;// passing in the id, the data to be updated,&lt;/span&gt;
  &lt;span class="c1"&gt;// and set the new option to true&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByIdAndUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// _id of the document&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// the data to be merged with the existing document&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;new&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;// options&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;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;blockquote&gt;
&lt;p&gt;UPDATE a todo by &lt;strong&gt;replacing&lt;/strong&gt; existing data with incoming data&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// UPDATE a Todo resource in the database by making a PUT request to /todo/:todoID&lt;/span&gt;
&lt;span class="c1"&gt;// a PUT request is almost similar to a PATCH request&lt;/span&gt;
&lt;span class="c1"&gt;//  except that it overwrites the previous resource with incoming data&lt;/span&gt;
&lt;span class="c1"&gt;// :todoID is a request parameter and can be used by req.params.todoID&lt;/span&gt;
&lt;span class="c1"&gt;// the data to be saved must be in your request's body in json format &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;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todo/:todoID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//find and update a model by&lt;/span&gt;
  &lt;span class="c1"&gt;// passing in the id, the data to be updated,&lt;/span&gt;
  &lt;span class="c1"&gt;// and set the new and overwrite options to true&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByIdAndUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// _id of the document&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// data to be replaced&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;overwrite&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;// options&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;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;blockquote&gt;
&lt;p&gt;DELETE a todo&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// DELETE a Todo resource in the database by making a DELETE request to /todo/:todoID&lt;/span&gt;
&lt;span class="c1"&gt;// :todoID is a request parameter and can be used by req.params.todoID&lt;/span&gt;
&lt;span class="nx"&gt;app&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todo/:todoID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//find and delete a model by&lt;/span&gt;
  &lt;span class="c1"&gt;// passing in the id and a callback function&lt;/span&gt;
  &lt;span class="c1"&gt;// that takes in the error and the deletedDocument&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByIdAndDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoID&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="nx"&gt;doc&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Failed to delete the document with _id: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoID&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. Error: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Todo with _id: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoID&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; was not found`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Todo with _id: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoID&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; has been deleted`&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;h3&gt;
  
  
  &lt;strong&gt;Try out your API&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Anytime you make a change in the code, you have to stop the server and start it again by running &lt;code&gt;node index.js&lt;/code&gt;. This process of restarting the server can get pretty tiring.&lt;/p&gt;

&lt;p&gt;We install &lt;code&gt;nodemon&lt;/code&gt; earlier on when we create the project. &lt;code&gt;nodemon&lt;/code&gt; will watching your project and restart the server whenever you save a file.&lt;/p&gt;

&lt;p&gt;To use nodemon, add the &lt;code&gt;start&lt;/code&gt; &amp;amp; &lt;code&gt;start-dev&lt;/code&gt; commands to the &lt;code&gt;scripts&lt;/code&gt; object inside the &lt;code&gt;package.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start-dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"nodemon index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;

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

&lt;/div&gt;



&lt;p&gt;Start your server in development mode by running &lt;code&gt;npm run start-dev&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;npm run start&lt;/code&gt; will be used in production when you publish your server to hosting providers like cyclic.sh or Heroku to start the serve in production mode&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;VS Code has an extension called &lt;code&gt;Thunder Client&lt;/code&gt; so you can install it and try making request to your server.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make sure to use the right HTTP verb/methods for your requests and sure you replace &lt;code&gt;:todoID&lt;/code&gt; the &lt;code&gt;_id&lt;/code&gt; of a document  when you are making a &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;PATCH&lt;/code&gt; or &lt;code&gt;DELETE&lt;/code&gt; requests.&lt;br&gt;
Also, make sure that for your &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt; &amp;amp; &lt;code&gt;PATCH&lt;/code&gt; requests, you add the data to be sent in the body of the request in &lt;code&gt;json&lt;/code&gt; format.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;//TODO: Insert screenshot of the using Thunder Client here&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Publish your API&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To use the API you have just created in your mobile and web apps, you need to publish the server to a hosting provider.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;cyclic.sh is a free hosting provider that makes deploying your NodeJS projects as easy as pushing the codes to GitHub. I highly recommend that you try it out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Pushing your codes to GitHub&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;GitHub is the Google Drive or DropBox for developers. Its an remote store where you can keep your projects that and share your code with others. You can continue working on any other computer just by going to GitHub and cloning the project to the computer.&lt;/p&gt;

&lt;p&gt;Follow these steps to publish your codes to GitHub from this &lt;a href=""&gt;amazing article&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Deploying with cyclic.sh&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;After you have pushed your codes to GitHub, signUp for an account on cyclic.sh. You can use your GitHub account to signUp.&lt;/p&gt;

&lt;p&gt;Create a new app and link it to your GitHub repository.&lt;/p&gt;

&lt;p&gt;Add your MONGO_DB_CON_STRING environment variable you used in the &lt;code&gt;.env&lt;/code&gt; file in the Environment Secrets section and done.&lt;/p&gt;

&lt;p&gt;Anytime you push your code to GitHub, cyclic.sh will deploy your server and give you a url to access your server. This is known as Continuos Deployment.&lt;/p&gt;

&lt;p&gt;cyclic.sh will generate a random unique url for your server. Use this url instead of &lt;code&gt;http://localhost:5000&lt;/code&gt; when you are making requests.&lt;/p&gt;

&lt;p&gt;Follow this article for a detailed guide to &lt;a href="https://dev.to/_iametornam/deploy-your-nodejs-auth0-rest-api-to-cyclicsh-under-4-minutes-j8h"&gt;deploying your API on cyclic.sh&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ProTip: Anything can go wrong when a server receives a request. It is recommended that you always wrap your codes in a try/catch block to handle the errors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you have any issues, please write them in the comments section below.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can follow me on &lt;a href=""&gt;YouTube&lt;/a&gt; for more juicy contents daily. Find me on &lt;a href=""&gt;Twitter&lt;/a&gt; and browse through the source code for this project on &lt;a href=""&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's all folks!!!.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
