<?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: Zechariah Hounwanou</title>
    <description>The latest articles on DEV Community by Zechariah Hounwanou (@codexive_zech).</description>
    <link>https://dev.to/codexive_zech</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%2F273103%2F19107ef8-0d9b-4768-aab9-d9f80802b1cb.jpg</url>
      <title>DEV Community: Zechariah Hounwanou</title>
      <link>https://dev.to/codexive_zech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codexive_zech"/>
    <language>en</language>
    <item>
      <title>Streamlining Your Contribution: How to Get Your TMDB API Key for LDBflix Contribution</title>
      <dc:creator>Zechariah Hounwanou</dc:creator>
      <pubDate>Sun, 12 May 2024 14:13:23 +0000</pubDate>
      <link>https://dev.to/codexive_zech/streamlining-your-contribution-how-to-get-your-tmdb-api-key-for-ldbflix-contribution-52gf</link>
      <guid>https://dev.to/codexive_zech/streamlining-your-contribution-how-to-get-your-tmdb-api-key-for-ldbflix-contribution-52gf</guid>
      <description>&lt;p&gt;Developers wishing to contribute to the LBDflix open-source project might not be able to do so without explicit instructions about how to obtain the API key from The Movie Database (TMDB).&lt;/p&gt;

&lt;p&gt;In this article, we will guide developers through the process of obtaining their own TMDB API Key and securely storing it in their project development environment so that they can access and contribute to the data easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is LDBflix
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://lbdflix.vercel.app/"&gt;LDBflix&lt;/a&gt; is a dynamic movie web application that provides information and trailers for a wide range of movies and television shows. It is a comprehensive movie collection for any movie fan, and it also suggests movies and TV episodes to viewers. It is an open-source project that welcomes developers, designers, writers, and movie enthusiasts to cooperate and learn.&lt;/p&gt;

&lt;p&gt;This open-source project provides a user-friendly interface for browsing and discovering a large collection of movies and TV episodes. A third-party API is used to obtain data, ensuring up-to-date information.&lt;/p&gt;

&lt;p&gt;The platform is created using NextJS and CSS for styling. Here's the &lt;a href="https://github.com/lambeboluwatife/lbdflix"&gt;GitHub repository&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is TMDB
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.themoviedb.org/"&gt;TMDB&lt;/a&gt; is a community-built Movie and TV Show database that offers a fantastic Application Programming Interface (API) for accessing available movies and television shows. It also provides details about movies and TV shows, such as titles, synopses, release dates, cast lists, and genres.&lt;/p&gt;

&lt;p&gt;Once your API key has been generated, you will be able to interact with the TMDB API and retrieve data.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Obtain a TMDB API Key
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Open your browser and enter the phrase "&lt;strong&gt;tmdb&lt;/strong&gt;", and you should see several results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshvj2jv8r8jm8qu7ev5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshvj2jv8r8jm8qu7ev5t.png" alt="1. Search result for tmdb" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Click on the first result available in the search and it will redirect you to The Movie Database's official home page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzi70jjxnbu0s8yluyirv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzi70jjxnbu0s8yluyirv.png" alt="2. the official home page of tmdb" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; You can create an account on TMDB by clicking &lt;strong&gt;Join TMDB&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F74y16614yw0a0okdykbp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F74y16614yw0a0okdykbp.png" alt="3. tmdb account creation" width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Complete the sign-up form with valid credentials, and then click the &lt;strong&gt;Sign Up&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xn7fhraj07kly19j1o5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xn7fhraj07kly19j1o5.png" alt="4. clicking sign up button for account creation" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; After registering for a TMDB account, the next step is to confirm your TMDB account information. The next step is to go to your mailbox. You will receive a Verification required email, and you will need to click the &lt;strong&gt;Activate My Account&lt;/strong&gt; button to complete the process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffaiz4e2hnuf9wljc3dk4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffaiz4e2hnuf9wljc3dk4.png" alt="5. tmdb email verification reqirement" width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; When you click the &lt;strong&gt;Activate My Account&lt;/strong&gt; button in your email, you will be redirected to the TMDB login page with a banner that says Email Verified. You can now log in by entering your valid details and clicking &lt;strong&gt;Login&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzev9gln2n4mi4osvkly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzev9gln2n4mi4osvkly.png" alt="6. tmdb login after email verification" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; After successfully logging in, you will be taken to the Dashboard page, which displays all of your account information. Next, you need to generate a TMDB API key for your account by clicking the image icon in the top right corner of your dashboard. Next, locate and click on the &lt;strong&gt;Settings&lt;/strong&gt; option in the dropdown menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdy9vzilxl8tku0ch67n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdy9vzilxl8tku0ch67n.png" alt="7. tmdb dashboard" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; Now that you're on your account's &lt;strong&gt;Settings&lt;/strong&gt; page, you'll need to generate your own API key. To begin, select the &lt;strong&gt;API option&lt;/strong&gt; from the &lt;strong&gt;Settings Menu&lt;/strong&gt; located in the bottom left corner. Next, click on the &lt;strong&gt;Create&lt;/strong&gt; tab and choose the &lt;strong&gt;Developer option&lt;/strong&gt; to register the API Key as a Developer. Following that, you will be taken to a page where you must enter certain credentials and submit a form.&lt;/p&gt;

&lt;p&gt;NOTE: After 24 hours, TMDB will validate your request and generate your API Key. Don't panic after submitting; you'll receive an acceptance notification on your dashboard within 24 hours.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb8butcbl8elzszmhuc9r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb8butcbl8elzszmhuc9r.gif" alt="8. tmdb API Key creation" width="600" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt; Once your API Key creation request has been granted, return to the &lt;strong&gt;API option&lt;/strong&gt; in your &lt;strong&gt;Settings Menu&lt;/strong&gt;. The API Key and API Read Access Token have already been generated, but it is the API Key you should be concerned about first.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1iz9xhpxm42mohkdjl2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1iz9xhpxm42mohkdjl2.png" alt="9. accessing tmdb API Key" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations, you have successfully generated your own TMDB API key.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Access TMDB Documentations
&lt;/h2&gt;

&lt;p&gt;Now that you've received your API Key, let's look through the documentation to see which APIs are available for consumption.&lt;/p&gt;

&lt;p&gt;To begin using TMDB Documentation, return to the &lt;strong&gt;API option&lt;/strong&gt; of your &lt;strong&gt;Settings Menu&lt;/strong&gt; and select the link below the &lt;strong&gt;Documentation section&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkc5fdzde7ccofaug36i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkc5fdzde7ccofaug36i.png" alt="10. documentation section" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, you should be taken to the &lt;strong&gt;Getting Started section&lt;/strong&gt; of the TMDB API Documentation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F97vopi31la03cm5od8wr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F97vopi31la03cm5od8wr.png" alt="11. API Doc reference placement" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the &lt;strong&gt;API Reference&lt;/strong&gt; tab in the top left corner of the Documentation section to access a list of available APIs. In the left sidebar of the page, you can see and scroll through a list of APIs once you have completed that process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyp2ig31auioeykbr33b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyp2ig31auioeykbr33b.gif" alt="12. List of Available APIs" width="600" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila, you have now explored all the APIs in TMDB. Now, add the API key you created previously to the open-source project so you can contribute to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Add Your API Key to LDBflix
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; You will need to fork the Github repository where LDBflix is stored. To do so, visit the &lt;a href="https://github.com/lambeboluwatife/lbdflix"&gt;GitHub repository here&lt;/a&gt; and click the *&lt;em&gt;fork *&lt;/em&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3kgifbkr4b7f37dpx4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3kgifbkr4b7f37dpx4e.png" alt="13. Github URL To fork" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Next, clone the forked repository to your machine so that you can add your API Key and contribute features to the open-source project.&lt;/p&gt;

&lt;p&gt;To begin, get the forked repository URL and clone the project to your machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foi2szs2kel51tw98intf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foi2szs2kel51tw98intf.gif" alt="14. how to clone the repo" width="600" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the terminal/command prompt on your machine and execute the following commands:&lt;/p&gt;

&lt;h4&gt;
  
  
  Clone the Repo
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone git@github.com:&amp;lt;your github username&amp;gt;/lbdflix.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Navigate to the cloned directory
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd lbdflix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Install dependencies
&lt;/h4&gt;



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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Open the project in your text editor (VSCode)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; After successfully cloning the project to your machine and opening it in your VSCode Text Editor, your project's folder structure should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbqyrv25wxk8tn5pq7a61.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbqyrv25wxk8tn5pq7a61.png" alt="15. Cloned project in VScode" width="319" height="601"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, let's integrate the API Key we obtained from TMDB into our cloned open-source project. Your first step will be to create a separate branch for your changes, to do so type the following in your terminal.&lt;/p&gt;

&lt;h4&gt;
  
  
  Initializing the repository
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create a branch
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git branch &amp;lt;branch_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Checkout to the created branch
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout &amp;lt;branch_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your contribution to the project is now possible since the branch has been created. To get started create a .env file in the root directory of your project and include your API Key.&lt;/p&gt;

&lt;p&gt;It should look like this &lt;code&gt;MOVIE_API = &amp;lt;Your API Key&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bg3p2779b6rybg3ajau.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bg3p2779b6rybg3ajau.png" alt="16. how the .env file is placed" width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Your API Key has now been successfully added to the project, so you will need to run it to verify everything is working. To do so, type this 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 run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the command executes successfully without an error, navigate to &lt;code&gt;http://localhost:3000&lt;/code&gt; in your browser to see the UI of your project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfs94gbqeic7laa9x01r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfs94gbqeic7laa9x01r.png" alt="17. project working fine" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations, you have successfully added your API key to the open-source project LDBflix, and can now easily contribute to the project.&lt;/p&gt;

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

&lt;p&gt;The objective of this tutorial is to demonstrate how to generate a TMDB API key and use it to contribute to the open-source LDBflix project's front end.&lt;/p&gt;

&lt;p&gt;We hope to see your PR soon, and here's to many more coding adventures and contributions ahead.&lt;/p&gt;

&lt;p&gt;If you have found this helpful, please consider sharing it with others who might benefit.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>opensource</category>
      <category>webdev</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>From Zero to CRUD Hero: Building Your First Backend API in JavaScript</title>
      <dc:creator>Zechariah Hounwanou</dc:creator>
      <pubDate>Tue, 23 Apr 2024 07:30:00 +0000</pubDate>
      <link>https://dev.to/codexive_zech/from-zero-to-crud-hero-building-your-first-backend-api-in-javascript-238n</link>
      <guid>https://dev.to/codexive_zech/from-zero-to-crud-hero-building-your-first-backend-api-in-javascript-238n</guid>
      <description>&lt;p&gt;Application Programming Interface embed also known as API is the fundamental concept for interacting with data stored in a database. The process of setting it up can be time-consuming and difficult without adequate guidance. As a developer, you must understand how to efficiently develop CRUD (Create, Read, Update, and Delete) API for web applications.&lt;/p&gt;

&lt;p&gt;For a developer to build a robust and flexible API for applications, the ability to set up routes, handle HTTP requests, send back HTTP responses, connect to the MongoDB database, and implement CRUD operations by leveraging on the capabilities of Node.js, Express.js, and MongoDB is needed.&lt;/p&gt;

&lt;p&gt;This tutorial will walk you through the step-by-step process of building a RESTful CRUD API, from setting up the project development environment to connecting with the MongoDB database, then defining a data model with Mongoose which will interact with the MongoDB database cluster, then implementing the CRUD functionalities using Express routes and finally testing the API endpoints. You will also learn how to handle validations and errors. Upon completing this tutorial, you will be able to build dynamic CRUD APIs with efficient and safe data management capabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Node.js
&lt;/h2&gt;

&lt;p&gt;Node.js is a cross-platform runtime environment that runs JavaScript code on the server rather than in the browser. Node.js helps to create scalable and high-performance applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Express.js
&lt;/h2&gt;

&lt;p&gt;Express.js is a Node.js framework that provides a robust set of features for developing Web APIs. It integrates with Node.js, making the creation of APIs simpler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;p&gt;The API will be built using a three-layer architecture based on Node.js, Express.js, and MongoDB.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fms1mhau4voc43c2gxma5.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%2Fms1mhau4voc43c2gxma5.png" alt="CRUD API Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Data Access Layer&lt;/strong&gt; is where we'll communicate and interact with our database. &lt;/p&gt;

&lt;p&gt;Inside the &lt;strong&gt;Controller layer&lt;/strong&gt;, we will handle the entire business logic that is associated with our endpoint's HTTP requests and responses.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Router layer&lt;/strong&gt; will send URLs to the appropriate controller functions. The router functionality will be obtained from Express.&lt;br&gt;
Our API architecture for building CRUD APIs is straightforward, simple, and easy to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is CRUD API?
&lt;/h2&gt;

&lt;p&gt;Since the beginning of this article, we have been hearing the term CRUD API. Let's take a closer look at it now.&lt;/p&gt;

&lt;p&gt;A CRUD API refers to the basic operations that can be performed on the data in a database to enable it to interact with the application. They are used as building blocks in web development to create applications that require data persistence and manipulation.&lt;/p&gt;

&lt;p&gt;So, the phrase CRUD refers to the ability to CREATE, READ, UPDATE, and DELETE entities from a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Each Functionality/Operation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;CREATE:&lt;/strong&gt; This functionality allows the creation of new data in the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;READ:&lt;/strong&gt; This functionality allows the retrieval of existing data from the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; This functionality enables modification of existing data that is already in the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DELETE:&lt;/strong&gt; This functionality allows the removal of existing data that is no longer wanted from the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;p&gt;You do not need to be an expert to follow along with this tutorial, but you will need the following: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Solid understanding of JavaScript &lt;/li&gt;
&lt;li&gt;Knowledge of Node.js and Express.js. &lt;/li&gt;
&lt;li&gt;Download and install Node and Postman on your machine. &lt;/li&gt;
&lt;li&gt;Download and install the VSCode text editor on your computer.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Setting Up Application Development Environment
&lt;/h2&gt;

&lt;p&gt;This tutorial will use VSCode, but any text editor should work. To begin, let's create our development environment. We will not complicate the setup but rather create a simple and orderly project structure for our RESTful CRUD API. To make things easier, we'll split the following sections into two:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting up the project and installing dependencies.&lt;/li&gt;
&lt;li&gt;Creating the Basic Express Server.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Setting up the project and installing dependencies
&lt;/h3&gt;

&lt;p&gt;By typing the following command in our command prompt or command line interface, we can construct the overall project structure, including all relevant folders and files.&lt;/p&gt;

&lt;h4&gt;
  
  
  Navigating to the Desktop
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd Desktop&lt;/code&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  Create a project folder and navigate into it
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;mkdir book-management-api &amp;amp;&amp;amp; cd book-management-api&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create a src folder and navigate into it
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;mkdir src &amp;amp;&amp;amp; cd src&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create subfolders in the src folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;mkdir model &amp;amp;&amp;amp; mkdir controllers &amp;amp;&amp;amp; mkdir routes &amp;amp;&amp;amp; mkdir db&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  We need to move one level up since we are in the src folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd ..&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create an entry point for our API in the root directory
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;touch index.js&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Initialing our node app and creating a package.json file
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;npm init –y&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When you run the above commands, your package.json file and application project structure should be produced. Now launch your project in VSCode using the command below.&lt;br&gt;
&lt;code&gt;code .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xome9j3pc9fh9xge2ph.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%2F7xome9j3pc9fh9xge2ph.png" alt="1. Installing Node.js newly"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you've successfully set up your development environment, your &lt;code&gt;package.json&lt;/code&gt; file should look like the above. Next, we'll add additional development dependencies to our project. To install, open a terminal and enter the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express dotenv mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Express: a flexible Node.js web app framework&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dotenv: it loads environment variables from a .env file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mongoose: it is a MongoDB object modeling tool designed to work in an asynchronous environment.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After installing the essential dependencies, your updated &lt;code&gt;package.json&lt;/code&gt; file should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgu00mmswgjxlxtytxozn.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%2Fgu00mmswgjxlxtytxozn.png" alt="2. Successfully Installed dependencies"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the Basic Express Server
&lt;/h3&gt;

&lt;p&gt;Now let's configure our web server. In the entry file &lt;code&gt;index.js&lt;/code&gt;, add the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require("dotenv").config();
const express = require("express");

const app = express();

app.get("/", (req, res) =&amp;gt; {
  res.send("Hey! Sever is Up.");
});

const PORT = process.env.PORT || 5000;

app.listen(PORT, () =&amp;gt; {
  console.log(`Server Listening on Port ${PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code above accomplishes the following;&lt;br&gt;
(i). We imported dotenv and used the &lt;strong&gt;.config()&lt;/strong&gt; function to initialize it instantly. &lt;br&gt;
(ii). We imported Express and assigned it to a variable named app. This variable now has access to all express methods. &lt;br&gt;
(iii). Then we create a simple route &lt;strong&gt;(/)&lt;/strong&gt; that sends &lt;strong&gt;"Hey! Server is up."&lt;/strong&gt; We received a response when we accessed it. &lt;br&gt;
(iv). Finally, we listen for all incoming requests on port 5000, which we have set as our server origin.&lt;/p&gt;

&lt;p&gt;Let’s start the server, go back to your terminal, and run this command:&lt;/p&gt;

&lt;h4&gt;
  
  
  To Start the server
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;node index.js&lt;/code&gt;&lt;br&gt;
Once everything is completed, open your browser and navigate to &lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;; you should see "Hey! Server is up" displayed on the screen.&lt;/p&gt;

&lt;p&gt;We have one drawback with the node index.js command we used before, it restarts our server every time the file is altered. To make it easier to execute our Node program, we'll add a new start script to our &lt;code&gt;package.json&lt;/code&gt; file. Use the command below to install it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once installed, insert the following script into the &lt;code&gt;package.json&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; "scripts": {
    "start": "nodemon index.js"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The package.json file should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft03d5dak4dbc9ptdcq1b.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%2Ft03d5dak4dbc9ptdcq1b.png" alt="3. added Nodemon and its script"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use the following command to run the server and verify that everything is working correctly:&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;&lt;strong&gt;NOTE:&lt;/strong&gt; Always move to the root directory before running the server.&lt;/p&gt;

&lt;p&gt;The message in the terminal will indicate whether or not the above command has launched the Express server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkk3ae7y5c308eiigh6e4.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%2Fkk3ae7y5c308eiigh6e4.png" alt="4. server starting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From what we can see above our express server is running. You will also see the image below if you go to the URL &lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt; in your browser&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvy3xcrbnmkmvzq4x3aai.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%2Fvy3xcrbnmkmvzq4x3aai.png" alt="5. using Nodemon to run server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila! The project is perfectly set up. In the next section, we will set up our MongoDB Atlas account, Database Cluster, and then retrieve our connection string from MongoDB.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Connect a Node.js application to a MongoDB Database Cluster.
&lt;/h2&gt;

&lt;p&gt;In this part of the tutorial, we'll connect our MongoDB database cluster to our Node application using the connection string and also configure our Node application to connect to our database. To make learning easier, we have divided this section into 3 parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting up MongoDB Atlas, setting up the database cluster, and retrieving the connection strings.&lt;/li&gt;
&lt;li&gt;Configuring and Connecting the Node application to the Database.&lt;/li&gt;
&lt;li&gt;Modifying the Entry File to connect to our Express Server.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Setting up MongoDB Atlas, setting up database cluster, and retrieving the connection strings.
&lt;/h3&gt;

&lt;p&gt;We will set up our MongoDB database, and then retrieve our connection string.&lt;/p&gt;

&lt;p&gt;First, visit &lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;MongoDB Atlas and create an account&lt;/a&gt;, or sign in if you already have one. This article will guide you through &lt;a href="https://dev.to/zechariah17/mongodb-for-beginners-setting-up-your-database-with-atlas-4k53"&gt;the process of creating a MongoDB account&lt;/a&gt;. You should be redirected to your dashboard once you have completed the process. Locate the Connect button and click it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgncptq3u5zgs313q0o9j.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%2Fgncptq3u5zgs313q0o9j.png" alt="7. mongoDB dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Drivers option will appear in the modal, select it so we can connect to our application immediately. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8fzrc9fe9azcn2dcu87.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%2Fe8fzrc9fe9azcn2dcu87.png" alt="8. drivers options"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another modal will appear. Follow the steps below to set up your MongoDB driver and get your connection string:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Select the latest Node.js driver and version. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install the driver package with NPM and install MongoDB. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy and save it somewhere. The connection string will be used to connect the node application to MongoDB. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd162wd0kv24t5sw3jib1.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%2Fd162wd0kv24t5sw3jib1.png" alt="9. connection string section"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We've successfully set up our MongoDB Atlas account, configured our MongoDB Cluster, and obtained the connection string.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring and Connecting the Node application to the Database.
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in the root directory. It will be used to securely store all of our application's sensitive information. Assign a value name to the connection string inside the &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGO_URL=mongodb+srv://&amp;lt;databaseUsername&amp;gt;:&amp;lt;databasePassword&amp;gt;@nodeexp.4glzj53.mongodb.net/BOOK-LISTING-API?retryWrites=true&amp;amp;w=majority
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Replace the above-mentioned database username and password with your own.&lt;/p&gt;

&lt;p&gt;Within the db folder we created earlier, create a new file named &lt;code&gt;connect.js&lt;/code&gt;. You can accomplish this by opening up the terminal and entering the following command:&lt;/p&gt;

&lt;h4&gt;
  
  
  Move to the src folder and then move to the db folder as well
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd src &amp;amp;&amp;amp; cd db&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create a connect.js file
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;touch connect.js&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Navigate from the db folder and then move back to the root folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd .. &amp;amp;&amp;amp; cd ..&lt;/code&gt;&lt;br&gt;
Now, our Node application's folder structure should look like the image below, with the newly created file included.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidrf9h0lwz32vanco3r5.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%2Fidrf9h0lwz32vanco3r5.png" alt="10. successfully created the connect.js file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we need to define a method within the &lt;code&gt;connect.js&lt;/code&gt; file that facilitates the connection between our Node.js application and the database. Include the following code in the &lt;code&gt;connect.js&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

const connectDB = (url) =&amp;gt; {
  return mongoose.connect(url);
};

module.exports = connectDB;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above, we imported Mongoose and constructed a connected function that returns a Promise that can be resolved or rejected via the URL, using the Mongoose &lt;strong&gt;connect()&lt;/strong&gt; method. URL represents the connection string that should be added to the &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modifying the Entry File to connect our Express Server
&lt;/h3&gt;

&lt;p&gt;In &lt;code&gt;index.js&lt;/code&gt;, import the &lt;strong&gt;connectDB&lt;/strong&gt; function at the top and construct a function that connects to our database and launches the server, which will listen for incoming requests once the connection is established.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const connectDB = require("./src/db/connect");

const start = async () =&amp;gt; {
  try {
    await connectDB(process.env.MONGO_URL);
    app.listen(PORT, () =&amp;gt; {
      console.log("Database &amp;amp; Node app connected Successfully");
    });
  } catch (error) {
    console.log(error);
  }
};

start();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Because our &lt;strong&gt;connectDB&lt;/strong&gt; function returns a Promise, the preceding code uses async/await in its start function. The Express server will automatically begin listening for incoming requests once the asynchronous function is resolved and the database connection is established.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptgqdkw4eyflvwme1uzf.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%2Fptgqdkw4eyflvwme1uzf.png" alt="11. server connected node to mongoDB"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After restarting the Node application, you should see the above message, which indicates that your database and application have been synchronized. Good on so far, Let's keep going.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a RESTful CRUD API.
&lt;/h2&gt;

&lt;p&gt;Node JS uses the MVC (Model View Controller) architecture. It's a design pattern. Based on the idea, we will create the following API endpoints for our Book Management:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;POST &lt;strong&gt;/book&lt;/strong&gt; - Create or add a new book. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GET &lt;strong&gt;/book&lt;/strong&gt; - Retrieve all books available.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get &lt;strong&gt;/book/:id&lt;/strong&gt; - Retrieve a specific book.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PATCH &lt;strong&gt;/book/:id&lt;/strong&gt; - Update a book. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Delete &lt;strong&gt;/book/:id&lt;/strong&gt; - DELETE a book.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the first step, we will create the model, followed by the CRUD API operations which will store and retrieve all data in MongoDB.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Model
&lt;/h3&gt;

&lt;p&gt;A model is a fancy constructor derived from a schema. They create and read documents from the underlying MongoDB database. In other words, they form part of the development that represents and handles data. Models are the blueprints that organize our application's data. They describe what can be created, read, updated, and deleted as well as the structure, behavior, and actions that are allowed. We can define our data structure with Mongoose's schema.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;Book.js&lt;/code&gt; file within the model folder. To accomplish this, run the following commands:&lt;/p&gt;

&lt;h4&gt;
  
  
  Move to the src folder and then move to the db folder as well
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd src &amp;amp;&amp;amp; cd model&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create a book.js file (To Define Model structure)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;touch Book.js&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Navigate from the db folder and then move back to the root folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd .. &amp;amp;&amp;amp; cd ..&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After executing the command to create the &lt;code&gt;Book.js&lt;/code&gt; model file, our Node application's folder structure should look like the image below, with the newly created file included.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F90529k2broemrcszmbip.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%2F90529k2broemrcszmbip.png" alt="12. Newly added Book.js file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we'll create a simple book schema with its fields included. In the newly created &lt;code&gt;Book.js&lt;/code&gt; file, include the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

const bookSchema = new mongoose.Schema({
  title: String,
  author: String,
  publicationYear: Number,
  isStared: Boolean,
});

const Book = mongoose.model("Book", bookSchema);

module.exports = Book;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With the code above we accomplish the following. &lt;br&gt;
(i). Imported the Mongoose library to communicate with MongoDB in our Node.js project. &lt;br&gt;
(ii). Used &lt;strong&gt;mongoose.Schema()&lt;/strong&gt; to create the database schema structure for our book document in the MongoDB database. &lt;br&gt;
(iii). &lt;code&gt;Title, Author, PublicationYear, and isStared&lt;/code&gt; indicate the specific fields in each book document. &lt;br&gt;
(iv). The &lt;strong&gt;mongoose.model()&lt;/strong&gt; function creates a model based on the database schema structure we supplied. In addition, we exported the Book model so that it could be reused in other files.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building a CRUD API that uses MongoDB
&lt;/h3&gt;

&lt;p&gt;Now that we have structured the schema of our API, let's start by talking about the two most critical components of a RESTful CRUD API: controllers and routes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Controllers&lt;/strong&gt; are responsible for processing data, making decisions, and orchestrating API flows. They receive requests from routes, interact with the model, and determine the response to give to the client. They include business logic for handling individual requests and generating responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Route&lt;/strong&gt; functions are similar to maps, directing incoming requests to the proper location inside the application. They define which URLs clients can access and what happens when they are visited. Routes are paths users can follow to explore your application, and each route corresponds to a specific function or collection of functions that process requests and respond to them.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Controllers and Routes Work Together in Node.js Applications
&lt;/h3&gt;

&lt;p&gt;Routes serve as the entry point for receiving requests in a Node.js application. When the router receives the request, it determines which route should handle it depending on the URL and HTTP method.&lt;/p&gt;

&lt;p&gt;Next, after identifying the right route, the router calls the controller function associated with it. A controller executes business logic by retrieving data from the request and using it to perform actions such as validating input, generating a response, and retrieving data from the database.&lt;/p&gt;

&lt;p&gt;After processing the request, handling the business logic, and generating a response, the controller returns it to the router, which sends it back to the client. &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Controller
&lt;/h3&gt;

&lt;p&gt;Create a file called &lt;code&gt;bookControllers.js&lt;/code&gt; in the src/controllers folder. To accomplish this, run the following commands:&lt;/p&gt;

&lt;h4&gt;
  
  
  Move to the src folder and then move to the db folder as well
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd src &amp;amp;&amp;amp; cd controllers&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create a bookController.js file in the controllers folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;touch bookControllers.js&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Navigate from the db folder and then move back to the root folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd .. &amp;amp;&amp;amp; cd ..&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After executing the command to create the &lt;code&gt;bookControllers.js&lt;/code&gt; file, our Node application's folder structure should look like the image below, with the newly created file included.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyd702a8xw2jmhsrryz9f.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%2Fyd702a8xw2jmhsrryz9f.png" alt="13. newly added book controller file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we need to add the following functions to &lt;code&gt;bookController.js&lt;/code&gt;, which will function as a communication interface with the Book model, as well as a method to handle CRUD API business logic, as well as a method to forward responses to other parts of the application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Book = require('../model/Book');

const createBook = (req, res) =&amp;gt; {};
const getAllBooks = (req, res) =&amp;gt; {};
const getSingleBook = (req, res) =&amp;gt; {};
const updateSingleBook = (req, res) =&amp;gt; {};
const deleteBook = (req, res) =&amp;gt; {};

module.exports = {
  createBook,
  getAllBooks,
  getSingleBook,
  updateSingleBook,
  deleteBook,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's examine each controller function in the &lt;code&gt;bookController.js&lt;/code&gt; file one by one to see how it implements the business logic.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logic 1: Create a book
&lt;/h4&gt;

&lt;p&gt;Inside the entry file &lt;code&gt;index.js&lt;/code&gt;, add the following built-in Express middleware function:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(express.json());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code above creates middleware that parses incoming JSON data from the request body and makes it available for your express application in &lt;code&gt;req.body&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is required for managing JSON data in the application, such as processing data submitted via forms or API requests.&lt;/p&gt;

&lt;p&gt;Next, open the &lt;code&gt;bookController.js&lt;/code&gt; file. Using the Book model, we can create a document for every book in our database cluster.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const createBook = async (req, res) =&amp;gt; {
  const book = await Book.create(req.body);
  res.status(201).json({book})
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Logic 2: Retrieve all books.
&lt;/h4&gt;

&lt;p&gt;Next, inside the &lt;code&gt;bookController.js&lt;/code&gt; file. To access the complete list of books available in our API, use the &lt;strong&gt;.find({})&lt;/strong&gt; function on the Book model.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getAllBooks = async (req, res) =&amp;gt; {
  const books = await Book.find({});
 res.status(200).json({books})
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Logic 3: Retrieve a Single Book
&lt;/h4&gt;

&lt;p&gt;Our next step will be to retrieve a single book from the database using its unique ID.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getSingleBook = async (req, res) =&amp;gt; {
  const { id: bookId } = req.params;
  const singleBook = await Book.findOne({ _id: bookId });
  res.status(200).json({ singleBook });
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code above accomplishes the following: &lt;/p&gt;

&lt;p&gt;(i). Assign the ID parameter from the request parameter object &lt;code&gt;req.params&lt;/code&gt; to a variable called &lt;code&gt;bookId&lt;/code&gt;. When you make a request, the URL will include the ID. &lt;br&gt;
(ii). The &lt;strong&gt;Book.findOne()&lt;/strong&gt; method searches the database for a single document with the &lt;code&gt;_id&lt;/code&gt; field matching the &lt;code&gt;bookId&lt;/code&gt;from the request parameters.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logic 4: Update a book
&lt;/h4&gt;

&lt;p&gt;Next, we'll update a book document in the database.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const updateSingleBook = async (req, res) =&amp;gt; {
  const { id: bookId } = req.params;
  const singleBook = await Book.findOneAndUpdate({ _id: bookId }, req.body, {
    new: true,
    runValidators: true,
  });
  res.status(200).json({ singleBook });
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above, the following is done: &lt;br&gt;
(i). The &lt;code&gt;const {id: bookId} = req.params&lt;/code&gt; method extracts the id parameter from the request and applies it to the bookId variable. &lt;br&gt;
(ii). The &lt;strong&gt;findOneAndUpdate()&lt;/strong&gt; method changes a single book document stored in the database. &lt;br&gt;
(iii). It searches for a book document using the &lt;code&gt;bookId&lt;/code&gt; and changes it with data from the request body. &lt;br&gt;
(iv). While the &lt;code&gt;new:true&lt;/code&gt; option returns the revised document, the &lt;code&gt;runValidators:true&lt;/code&gt; option executes Mongoose validators.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logic 5: Delete a book
&lt;/h4&gt;

&lt;p&gt;Finally, removing a single book from the database depends on its ID. Add the code below to the bookController.js file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const deleteBook = async (req, res) =&amp;gt; {
  const { id: bookId } = req.params;
  const deleteBook = await Book.findOneAndDelete({ _id: bookId });
  res.status(200).json({ deleteBook });
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above code performs the following: &lt;/p&gt;

&lt;p&gt;(i). Assign the id parameter from the request parameter object &lt;code&gt;req.params&lt;/code&gt; to a variable called &lt;code&gt;bookId&lt;/code&gt;. &lt;br&gt;
(ii). The Book model's &lt;strong&gt;findOneAndDelete()&lt;/strong&gt; method deletes a single book document with a matching &lt;code&gt;_id&lt;/code&gt;field from the database.&lt;/p&gt;

&lt;p&gt;We have developed all the business logic for the CRUD API operations we will be using. To generate the operations, we used the async and await keywords since the database query takes time, which is when the Node.js asynchronous attribute comes into play. The JSON response we return with each operation includes the status code as well as the book document stored in our database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining Routes
&lt;/h3&gt;

&lt;p&gt;When a client sends a request for an endpoint via HTTP requests (GET, POST, PATCH, DELETE), we need to define how the server will respond by setting up the routes.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;code&gt;bookRoute.js&lt;/code&gt; in the src/routes folder. To do this task, open your terminal and type the following commands:&lt;/p&gt;

&lt;h4&gt;
  
  
  Move to the src folder and then move to the db folder as well
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd src &amp;amp;&amp;amp; cd routes&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create a bookRoutes.js file in the controllers folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;touch bookRoutes.js&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Navigate from the db folder and then move back to the root folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;cd .. &amp;amp;&amp;amp; cd ..&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After executing the command to create the &lt;code&gt;bookRoutes.js&lt;/code&gt; file, our Node application's folder structure should look like the image below, with the newly created file included.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmseg2o07qqrvs5jfsz2o.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%2Fmseg2o07qqrvs5jfsz2o.png" alt="14. newly added book routes file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we'll set up routes for each controller. Include the following code content:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const {
  createBook,
  getAllBooks,
  getSingleBook,
  updateSingleBook,
  deleteBook,
} = require("../controllers/bookControllers");
const router = express.Router();

router.post("/", createBook);
router.get("/", getAllBooks);
router.get("/:id", getSingleBook);
router.patch("/:id", updateSingleBook);
router.delete("/:id", deleteBook);

module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code snippet,&lt;br&gt;
(i). Our API is built using the Express package, which provides the functionality that we need. &lt;br&gt;
(ii). Express Router is middleware that organizes application routes. The &lt;code&gt;const router = express.Router()&lt;/code&gt; method is used to create a new router object using Express Router.&lt;br&gt;
(iii). &lt;code&gt;const {...} = require("../controllers/bookControllers")&lt;/code&gt;imports all business logic functions from bookControllers.js. &lt;br&gt;
(iv). There are four types of routes: router.post, router.get, router.patch, and router.delete, linked by URL patterns and controllers. &lt;br&gt;
(v). We export the router object for use in index.js.&lt;/p&gt;

&lt;p&gt;Finally, we must register the &lt;code&gt;bookRoute.js&lt;/code&gt; file in our entry file index.js so that any calls to endpoints are redirected to the correct URL path. To accomplish this, we'll add the following code to the index.js file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const bookRouter = require("./src/routes/bookRoutes");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Use the &lt;strong&gt;app.use()&lt;/strong&gt; function, specifying the path and router handler.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use("/api/v1/books", bookRouter);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When we access the URL &lt;a href="http://localhost:5000/api/v1/books" rel="noopener noreferrer"&gt;http://localhost:5000/api/v1/books&lt;/a&gt;, the router is activated. It effectively functions as a filter, determining when a particular set of routes should be used. &lt;/p&gt;

&lt;p&gt;Here's the whole complete code for the &lt;code&gt;index.js&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require("dotenv").config();
const express = require("express");
const connectDB = require("./src/db/connect");
const bookRouter = require("./src/routes/bookRoutes");

const app = express();

app.use(express.json());

app.get("/", (req, res) =&amp;gt; {
  res.send("Hey! Sever is Up.");
});

app.use("/api/v1/books", bookRouter);

const PORT = process.env.PORT || 3000;

const start = async () =&amp;gt; {
  try {
    await connectDB(process.env.MONGO_URL);
    app.listen(PORT, () =&amp;gt; {
      console.log("Database &amp;amp; Node app connected Successfully");
    });
  } catch (error) {
    console.log(error);
  }
};
start();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Congratulations! We have successfully integrated the MongoDB database into our Node.js application and built the CRUD API action. Before we test our API, let's add some basic validation and error handling to improve its reliability and usability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Validation and Error Handling
&lt;/h2&gt;

&lt;p&gt;Let's work on improving our CRUD API now that it has connected to our MongoDB cluster and we have set up CRUD API operations.&lt;/p&gt;

&lt;p&gt;For any API, error handling and data validation are crucial to ensuring the API responds appropriately to incorrect inputs and unexpected scenarios, thus improving the user experience.&lt;/p&gt;

&lt;p&gt;If a user tries to add a book without a title, author, or publication year, they will be unable to do so. Without effective validation, our database may contain incomplete data. When error validation and error handling are in place, users will be informed of what went wrong rather than left in the dark.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding basic validation
&lt;/h3&gt;

&lt;p&gt;As a first step, let's validate our Book model. Add basic validation to &lt;code&gt;Book.js&lt;/code&gt; in the model folder.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

const bookSchema = new mongoose.Schema({
  title: { type: String, required: [true, "Please Provide a Title"] },
  author: { type: String, required: [true, "Please Provide Author Name"] },
  publicationYear: {
    type: Number,
    required: [true, "Please Provide Publication Year"],
  },
  isStared: { type: Boolean, default: false },
});

const Book = mongoose.model("Book", bookSchema);

module.exports = Book;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Mongoose will produce an error if we create a book without a title, author, or publication year because those fields are required, and the error message will explain what went wrong.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing error handling
&lt;/h3&gt;

&lt;p&gt;An effective error management process involves detecting errors and responding appropriately to them. To handle errors graciously, let's improve our &lt;code&gt;bookController.js&lt;/code&gt; CRUD API action.&lt;/p&gt;

&lt;h4&gt;
  
  
  Create a Book with Error Handling
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const createBook = async (req, res) =&amp;gt; {
  try {
    const book = await Book.create(req.body);
    res.status(201).json({ book });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Retrieving All Books with Error Handling
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getAllBooks = async (req, res) =&amp;gt; {
  try {
    const books = await Book.find({});
    res.status(200).json({ books });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Get a Single Book with Error Handling
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getSingleBook = async (req, res) =&amp;gt; {
  try {
    const { id: bookId } = req.params;
    const singleBook = await Book.findOne({ _id: bookId });
    if (!singleBook) {
          return res.status(404).json({ msg: `No Book with Id ${bookId}` });
    }
    res.status(200).json({ singleBook });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Update the Book with Error Handling
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const updateSingleBook = async (req, res) =&amp;gt; {
  try {
    const { id: bookId } = req.params;
    const singleBook = await Book.findOneAndUpdate({ _id: bookId }, req.body, {
      new: true,
      runValidators: true,
    });
    if (!singleBook) {
          return res.status(404).json({ msg: `No Book with Id ${bookId}` });

    }
    res.status(200).json({ singleBook });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Delete a Book with Error Handling
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const deleteBook = async (req, res) =&amp;gt; {
  try {
    const { id: bookId } = req.params;
    const deleteBook = await Book.findOneAndDelete({ _id: bookId });
    if (!deleteBook) {
      return res.status(404).json({ msg: `No Book with Id ${bookId}` });
    }
    res.status(200).json({ deleteBook });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We implemented &lt;code&gt;try-catch blocks&lt;/code&gt; in all of our CRUD API operations to ensure that every issue detected is handled appropriately. With these additions, our CRUD API now includes comprehensive error handling and validation capabilities. This not only makes our API more stable but also enhances the user experience by displaying clear and informative error messages.&lt;/p&gt;

&lt;p&gt;Here is the complete code for the &lt;code&gt;bookController.js&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Book = require("../model/Book");

const createBook = async (req, res) =&amp;gt; {
  try {
    const book = await Book.create(req.body);
    res.status(201).json({ book });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};

const getAllBooks = async (req, res) =&amp;gt; {
  try {
    const books = await Book.find({});
    res.status(200).json({ books });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};

const getSingleBook = async (req, res) =&amp;gt; {
  try {
    const { id: bookId } = req.params;
    const singleBook = await Book.findOne({ _id: bookId });
    if (!singleBook) {
      return res.status(404).json({ msg: `No Book with Id ${bookId}` });
    }
    res.status(200).json({ singleBook });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};

const updateSingleBook = async (req, res) =&amp;gt; {
  try {
    const { id: bookId } = req.params;
    const singleBook = await Book.findOneAndUpdate({ _id: bookId }, req.body, {
      new: true,
      runValidators: true,
    });
    if (!singleBook) {
      return res.status(404).json({ msg: `No Book with Id ${bookId}` });
    }
    res.status(200).json({ singleBook });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};

const deleteBook = async (req, res) =&amp;gt; {
  try {
    const { id: bookId } = req.params;
    const deleteBook = await Book.findOneAndDelete({ _id: bookId });
    if (!deleteBook) {
      return res.status(404).json({ msg: `No Book with Id ${bookId}` });
    }
    res.status(200).json({ deleteBook });
  } catch (error) {
    res.status(400).json({ msg: error });
  }
};

module.exports = {
  createBook,
  getAllBooks,
  getSingleBook,
  updateSingleBook,
  deleteBook,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the next section of the tutorial, we will test our CRUD API to ensure its stability and robustness. We are close to the finish line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the CRUD API
&lt;/h2&gt;

&lt;p&gt;After implementing error handling and validation, it is critical to test our CRUD API. Testing ensures that everything works as planned and detects any problems before they affect our users. Let's get started testing our CRUD API using Postman.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test each endpoint
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To create a Book API, send a POST request to &lt;a href="http://localhost:3000/api/v1/books" rel="noopener noreferrer"&gt;http://localhost:3000/api/v1/books&lt;/a&gt; with a JSON body that includes the book title, author, and publication year.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu9bgumj5iqij3lwe0613.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu9bgumj5iqij3lwe0613.gif" alt="15. Testing Creating Book endpoint"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Send a GET request to &lt;a href="http://localhost:3000/api/v1/books" rel="noopener noreferrer"&gt;http://localhost:3000/api/v1/books&lt;/a&gt; to retrieve a list of all books available in the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqdt40s4j1psbyjulfu1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqdt40s4j1psbyjulfu1.gif" alt="16. Testing all book endpoint"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To retrieve a single book, use the GET method and include the Book ID in the URL. If the Book ID is incorrect, it will return an error message; however, if the Book ID is correct, it will provide detailed information about that book. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubxhdtlis4hb57k35s1x.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubxhdtlis4hb57k35s1x.gif" alt="17. Testing getting single book endpoint"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The content of a book can be updated by submitting a PACTH request with the new data for the specified Book ID. You will get an error message if the Book ID is incorrect. The new data should, however, be inserted correctly into the book.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3nxv5fvhn2oldpz187t.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3nxv5fvhn2oldpz187t.gif" alt="18. Testing updating book endpoint"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To delete a book from the database, use the DELETE method on the book endpoint and enter the Book ID.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzmpg4571su0vafzzav46.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzmpg4571su0vafzzav46.gif" alt="19. Testing delete book endpoint"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! We did it. After testing the CRUD API we created, they are now fully operational. The tutorial started with setting up our development environment and ended with building our API. That's an impressive achievement, and we should be proud of it.&lt;/p&gt;

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

&lt;p&gt;That was a fantastic ride. I enjoyed creating this piece of content for you and learned a lot in the process. Building a basic CRUD API from scratch is a difficult task that demands patience, perseverance, and the willingness to learn. Fortunately for me, you have shown all of these qualities and more.&lt;/p&gt;

&lt;p&gt;In this tutorial, we looked at backend development by creating a simple Book CRUD API with Node.js and Express.js. This tutorial covered a wide range of topics, including setting up our development environment, creating our MongoDB Atlas account and retrieving our connection string, developing CRUD APIs using Express.js and Node.js, and testing them using Postman.&lt;/p&gt;

&lt;p&gt;Congratulations on creating your CRUD API with Node.js and Express! Here's to many more coding adventures ahead. May the codes be with you!&lt;/p&gt;

&lt;p&gt;If you could provide feedback, I would greatly appreciate it. Please post it in the comments section. If you find this tutorial helpful, please consider sharing it with others who might find it helpful too.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>api</category>
    </item>
    <item>
      <title>Demystifying HTTP: The Secret Language of the Web</title>
      <dc:creator>Zechariah Hounwanou</dc:creator>
      <pubDate>Thu, 28 Mar 2024 08:00:00 +0000</pubDate>
      <link>https://dev.to/codexive_zech/demystifying-http-the-secret-language-of-the-web-2p0a</link>
      <guid>https://dev.to/codexive_zech/demystifying-http-the-secret-language-of-the-web-2p0a</guid>
      <description>&lt;p&gt;In the early days of the web, there was no standard way for the browser to connect to a server. The functionality of web applications was limited because the HTTP protocol lacked valid support for various actions such as submitting form data, updating resources, or deleting resources. After all, that was beyond the simple HTML document retrieval.&lt;/p&gt;

&lt;p&gt;HTTP methods were created to provide a uniform language for communicating on the web. These methods are globally understood by the browser and used to communicate with the server regarding what information is required or what action should be taken. There are many HTTP methods to choose from, each of which allows browsers and servers to interact with each other in various ways.&lt;/p&gt;

&lt;p&gt;The purpose of this article is to provide you with a better understanding of the many HTTP protocols available, how data travels across the web, and how browsers and servers work together to gather accurate data.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is HTTP?
&lt;/h2&gt;

&lt;p&gt;HTTP stands for Hypertext Transfer Protocol, and it serves as a communication channel between the browser and the web server. When a website's URL is entered into a browser, it sends an HTTP request to the server. The server receives the information from the request and sends an HTTP response back to the browser, which then displays the necessary information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Evolution of HTTP
&lt;/h2&gt;

&lt;p&gt;During the early days of the internet, HTTP/0.9 was a simple protocol for retrieving only HTML documents. As the web evolved, HTTP/1.0 was introduced as a more robust way to communicate between browsers and servers. It included features such as headers and status codes, but it lacked efficiency and performance, particularly when loading multiple resources simultaneously.&lt;/p&gt;

&lt;p&gt;HTTP/1.1 was then designed to address that issue with various enhancements such as persistent connections and caching, which dramatically improves web surfing performance. HTTP was later updated to accommodate richer content and more complicated interactions. There were more advancements in HTTP/2, including multiplexing, header compression, and server push, which improved performance and minimized latency.&lt;br&gt;
Today, HTTP not only retrieves HTML web documents but also accesses databases, images, and web content. These resources are accessible with the help of APIs, and we send requests to the API and receive responses through the HTTP protocol, which allows for seamless exchange of data on the web.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is Data Exchanged on the Web?
&lt;/h2&gt;

&lt;p&gt;When we open our favorite search engine and type in a web address such as &lt;a href="//www.hashnode.com"&gt;www.hashnode.com&lt;/a&gt; or &lt;a href="//www.twitter.com"&gt;www.twitter.com&lt;/a&gt;, we send a request message to the server in charge of serving those resources, and the server answers by sending a response message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuafqlx2nqpef6ujrsda2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuafqlx2nqpef6ujrsda2.jpg" alt="image of how data is exchanged on the web" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assuming we were building a business listing application, there would be a search bar, a table to list all businesses, and the ability to search by name, state, and city. Having all business information manually coded into the business listing app would slow down the app and it would be difficult to update manually.&lt;/p&gt;

&lt;p&gt;Instead, the app could use an API to obtain the business listing data. The app would determine the user's location, then send a request to the server and show businesses of all kinds available in the user’s location. Users can choose from a variety of request methods depending on what they are trying to accomplish with the app. A response from the server would include business information plus a few more details, depending on the API.&lt;/p&gt;

&lt;p&gt;To keep track of requests associated with that address, the business listing app communicates with a server application located somewhere. When it receives a request, it works to fulfill it by either reading from a database, using another API, or performing a programmatic computation using the data provided by the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of HTTP Methods
&lt;/h2&gt;

&lt;p&gt;HTTP is a protocol that allows the browser and the server to exchange requests and responses. It is important to note that while each method implements a different semantic, they all share some common characteristics.&lt;/p&gt;

&lt;h3&gt;
  
  
  POST Request
&lt;/h3&gt;

&lt;p&gt;The POST request submits an entity to a specified resource, and the entity data is stored in the HTTP request body when making the request. Successful POST requests will return a 201 response code.&lt;/p&gt;

&lt;p&gt;To add new business data to our listing API, we can use the POST method.&lt;/p&gt;

&lt;h3&gt;
  
  
  GET Request
&lt;/h3&gt;

&lt;p&gt;A GET request is used to retrieve data from a specified resource. A successful GET request has a status code of 200 and returns a response providing the requested information. Depending on how the API is structured.&lt;/p&gt;

&lt;p&gt;In our business listing app, we can use a GET method to obtain business information available for a certain city or the entire country. &lt;/p&gt;

&lt;h3&gt;
  
  
  PUT Request
&lt;/h3&gt;

&lt;p&gt;The PUT requests are used to update an entity of a specific resource available on the server. The PUT request updates the entire entity with the data given in the HTTP request body. If we want to update a portion of our resource with a PUT request, we must include the entire resource's data. Otherwise, it will create a new resource. The next HTTP request will demonstrate how to overcome this obstacle.&lt;/p&gt;

&lt;p&gt;In our business listing app, we can update each business' information using the PUT method.&lt;/p&gt;

&lt;h3&gt;
  
  
  PATCH Request
&lt;/h3&gt;

&lt;p&gt;In a PATCH request, a resource is partially updated. We only need to provide the data that needs updating when using the PATCH method. &lt;br&gt;
When a PATCH request is made, only the field that needs to be updated is updated, whereas when a PUT request is made, the whole resource is updated, even if only one field needs to be changed.&lt;/p&gt;

&lt;p&gt;To update the address of a business in our business listing app, we can use the PATCH method.&lt;/p&gt;

&lt;h3&gt;
  
  
  DELETE Request
&lt;/h3&gt;

&lt;p&gt;We use the DELETE request to remove a specific entity or resource from the server. The business listing app allows us to remove a business from the app if we no longer wish to list it.&lt;/p&gt;

&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we have learned about how the browser communicates with the server and how we can use HTTP requests available to exchange data on the web. If you have found this helpful, please consider sharing it with others who might benefit.&lt;/p&gt;

&lt;p&gt;Thank you for Reading.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>http</category>
      <category>node</category>
      <category>web</category>
    </item>
    <item>
      <title>Conquer the Challenge: A Streamlined Guide to Connecting MongoDB to Your Node Application</title>
      <dc:creator>Zechariah Hounwanou</dc:creator>
      <pubDate>Tue, 12 Mar 2024 08:00:00 +0000</pubDate>
      <link>https://dev.to/codexive_zech/conquer-the-challenge-a-streamlined-guide-to-connecting-mongodb-to-your-node-application-48lk</link>
      <guid>https://dev.to/codexive_zech/conquer-the-challenge-a-streamlined-guide-to-connecting-mongodb-to-your-node-application-48lk</guid>
      <description>&lt;p&gt;After creating my MongoDB Atlas account and setting up my MongoDB Atlas cluster for the first time, a big challenge arose. I didn't know what to do next. I asked myself, how can I connect to my database? What is the process for inserting data into the database? What are the options for querying the database? Those were the few questions that popped into my head. As soon as I realized these questions had to be answered, I began looking for answers. That's when I realized I needed a connection string to establish a successful connection between my Node.js application and my MongoDB cluster.&lt;/p&gt;

&lt;p&gt;The purpose of this article is to provide you with the necessary information on how to seamlessly connect your Node.js application to your MongoDB Atlas Cluster, revealing the full potential of a scalable and flexible database solution. From creating the connection string to establishing a successful connection integration, we'll help you make sure your application communicates efficiently with MongoDB.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To follow along with this article and seamlessly connect your MongoDB Cluster to your Node application, the following are necessary:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Installation of Node and NPM on your machine. &lt;/li&gt;
&lt;li&gt;Basic understanding of JavaScript, Node.js, and Express.js.&lt;/li&gt;
&lt;li&gt;Have a MongoDB Atlas account and already set up a Cloud Cluster. You can get the &lt;a href="https://codexive.hashnode.dev/mongodb-for-beginners-setting-up-your-database-with-atlas"&gt;guide here&lt;/a&gt; if you haven't already.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is a Connection String?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A MongoDB connection string is a piece of text that instructs the MongoDB client such as your Node.js application on how to establish a connection with the MongoDB cluster. In other words, it serves as a secret passcode that connects your Node.js application to the MongoDB cluster.&lt;/p&gt;

&lt;p&gt;A connection string contains vital encoded information, such as the cluster's servers, the database user's password and username, and the name of the database used for query execution. The database driver requires all of these to locate and connect to the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Find the MongoDB Connection String.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before we can obtain the connection string, we must first determine who will have access to our database and prevent others from altering it.&lt;/p&gt;

&lt;p&gt;As you can see, a user is already available. We have already set up our MongoDB Cluster by providing a database username and password. If you skip that step or want to grant another user access to the database, then click the Add New Database User button.&lt;/p&gt;

&lt;p&gt;Now that we understand what a connection string is and how vital it is for establishing a seamless connection, we need to retrieve it and use it to connect our Node.js application to our MongoDB cluster. To accomplish this, we will break this guide down into 3 sections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the MongoDB Connection String.&lt;/li&gt;
&lt;li&gt;Setting up the Development Environment for a Node.js application.&lt;/li&gt;
&lt;li&gt;Connecting a Node.js application to a MongoDB cluster.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Find the MongoDB Connection String.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before we can obtain the connection string, we must first determine who will have access to our database and prevent others from altering it.&lt;/p&gt;

&lt;p&gt;As you can see, a user is already available. We have already set up our MongoDB Cluster by providing a database username and password. If you skip that step or want to grant another user access to the database, then click the &lt;strong&gt;Add New Database User&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktdu4iehktj1smrg79g3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktdu4iehktj1smrg79g3.png" alt="Image 1" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After clicking the &lt;strong&gt;Add New Database&lt;/strong&gt; User button, a modal will appear. Specify the new user's credentials, such as their username and password.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frah4md4l93whvwipls0t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frah4md4l93whvwipls0t.png" alt="Image 2" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To configure database privileges for the new user, scroll down the modal after adding the new user credential. Click the &lt;strong&gt;Add a Built-in Role&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tre08inkra3oz4xakn0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tre08inkra3oz4xakn0.png" alt="Image 3" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the appropriate role for that user from the selection list. To complete the process, click the &lt;strong&gt;Add User&lt;/strong&gt; button after adding the new user to the database.&lt;/p&gt;

&lt;p&gt;Note: Before saving, a role must be selected for the new user, else even if the user's credentials are valid, they will not be able to connect to the MongoDB cluster.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fecmxi7go39r0ak6iw28f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fecmxi7go39r0ak6iw28f.png" alt="Image 4" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we'll set up Network Access, which specifies where we may access our database from. As you can see, an IP address is already available when we set up our MongoDB cluster, we included the IP address from which we will access our database. If you have skipped that step when setting up your cluster or you want to add an IP address to access the database, click on the &lt;strong&gt;Add IP Address&lt;/strong&gt; option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7x99w6gt9z67yamkhpce.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7x99w6gt9z67yamkhpce.png" alt="Image 5" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After hitting the &lt;strong&gt;Add IP Address&lt;/strong&gt; button, a modal will appear allowing us to choose where we will access our database. Because we will be working locally at first, we can provide database access to our machine's current IP address, but once we deploy the web service, we must change the IP address to the host IP address, or we can choose &lt;strong&gt;Allow Access from Anywhere&lt;/strong&gt;, which I recommend using when working locally.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkg7bh0t4dmrd671bpvv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkg7bh0t4dmrd671bpvv.jpg" alt="Image 6" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To connect to our Node.js application, which we will be developing shortly, we need to retrieve our connection string.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Deployment&lt;/strong&gt; section of the Sidebar, click on &lt;strong&gt;Database&lt;/strong&gt;. This will take you to the MongoDB cluster that we have already created. To retrieve our connection string, we'll need to click the **Connect **button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2znkp5c2k70g2034pdn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2znkp5c2k70g2034pdn.png" alt="Image 7" width="800" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we click &lt;strong&gt;Connect&lt;/strong&gt;, a modal will appear. Since we want to connect to our application immediately and without stress, we will use the MongoDB native driver option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvl6iywrsdxjkoxhq5995.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvl6iywrsdxjkoxhq5995.png" alt="Image 8" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we'll choose the connection method we'll use:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Select the latest Node.js driver and version.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the command to install the driver package.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy and keep the connection string for connecting the Node.js app to the MongoDB cluster very close to you.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh05kzztw0wxjso3wdpx3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh05kzztw0wxjso3wdpx3.png" alt="Image 9" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have now successfully configured our MongoDB Cluster's network and database access, as well as obtained our connection string. We'll be building our Node.js application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the Development Environment for a Node.js application
&lt;/h2&gt;

&lt;p&gt;In this guide, we will use VSCode, but any text editor should work. To begin, let's create our Demo Node.js application and install the required dependencies. To make things easier, we'll divide this piece into 2 sections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Project Setup and Dependency Installation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a Basic Express Server&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Project Setup and Dependency Installation
&lt;/h3&gt;

&lt;p&gt;Open the terminal or command prompt on your machine and run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd Desktop

mkdir demo-app &amp;amp;&amp;amp; cd demo-app

touch app.js

npm init –y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the commands above correctly, your node.js application should look like this in VSCode.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywqwid2awnv3lbp0vt1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywqwid2awnv3lbp0vt1i.png" alt="Image 10" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a Basic Express Server
&lt;/h3&gt;

&lt;p&gt;To set up a basic express server for Node.js, type this 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 install express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Express framework helps developers develop APIs using Node.js. Once installed, Express should be present in the dependencies object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6k73haq0v6hitz3cku6y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6k73haq0v6hitz3cku6y.png" alt="Image 12" width="800" height="494"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For our server to run inside of the app.js file, we must import express, initialize it with a variable, create routes, and listen to port 5000.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");

const app = express();

app.get("/", (req, res) =&amp;gt; {
  res.status(200).send("Welcome To The Demo App");
});

const port = 5000;
app.listen(port, () =&amp;gt; {
  console.log(`Server Running on port ${port} `);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a next step, we'll add a new start script to the &lt;strong&gt;package.json&lt;/strong&gt; file to make it easy to execute the Demo-App project. The node app.js command restarts our server whenever we make changes to the file. However, we can fully avoid this by running our development server on Nodemon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Insert the following script into the 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 app.js"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;package.json&lt;/strong&gt; file should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjyuoluwt231wf782t60n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjyuoluwt231wf782t60n.png" alt="Image 13" width="800" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To see if everything is working properly, execute the project 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;Note: Always move to the root directory before running the server.&lt;/p&gt;

&lt;p&gt;The message in the console indicates whether or not the above command launches the express server, from what we can see our Express server is running.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcg32zz76sif64r29krnl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcg32zz76sif64r29krnl.png" alt="Image 14" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, if we go to the URL &lt;a href="http://localhost:5000/"&gt;http://localhost:5000/&lt;/a&gt; in our browser, we will get the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2lnqs3wpmriw3nifd47.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2lnqs3wpmriw3nifd47.png" alt="Image 15" width="740" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great! The project is set up and operating nicely. The next section will be to link our node.js application to our MongoDB cluster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting a Node.js application to a MongoDB cluster.
&lt;/h2&gt;

&lt;p&gt;We will now connect our MongoDB cluster to our Node.js application using the connection string we obtained before. To begin this phase, we will install the dependencies necessary for our project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install mongoose dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which will install the following packages:&lt;/p&gt;

&lt;p&gt;Mongoose: An ODM (Object Data Modeling) library for MongoDB.&lt;/p&gt;

&lt;p&gt;Dotenv: loads environment variable from a .env file.&lt;/p&gt;

&lt;p&gt;Create a &lt;strong&gt;.env&lt;/strong&gt; file to safely store sensitive information like the connection string. Open a terminal and 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;touch .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Always create this file in the root directory&lt;/p&gt;

&lt;p&gt;In the.env file, assign a variable to the MongoDB cluster connection string.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68oend6ixknk6w9mlull.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68oend6ixknk6w9mlull.png" alt="Image 16" width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up MongoDB Connection Function
&lt;/h3&gt;

&lt;p&gt;Next, create a folder called &lt;strong&gt;db&lt;/strong&gt;, and inside it, create a &lt;strong&gt;connect.js&lt;/strong&gt; file. We can also accomplish this in the terminal by 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;mkdir db &amp;amp;&amp;amp; cd db

touch connect.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to create a function in the &lt;strong&gt;connect.js&lt;/strong&gt; file that will establish a connection between our database and the node.js application so that we can access the MongoDB database on our express server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

const connectDB = (URL) =&amp;gt; {
  return mongoose.connect(URL);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We imported the Mongoose package into the code above. Next, we define a connectDB function that returns the connect() method in Mongoose, which is a Promise. The URL represents our connection string, which will be obtained from the &lt;strong&gt;.env&lt;/strong&gt; file. Finally, export the connectDB function, as we will need it in our entry file to configure our simple express server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating the Entry File of our Express Server
&lt;/h3&gt;

&lt;p&gt;Next, modify the &lt;strong&gt;app.js&lt;/strong&gt; file. Import the dotenv package to retrieve the values stored in our.env file. Inside app.js, write a function that will connect to the database and start our Express server only when the connection is successful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ADD CODE INTO app.js File (entry point)

require("dotenv").config();
const connectDB = require("./db/connect");

const startConnection = async () =&amp;gt; {
  try {
    await connectDB(process.env.MONGODB_URL);
    app.listen(port, () =&amp;gt; {
      console.log(`Server Running on port ${port} `);
    });
  } catch (error) {
    console.log(error);
  }
};
startConnection();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The startConnection function in the preceding code uses async/await since our connectDB function produces a Promise that might be resolved or denied. Once the asynchronous function is resolved and the database connection is made, our Express server will start automatically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqiocu7sysn711phv8p4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqiocu7sysn711phv8p4.png" alt="Image 17" width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you get the above message in your terminal after running your Demo-App project, you can be certain that you have linked and synchronized your database with your application, and that everything is working properly.&lt;/p&gt;

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

&lt;p&gt;Viola! We have successfully connected our MongoDB Cluster to our Node.js application (Demo-App). Now you can easily follow this process to connect your database to your node application. If you have found this helpful, please consider sharing it with others who might benefit.&lt;/p&gt;

&lt;p&gt;Thank you for Reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>express</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>MongoDB for Beginners: Setting Up Your Database with Atlas</title>
      <dc:creator>Zechariah Hounwanou</dc:creator>
      <pubDate>Thu, 29 Feb 2024 01:53:44 +0000</pubDate>
      <link>https://dev.to/codexive_zech/mongodb-for-beginners-setting-up-your-database-with-atlas-4k53</link>
      <guid>https://dev.to/codexive_zech/mongodb-for-beginners-setting-up-your-database-with-atlas-4k53</guid>
      <description>&lt;p&gt;Imagine your data as fixed columns. Every data has to fit into its exact spot. Old-school databases lock you into a super strict way of storing your data. Scaling up a database was painful, expensive, and time-consuming. All these were some of the problems before the creation of MongoDB.&lt;/p&gt;

&lt;p&gt;MongoDB is very powerful when used along with Node.js. It is open source and it also offers fast development and efficient performance for users. Nowadays everything has been moved to the cloud. We will learn about this cloud-based database called MongoDB.&lt;/p&gt;

&lt;p&gt;As a developer embarking on your first MongoDB project, this article will be a guide that will equip you with the knowledge on how to create a MongoDB Atlas account and how to set up a MongoDB Atlas Cloud-Cluster that can be used in any Node.js project you are working on or planning to work on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is MongoDB?&lt;/strong&gt;&lt;br&gt;
MongoDB Atlas is a cloud-based service that allows you to easily set up, manage, and scale your MongoDB database. It was created in 2009. MongoDB Atlas Cluster is a managed NoSQL database service. With MongoDB Atlas, you can access, and manage your MongoDB database through the web interface, command-line tool, or directly from your application and deploy your database in multiple regions around the world for low latency and high availability.&lt;/p&gt;

&lt;p&gt;When using MongoDB Atlas cluster you do not need to worry about the cloud service configurations, as it handles this for you under the hood without stress. There are paid options for the service but you can start with the Free Tier (which we are going to be starting with), and then scale up to a premium plan later on once your application needs increase.&lt;/p&gt;

&lt;p&gt;Below are the steps involved in creating a MongoDB account and a MongoDB cluster that you can use for your project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Create a MongoDB Atlas Account&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; Open up your browser and type &lt;strong&gt;“mongodb”&lt;/strong&gt; Google will provide you with sufficient results for your search but we are more concerned about the first website from the result page. Click on it will take you to the official website of MongoDB&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9wnngka0427c1xp83fl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9wnngka0427c1xp83fl5.png" alt="google results" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; After clicking on the link, you should be redirected to the official website of MongoDB. To create a MongoDB Atlas Account, you will need to click on the &lt;strong&gt;Try Free&lt;/strong&gt; Button in the top right corner to create an account for free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; No Credit Card info is required during account creation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv9638uveoxrscx5zoogt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv9638uveoxrscx5zoogt.png" alt="mongoDB landing page" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; After clicking on the &lt;strong&gt;Try Free&lt;/strong&gt; Button you will need to fill up your details in the Sign Up section or you can sign up utilizing your Google Account. I would recommend you take your time to fill in your appropriate information, acknowledge the Terms of Service and Privacy Policy, and then click on the button below to &lt;strong&gt;Create your Atlas Account&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmgy8wlr66dzad3v9k30d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmgy8wlr66dzad3v9k30d.png" alt="MongoDB registration form" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; MongoDB would like to verify the email address you registered with as soon as you create an account. So if you are seeing the screen below, you will need to head down to the mailbox associated with the email (you provided during registration) to verify your account by clicking the verification link in the email.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F664rutrk2kiad3j1tkzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F664rutrk2kiad3j1tkzc.png" alt="email verification" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; After you have successfully created and verified your MongoDB Atlas Account, you will be redirected to the next screen below. Here MongoDB would like you to answer a few questions (like what project you will be building, the technology involved, etc.) so they can tailor your experience based on it. You shouldn't have to spend much time answering this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmurrp409fiiog2yanxkp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmurrp409fiiog2yanxkp.png" alt="anwsering project questions" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After following through on the steps outlined above, your MongoDB Atlas Account is now ready for use without any hassle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Create a MongoDB Atlas Cluster&lt;/strong&gt;&lt;br&gt;
Now we need to create our Cloud-Cluster which will allow us to deploy, scale, and manage our database without the need for setting up and maintaining our infrastructure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; As a first step, we need to deploy the database we will be using for our project. From the available options, select the Free Tier (M0 Sandbox), which will provide you with limited resources ideal for development and testing purposes. Next, choose a Cloud Provider from AWS, Google Cloud, or Azure, and select the region closest to your location to ensure optimal performance. Provide a name for your cloud cluster, and then click on the &lt;strong&gt;Create&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftn342lb2dm2pqsanxxuh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftn342lb2dm2pqsanxxuh.png" alt="cluster creation" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Now that you have created the Free Cloud-Cluster that you will be using for your project, you will be redirected to this section and you will need to provide the username and password you will be using for the Cloud-Cluster you have just created.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You need to use details you will be able to remember easily because during development you will need to include the username and password in your connection string so that your Node.js app can communicate with your cluster.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhf31wlah9oonglfcne0j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhf31wlah9oonglfcne0j.png" alt="cluster credential creation" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; After adding your cloud-cluster username and password, you will also need to connect your machine IP address to the cluster you have created. To add your IP address click on Add My Current IP Address and automatically it will pick and insert the IP address of your machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcu9qq0smwy8on41tq4tj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcu9qq0smwy8on41tq4tj.png" alt="selecting IP address" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After you have successfully done all of the above-outlined processes, you can click on the &lt;strong&gt;Overview&lt;/strong&gt; on the sidebar and you will land on this page. You should see the name of the cluster you have created in the Database Deployment section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft91431l9cxtydc5x86cz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft91431l9cxtydc5x86cz.png" alt="succesful cluster setup" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have successfully created a free MongoDB Atlas account and Cloud-Cluster for your project. You can find more information on &lt;a href="https://www.mongodb.com/docs"&gt;their official documentation.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>mongodb</category>
      <category>mongoose</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Solving FizzBuzz Problem in JavaScript</title>
      <dc:creator>Zechariah Hounwanou</dc:creator>
      <pubDate>Mon, 17 Apr 2023 08:00:39 +0000</pubDate>
      <link>https://dev.to/codexive_zech/solving-fizzbuzz-problem-in-javascript-1ae4</link>
      <guid>https://dev.to/codexive_zech/solving-fizzbuzz-problem-in-javascript-1ae4</guid>
      <description>&lt;p&gt;We are going to play a game called FizzBuzz, Have you played the game before, if you havent no need to worry I will show you how. I can tell you for free that afterward you would love to play it and write a program code for it in your favorite programming language, its so enticing.&lt;/p&gt;

&lt;p&gt;In this game we are going to start by counting from 1 and saying each number out loud but here is where the fun comes in when we get to a number that is a multiple of 3, instead of saying that number itself we say the word Fizz! And when we get to a number that is a multiple of 5 we say the word Buzz! if a number is then a multiple of both 3 and 5 we say the word FizzBuzz! cool right? I know.&lt;/p&gt;

&lt;p&gt;My preferred language is Javascript so let's write a program to play the game using my language. Any programming will do the trick but I choose to use JS.&lt;/p&gt;

&lt;p&gt;First, we need to tell the computer that we want to start the counting of numbers from 1. We can do that by creating a variable called count and setting it up with the value of 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let count = 1

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

&lt;/div&gt;



&lt;p&gt;Next, we need to write out the logic that will make our program say the numbers and FizzBuzz when appropriate. We can use a loop to go through all the numbers from 1 which is the count value to 100 (or any other number we wish to play up to). Inside the loop, we want to use an IF statement to check if the current number is a multiple of 3, 5, or both. If it is we can then use the console.log command to make the program say either Fizz, Buzz or FizzBuzz. Now let's write the code down&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(let count = 1; count &amp;lt;= 100; count++){
    if(count % 3 === 0 &amp;amp;&amp;amp; count % 5 === 0){
        console.log("FizzBuzz");
    } else if (count % 3 === 0){
        console.log("Fizz");
    } else if (count % 5 === 0){
        console.log("Buzz");
    } else {
        console.log(count);
    }
}

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

&lt;/div&gt;



&lt;p&gt;Our code is ready to be shipped by the power in 0s and 1s. Now when we run this program, our computer will play the FizzBuzz game for us! We can change the number we wish to play with by changing the number in the loop. For example, if we want to play up to 50, we can change the loop to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(let count = 1; count &amp;lt;= 50; count++){
    //FizzBuzz Logic as above goes here
}

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

&lt;/div&gt;



&lt;p&gt;And thats it, my fellow developers, we have just created a FizzBuzz game program that we can play using JavaScript. Have fun coding!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
