<?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: Emeka Okezie</title>
    <description>The latest articles on DEV Community by Emeka Okezie (@staunchcfo).</description>
    <link>https://dev.to/staunchcfo</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%2F497883%2Fb52648de-4cfe-4ad8-9c7e-7c84ef023892.jpeg</url>
      <title>DEV Community: Emeka Okezie</title>
      <link>https://dev.to/staunchcfo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/staunchcfo"/>
    <language>en</language>
    <item>
      <title>Build a REST API with Node, Express MongoDB &amp; Postman!</title>
      <dc:creator>Emeka Okezie</dc:creator>
      <pubDate>Mon, 26 Oct 2020 02:41:37 +0000</pubDate>
      <link>https://dev.to/staunchcfo/build-a-rest-api-with-node-express-mongodb-post-man-49ca</link>
      <guid>https://dev.to/staunchcfo/build-a-rest-api-with-node-express-mongodb-post-man-49ca</guid>
      <description>&lt;p&gt;In this project, we will be creating a simple API that would allow people Update, Delete, Create, and Subscribe to a social media account. We will do this by coding the data the users will be interacting with within our social media database and how it goes about manipulating them depending on what a user wants to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Requirements and Dependencies
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Important Prerequisite:&lt;/strong&gt; Make sure you already have MongoDB installed and set up on your machine before starting this tutorial. Here is a link to a guide that MongoDB provides on their website: &lt;a href="https://docs.mongodb.com/manual/administration/install-community/" rel="noopener noreferrer"&gt;MongoDB Installation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After installing MongoDB, the next step would be the opening of your command line, then create a new directory for the project we will be working on using this command &lt;code&gt;mkdir directory_name&lt;/code&gt; this would quickly create your new directory, then you would need to be in the newly created directory by using the following command &lt;code&gt;cd directory_name&lt;/code&gt; this would give you access to the directory you have created.&lt;/p&gt;

&lt;p&gt;In this project, we would be working with the &lt;code&gt;express generator&lt;/code&gt; to quickly create our application skeleton. For earlier Node versions, install the generator as a global npm package and then launch it using the following command &lt;code&gt;npm install -g express-generator&lt;/code&gt;. This would automatically install the &lt;code&gt;express generator&lt;/code&gt; globally on your local machine, after installation, you would be required to type &lt;code&gt;express&lt;/code&gt; in your command line, this will create all the required files to set up your express app skeleton&lt;/p&gt;

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

&lt;p&gt;After creating our express app, you would be required to delete the &lt;code&gt;views and public&lt;/code&gt; folders as well as the &lt;code&gt;bin&lt;/code&gt; folder, since all we are creating is a simple REST-API, we won't be needing to make use of them, also, we will be required to take out the line of codes for the view engine set up and the middleware for joining the public directory to our app.js file.&lt;/p&gt;

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

&lt;p&gt;Next, we will be installing &lt;a href="https://mongoosejs.com/" rel="noopener noreferrer"&gt;Mongoose&lt;/a&gt;, which connects our application to the MongoDB Database using this command &lt;code&gt;npm install mongoose&lt;/code&gt; then &lt;code&gt;dotenv and nodemon&lt;/code&gt; for setting up our environment variables and restarting our local server respectively, after we saved our files with the &lt;code&gt;.JS&lt;/code&gt; extension. this will be installed using the following command &lt;code&gt;npm install --save-dev dotenv nodemon&lt;/code&gt;.&lt;br&gt;
&lt;em&gt;Note: --save-dev from above will save development-only dependencies without saving them to production&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dotenv&lt;/code&gt; will allow us to pull in environment variables from a &lt;code&gt;.env&lt;/code&gt; file. When we ran the &lt;code&gt;express&lt;/code&gt; command earlier it created a &lt;code&gt;package.json&lt;/code&gt; file. In this file under the scripts, we want to replace that ‘test’ script with our own script that starts our server with &lt;code&gt;nodemon&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%2Fi%2Fojc9kc8g19wfacvovw1r.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fojc9kc8g19wfacvovw1r.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up Our Local Server
&lt;/h1&gt;

&lt;p&gt;Running the &lt;code&gt;express&lt;/code&gt; command in the command line, after installing &lt;code&gt;express-generator&lt;/code&gt; automatically created an &lt;code&gt;app.js&lt;/code&gt; file. Once created, we will need to require all our packages at the top of our &lt;code&gt;app.js&lt;/code&gt; file, for the file created using the &lt;code&gt;express&lt;/code&gt; command, you will already have the express dependency declared at the top of the &lt;code&gt;app.js&lt;/code&gt; file, all you will need to do is require the &lt;code&gt;dotenv and mongoose&lt;/code&gt; packages.&lt;/p&gt;

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

&lt;p&gt;When we want to test and make sure our server is working, we will run this function to listen on port 3000 and log a string if it's successful: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.listen(3000, () =&amp;gt; console.log('server started'))&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%2Fi%2F0kwkktxgiv4jzaoc4ulw.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0kwkktxgiv4jzaoc4ulw.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Connecting to Our Database
&lt;/h1&gt;

&lt;p&gt;Before running the database connection, first, you will be required to pull in the &lt;code&gt;dotenv&lt;/code&gt; package you had initially installed, just the same way you did for express and mongoose like this &lt;code&gt;const dotenv = require('dotenv').config()&lt;/code&gt;. The &lt;code&gt;.config()&lt;/code&gt; function automatically configures the &lt;code&gt;dotenv&lt;/code&gt; package to be used in your application entry point.&lt;br&gt;
This next chunk of code in our &lt;code&gt;app.js&lt;/code&gt; file starting with &lt;code&gt;const CONFIG&lt;/code&gt; is how we will be connecting to the database. I’ll explain it line by line:&lt;/p&gt;

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

&lt;p&gt;The &lt;code&gt;OPTIONS&lt;/code&gt; object is actually optional for the purpose of this API, &lt;code&gt;mongoose.connect(CONFIG.uri, CONFIG.OPTIONS)&lt;/code&gt; allows us connect to the database, you may be wondering the &lt;code&gt;DATABASE_URL&lt;/code&gt;, this is the location where we want to store our database, which has been identified in the &lt;code&gt;.env&lt;/code&gt; file created in the root folder of our API. If you haven't created a &lt;code&gt;.env&lt;/code&gt; file, now will be a good time to go about this and set the &lt;code&gt;DATABASE_URL&lt;/code&gt; there:&lt;/p&gt;

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

&lt;p&gt;Moving on, we have to set &lt;code&gt;db&lt;/code&gt; to &lt;code&gt;mongoose.connection&lt;/code&gt; as a syntactically easier way to reference our database. Next, we’ve defined what happens when the database connection is successful, which is to log the string of connection to the database on our terminal.&lt;/p&gt;

&lt;h1&gt;
  
  
  Testing the Database Connection
&lt;/h1&gt;

&lt;p&gt;At this point, we can now test out our connection to the database to make sure everything is working smoothly. In our terminal, let’s go ahead and start our database by typing mongod and then in another terminal tab let’s start our server with &lt;code&gt;npm run dev&lt;/code&gt;. If all went well, we should get these messages after we start our 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%2Fi%2Fxf4pqioibtjy8u0sseht.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxf4pqioibtjy8u0sseht.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cool! We’ve successfully started our server and our database appears to be operational.&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up our Server to Accept JSON
&lt;/h1&gt;

&lt;p&gt;In our same app.js file we want to tell &lt;code&gt;Express&lt;/code&gt; that it should accept JSON. Lets put this line of code in between our ‘database is open’ line of code and the ‘listen on port 3000’ line:&lt;/p&gt;

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

&lt;p&gt;The &lt;code&gt;.use&lt;/code&gt; is a middleware that allows you to run code when the server gets a request but before it gets passed to your routes. So in this instance, we’re telling &lt;code&gt;Express&lt;/code&gt; to accept JSON as the data format.&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating our Route File
&lt;/h1&gt;

&lt;p&gt;Inside our directory, you will notice you already have a &lt;code&gt;routes&lt;/code&gt; folder in it, we also have an &lt;code&gt;index.js&lt;/code&gt; file, this is where we will define how the server should handle the data when it receives a GET, POST or PATCH/UPDATE request.&lt;br&gt;
But before we start creating those, let's switch back to our app.js file and tell the server that we now have routes that it needs to handle and use. These next two lines of code are actually the last ones we need for our app.js file.&lt;br&gt;
&lt;code&gt;var indexRouter = require('./routes/index');&lt;/code&gt;&lt;br&gt;
&lt;code&gt;app.use('/', indexRouter);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here’s the full server.js file with the new lines of code added:&lt;/p&gt;

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

&lt;p&gt;If something is missing or incorrect, right now is a great checkpoint to make sure you’ve got everything updated with the above. Give yourself a pat on the back for getting this far! We’re getting onto the fun stuff next…&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up Our Routes
&lt;/h1&gt;

&lt;p&gt;Now that our server file is all set up, we will now switch gears to our new &lt;code&gt;index.js&lt;/code&gt; routes file. To start, let us require &lt;code&gt;Express&lt;/code&gt; and define the &lt;code&gt;express.router()&lt;/code&gt; function on a variable called router. While we’re at it let’s add our module export function as well at the very bottom:&lt;/p&gt;

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

&lt;p&gt;Before we get into the code, let’s get an understanding of what we exactly want to do with our routes. Since we’re trying to see and artificially create new subscribers on Social App, we need this set of criteria:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Route for getting all subscribers&lt;br&gt;
Route for getting one subscriber&lt;br&gt;
Route for creating one subscriber&lt;br&gt;
Route for updating one subscriber&lt;br&gt;
Route for deleting one subscriber&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let’s start each of the bullet points above with the basic framework depending on if its a GET, POST, or PATCH request:&lt;/p&gt;

&lt;p&gt;To get this fully set up, we could be creating this API using the MVC method without a &lt;code&gt;View&lt;/code&gt;, by this, I mean since it's just an API we won't be needing to render page here. We will be creating a &lt;code&gt;Controller&lt;/code&gt; folder which will handle all of our routing functions, then we will be exporting these functions to our &lt;code&gt;index.js&lt;/code&gt; file in the &lt;code&gt;Routes&lt;/code&gt; folder. First, you should have these lines of code in your &lt;code&gt;index.js&lt;/code&gt; folder for your routing.&lt;/p&gt;

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

&lt;p&gt;Ignore all the errors, for now, we will sort out all of those very soon, you may go ahead in creating your controller folder and an &lt;code&gt;indexController.js&lt;/code&gt; file.&lt;br&gt;
The one thing that might look weird is the PATCH method. The PATCH method is being used here instead of the more traditional PUT method because we only want to use this to update pieces of the subscriber’s information and not all the information of the subscriber.&lt;br&gt;
You also may have noticed that we include &lt;code&gt;/:id&lt;/code&gt; in most of our parameters. This is for the routes that are requesting an action to be taken on a single subscriber thus we need that subscriber’s unique ID.&lt;/p&gt;

&lt;h1&gt;
  
  
  Testing Our IndexController File
&lt;/h1&gt;

&lt;p&gt;In our &lt;code&gt;indexController.js&lt;/code&gt; file we will be writing a chunk of code, this is with the aim of testing our API to see if it's well connected using our routing. I will be needing to explain the code below, later on, right now our routing functionality is now been declared in the &lt;code&gt;indexController.js&lt;/code&gt; file then export to the &lt;code&gt;index.js&lt;/code&gt; file in the router folder&lt;/p&gt;

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

&lt;p&gt;Below is where the &lt;code&gt;indexController.js&lt;/code&gt; file is being imported into the routing file.&lt;/p&gt;

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

&lt;p&gt;What the above block of code means is this, in the &lt;code&gt;indexController.js&lt;/code&gt; file, we used a class-based method in exporting all our functions, then we created an arrow function called &lt;code&gt;testingRoute&lt;/code&gt; then assigned a response and require argument to it, afterward, we sent a simple JSON message &lt;code&gt;'Hello World'&lt;/code&gt; using the &lt;code&gt;res.send()&lt;/code&gt; command in a try and catch block. What this does is to try the initial command in this function, in case there is an error, the catch block prints the error in the console, after this is done, we then export the functions inside the &lt;code&gt;Class&lt;/code&gt; to the router file.&lt;br&gt;
In the &lt;code&gt;index.js&lt;/code&gt; file we then require the &lt;code&gt;indexContoller.js&lt;/code&gt; file and assigned it to a new variable called &lt;code&gt;IndexController&lt;/code&gt;,  then using the &lt;code&gt;GET&lt;/code&gt; method, we will be calling out the content of the &lt;code&gt;indexController.js&lt;/code&gt; as shown in the code above.&lt;/p&gt;

&lt;p&gt;Now for testing our API, I will be making use of Postman, you can download it from their official page &lt;a href="https://www.postman.com/downloads/" rel="noopener noreferrer"&gt;Postman Installation&lt;/a&gt;. After you are done installing, open the Postman app, you will get the same page:&lt;/p&gt;

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

&lt;p&gt;All you will be required to do is test the GET call to our Server&lt;br&gt;
&lt;code&gt;http://localhost:3000&lt;/code&gt; using the Postman app, just click on the &lt;code&gt;+&lt;/code&gt; icon on the app to access a new tab, then copy this URL and paste in the input field, first you need to select the action as a GET request, then hit the Send button, this would print the &lt;code&gt;'Hello World'&lt;/code&gt; text we sent through the &lt;code&gt;indexController.js&lt;/code&gt; file as shown below: &lt;/p&gt;

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

&lt;p&gt;This means our API is working correctly! Ok so we’ve done a lot so far, let’s take a hydration break, and then we’ll get started with models.&lt;/p&gt;

&lt;h1&gt;
  
  
  Making the Models
&lt;/h1&gt;

&lt;p&gt;Let’s go ahead and set up our model and the schema inside of our model. A schema is how our app defines what the data looks like and also sets up the document in MongoDB. If that sounds confusing, it’ll make more sense once we see what’s going on.&lt;/p&gt;

&lt;p&gt;Let’s first start by creating a new folder called models. Inside of this folder, let’s create a new file called subscriber.js. The idea is that the model will handle how each and every subscriber, on an individual level, will look inside of our database. The ‘index’ routes handle sometimes multiple subscriber's requests such as Get All Subscribers route. It’s an important thing to mention as verbiage is important when naming files in projects.&lt;br&gt;
In this new file, we want to first require Mongoose since we will be using their schema models:&lt;br&gt;
&lt;code&gt;const mongoose = require('mongoose')&lt;/code&gt;&lt;br&gt;
After we require mongoose, we’ll start by defining our schema&lt;br&gt;
&lt;code&gt;const mongoose = require('mongoose')&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const subscriberSchema = new mongoose.Schema({})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Inside of this javascript object will be all of the keys for the different properties of our subscriber. These keys will include &lt;code&gt;name, subscribedChannel and subscribeDate&lt;/code&gt;. We essentially want to tell our database what to expect from each one of these keys such as their type, if they’re required and if a default value should be applied.&lt;/p&gt;

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

&lt;p&gt;The type and required properties should be pretty self-explanatory. These are defining the expected schema type (a String and Date in this case) as well if that key is required upon entering information for a new subscriber.&lt;/p&gt;

&lt;p&gt;One thing to note about &lt;code&gt;subscribeDate&lt;/code&gt;, we set the type to Date instead of String since we will be expecting a date from the user. If no date is provided then we default it to the current date by using &lt;code&gt;Date.now&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Moving on, the last line of code we want to write in our schema is the module.exports. This will allow us to use and interact with the database using our schema. Mongoose has a special way of exporting models utilizing mongoose.model() that takes two properties:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;module.exports = mongoose.model('Subscriber', subscriberSchema)&lt;/code&gt;&lt;br&gt;
‘Subscriber’ is the name we want to give the model in our database and then the next one is the schema that corresponds to that model which is our subscriberSchema.&lt;/p&gt;

&lt;p&gt;That’s it for our model! Let’s take a look at the finished schema to make sure we’ve got it all:&lt;/p&gt;

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

&lt;h1&gt;
  
  
  Creating and Getting Subscribers
&lt;/h1&gt;

&lt;p&gt;Now that we have our model setup with a schema for our database, let’s require it in our indexController.js controller file below where we required router&lt;br&gt;
&lt;code&gt;const Subscriber = require('../models/subscribers')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have our test response from earlier when we sent ‘Hello World’ to the server but we can go ahead and delete that since we want to actually tell that route to Get All Subscribers.&lt;/p&gt;

&lt;p&gt;The first thing we’ll need to do after we get rid of our old res.send('Hello World') line in the &lt;code&gt;indexController.js&lt;/code&gt; file, is wrap the function in a promise with a try/catch statement, just like we already did previously:&lt;/p&gt;

&lt;p&gt;Inside of our try statement we want to get all of the subscribers from our model. So we want to set a new variable called subscribers to that model with a .find() method applied to our Subscriber model.&lt;/p&gt;

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

&lt;p&gt;As the name implies, the find() Mongoose method works by returning all associated subscriber objects that meet its criteria. Since we’re returning all subscribers, we just leave the parentheses blank since we want all the subscribers:&lt;/p&gt;

&lt;p&gt;After that line, we then want to send a response with the data of our subscribers variable we just created in the form of JSON. Finally, in our catch statement we want to just catch any errors that may occur and have it sent to the user as a 500 error in JSON format:&lt;/p&gt;

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

&lt;p&gt;Now that we have our route to send us all the subscribers in our database, we need to code a way for us to actually add a subscriber into our database. So, lets move onto our Create One Subscriber controller so we can enter data about a subscriber:&lt;/p&gt;

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

&lt;p&gt;You can see it's somewhat similar to our Get All Subscribers controller except for a few important differences. For one, we’re no longer doing a GET call on our database but a POST which will allow us to push data to our database.&lt;/p&gt;

&lt;p&gt;On this line:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const subscriber = new Subscriber({...&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we are creating a variable that will be assigned to a new Subscriber from our model that we created earlier. If you recall, we require a &lt;code&gt;name, subscribedChannel and subscribeDate&lt;/code&gt; properties for a new subscriber.&lt;/p&gt;

&lt;p&gt;These next two lines of code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name: req.body.name,&lt;br&gt;
subscribedChannel: req.body.subscribedChannel&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We’re telling our controller to save the request made from a user’s input of a new Subscriber name property and &lt;code&gt;subscribedChannel&lt;/code&gt; property. Our subscribeDate doesn’t need to be defined because it will default automatically to the date/time that this database entry is made.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;try and catch&lt;/code&gt; statements should look familiar. We’re instead using a &lt;code&gt;.save()&lt;/code&gt; Mongoose method instead of &lt;code&gt;find()&lt;/code&gt; because this is how we will tell the database that we want it to hold the information a user passes to us through this controller function.&lt;/p&gt;

&lt;p&gt;Lastly:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;...&lt;br&gt;
res.status(201).json(newSubscriber)&lt;br&gt;
} catch (err) {&lt;br&gt;
res.status(400).json({ message: err.message })&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We’re sending the user response with a success status of 201 and to pass our new subscriber back to us as JSON. The catch is just like our Get All Subscribers controller except we pass a 400 error since this would be a user error for passing us bad data.&lt;/p&gt;

&lt;p&gt;To test all of this, in our &lt;code&gt;index.js&lt;/code&gt;route file, after requiring the &lt;code&gt;indexController.js&lt;/code&gt; file, for the Get all Subscribers route, this would be written out like this &lt;code&gt;router.get('/', IndexController.getAllSubcribers)&lt;/code&gt; since it is a get request to all the Subscribers, for the create a single Subscriber, since it is a POST action &lt;code&gt;router.post('/create', IndexController.createSingleSubcriber)&lt;/code&gt;, mind you at this point you have your IndexController called up in the &lt;code&gt;index.js&lt;/code&gt; file like we do in the code below:&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;...&lt;br&gt;
IndexController.getAllSubcribers and &lt;br&gt;
IndexController.createSingleSubcriber&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is just a way of targeting the functions inside the &lt;code&gt;indexControler.js&lt;/code&gt; file, remember we recently declared these two functions to get all subscribers and create a single subscriber, we do this by appending the functions of the &lt;code&gt;indexController.js&lt;/code&gt; file to the &lt;code&gt;IndexController&lt;/code&gt; variable which was declared in the route file &lt;code&gt;const IndexController = require('../controller/indexController')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now to actually test all we have done, we will be create a new subscriber using the &lt;code&gt;create&lt;/code&gt; route we just created, remember it is a POST action. we will do this by typing the following url in our Postman application. &lt;code&gt;http://localhost:3000/create&lt;/code&gt;, the &lt;code&gt;/create/ path was declared in our&lt;/code&gt;index.js` route file. Before hitting the create route, we need to do a quick set up in Postman to be able to pass in raw JSON data&lt;/p&gt;

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

&lt;p&gt;First, we need to select the &lt;code&gt;Body(in green)&lt;/code&gt;, then the raw option, afterward we then select JSON from the drop-down option, this would make POSTMAN know we are about passing a JSON data,&lt;br&gt;
Once you do this as shown above you can then manually type this in the same format in the input space provided in POSTMAN&lt;br&gt;
&lt;code&gt; {&lt;br&gt;
  "name": "Robert",&lt;br&gt;
  "subscribedChannel": "Bennetts Channel"&lt;br&gt;
} &lt;/code&gt;&lt;br&gt;
If after you are done with this and hit the submit button, you should get this output:&lt;/p&gt;

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

&lt;p&gt;We just created a new user, the user now has his own unique ID, If everything went well, our response when we click ‘Send Request’ should look like the above. We can see that we received our 201 success status at the very top along with our Subscriber object at the bottom with all the information we just plugged in.&lt;/p&gt;

&lt;p&gt;Again, subscribeDate is set automatically to the date of creation so we don’t need to manually set it.&lt;/p&gt;

&lt;p&gt;At this point, we can easily get the number of subscribers in our database by just hitting the &lt;code&gt;http:localhost:3000/&lt;/code&gt; URL, but this time with a GET action, after hitting the send button, POSTMAN would print out all SUbscribers in the Database.&lt;/p&gt;

&lt;h1&gt;
  
  
  Get Single User
&lt;/h1&gt;

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

&lt;p&gt;The first line on this code looks pretty familiar already, in the &lt;code&gt;try and catch&lt;/code&gt; block, we used a MongoDB method of &lt;code&gt;findById&lt;/code&gt; to target a particular ID from our Database, where &lt;code&gt;(req.params.id)&lt;/code&gt; The req.params property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /student/:id, then the “id” property is available as req.params.id. This object defaults to {}. Now, we are mapping through the Subscriber Database to search for a particular ID,&lt;code&gt;findUser == null&lt;/code&gt; implies that if the subscriber is not in the Database, the next command in the function is to return a status code of &lt;code&gt;Error 404&lt;/code&gt; with the message of &lt;code&gt;'Cannot find the subscriber'&lt;/code&gt; else &lt;code&gt;res.status(201).json(findUser)&lt;/code&gt; simply implies that the subscriber found should be printed in JSON with the status code of 201, which means that everything is working well.&lt;br&gt;
At this point your &lt;code&gt;index.js&lt;/code&gt; route file should be looking 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%2Fi%2Fckrzpucs9ik4jooa4n7n.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fckrzpucs9ik4jooa4n7n.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get a single Subscriber, we need to first GET all the Subscribers, using the URL &lt;code&gt;http:localhost:3000/&lt;/code&gt; in your POSTMAN, then copy a Subscriber's unique ID, after doing this you can now confirm if your function is working well by hitting this link &lt;code&gt;http:localhost:3000/single/theUserId&lt;/code&gt; this should get you the single User with the status code of 201.&lt;/p&gt;

&lt;h1&gt;
  
  
  Delete Subscriber
&lt;/h1&gt;

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

&lt;p&gt;Just the same way we did for the GET single Subscriber by using the MongoDB command of &lt;code&gt;findById&lt;/code&gt;, in this case, we had to do a little different thing, after finding the users by their ID using &lt;code&gt;const findUser = await Subscriber.findByIdAndDelete(req.params.id)&lt;/code&gt; the next action was confirmed is the User/Subscriber really exist in the Database, if so, remove this user using the following command &lt;code&gt;Subscriber.remove()&lt;/code&gt; where Subscriber is the model the user is located and remove() is a MongoDB function to remove a data from the database, after you are done, your code should look just like what we have above, if the subscriber is not in the Database, the next command in the function is to return a status code of &lt;code&gt;Error 404&lt;/code&gt; with the error message. After this has been done successfully, your delete route in the &lt;code&gt;index.js&lt;/code&gt; route file should and update and would 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%2Fi%2Fnnc0xt40rg812r7furlh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnnc0xt40rg812r7furlh.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also if you try deleting a Subscriber, after selecting their ID on POSTMAN, you should get this result too:&lt;/p&gt;

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

&lt;p&gt;--NB: This has got to be a DELETE action also like you did for GET to get all Subscribers or single Subscriber--&lt;/p&gt;

&lt;h1&gt;
  
  
  Patch/Update a Subscriber
&lt;/h1&gt;

&lt;p&gt;Our Update Subscriber route is the very last thing we need to write for this application to be fully functional! Ok so before we get into the code lets get a general idea of how updating will work in this case:&lt;/p&gt;

&lt;p&gt;User updates just the name&lt;br&gt;
User updates just the channel&lt;br&gt;
User updates both name and channel&lt;br&gt;
Or they mess up and it throws an error&lt;br&gt;
Our requirements need for us to essentially check and see if any changes were made and if so, update them appropriately. Now onto the code:&lt;/p&gt;

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

&lt;p&gt;Using the same method as wit the previous, the first line of code remains familiar, &lt;/p&gt;

&lt;p&gt;&lt;code&gt;...&lt;br&gt;
const updateUser = await Subscriber.findByIdAndUpdate(req.params.id , {&lt;br&gt;
            name : req.body.name,&lt;br&gt;
            channel : req.body.subscribedChannel&lt;br&gt;
        }&lt;/code&gt;&lt;br&gt;
In this case, we are using a method of findByIdAndUpdate, to map through the Database, and then if the particular that has been inputted is actually in the Database if so, we are targeting the &lt;code&gt;name and subscribedChannel&lt;/code&gt; from the &lt;code&gt;Subscriber&lt;/code&gt; model, and either update both fields or any one of them, if this operation is okay, print the &lt;code&gt;JSON&lt;/code&gt; message. &lt;br&gt;
If your code is correct and is the same as the one above, the next operation would be updating your &lt;code&gt;index.js&lt;/code&gt; route file, which would finally 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%2Fi%2Fvc0m3n3og2u9szb3okkv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvc0m3n3og2u9szb3okkv.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Afterward. we can now test this route with the POSTMAN. first, we will get a single user using his Id and this link,&lt;code&gt;http:localhost:3000/single/5f967dedbab34829a4eb83ee&lt;/code&gt;. this will be a GET request, after we get this User, and update his/her record, we can then PATCH the user using this URL &lt;code&gt;http:localhost:3000/update/5f967dedbab34829a4eb83ee&lt;/code&gt; and his the Send button, we will get the result below:&lt;/p&gt;

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

&lt;p&gt;You can confirm that the User details really got updated by either get just that user again or getting all the users as shown below:&lt;/p&gt;

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

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

&lt;p&gt;Guys, we made it. I really hope this tutorial was helpful to some of you. We went over a lot of stuff so if you’re feeling overwhelmed then that's totally understandable. But realize we just made a pretty awesome backend piece of code that translates into so many different real-world applications. So big props to you for making it through it all!&lt;/p&gt;

&lt;p&gt;The complete code can be found in the Master branch of my &lt;a href="https://github.com/staunchCFO/simple-API.git" rel="noopener noreferrer"&gt;GitHub repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you ever got stuck or found something worth mentioning, go ahead and drop a message on &lt;a href="https://twitter.com/staunchCFO" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or leave me a comment below.&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>mongodb</category>
      <category>api</category>
    </item>
  </channel>
</rss>
