CRUD API Tutorial – Node, Express, MongoDB

**Adding Juice to the API**

I can see that juice is added. I mean nothing happened. It's everything is working okay, so if if juice is added, can we view all these? Then just copy this and you could actually view it in their browser as well. So if you do this, you could see that all these lists are working. I mean it didn't basically impact the API performance of pancake doughnut cheesecake chocolate and juice have all been there. Nothing is being broken and nothing seems to break everything seems to work. You could see if I want to um add uh this is the ad so get API so if I just send you can see now all of these things are being populated here. If you want to you know delete anything suppose I don't really want you know cheesecake. I could just get this ID first of all let's copy this and we can actually update the ID as well right now try to update Pizza it's not going to allow you because uh it can't find it 404 right. We could put a really good message over there and that's something that I want you to put a message using you know response but right now you can see that this is the now I changed it to the updated uh so cheesecake so cheese I'll just write it and then the price and then I press okay. So now over here we have slash API products now this is important now whenever when I you know I did the organized way to do the route and controller I changed all of them from now so everything has Now a/ API SL products now if I do this you can see that the cheesecake has been updated and you could view this uh cheesecake by using get API and this would uh basically send it and you could see over here pancake donut updated cheese okay and then we have chocolate. If you want to delete the cheesecake uh you could just have this you know from here with the ID passed in but again the product will be changed to products and this is the delete method. Now if I press send you can see product deleted successfully and if I want to review all the products so I'll do here and I press send again and you can see pancake donut chocolate and juice.

**Adding Code to GitHub**

One final thing I wanted to do was to add this into GitHub so this code that I added I wanted all of this to be populated on GitHub. So what we could do is we could go to github.com so what we could do is essentially I'll go to my GitHub account so and then over here we have this plus button and create a repository and we'll you know we'll call it simple uh so we'll say simple crud app and we'll say backend okay and then over here we'll have it public. We'll create repository and then over here we could just initialize using this command. Make sure you have that so what you could do is this is the uh directory here you could just go and do CMD this opens up the command prompt you could have the following uh commands typed so first of First Command is I would uh you know I'll just turn off this because we don't need this for now. And now what we're going to do is we could just go here and uh first man so we could go here okay and what we could do first is we could have git in it which is initializing the git um and then after that we have get add all the files so we just say get add everything okay now I didn't have to do slash but dot okay. So this means I added everything then after I do get commit i m and I say first commit all right so you could just do any message you want I just did okay now after that we want get Branch hyphen M and then hyphen M and then we have main branch okay so now this is on the main branch. Then I want to get remote uh add origin and I have to put in the URL of that repo so basically just copy this uh and you have to put it in the URL path here so right there so get remote add origin and then once you have everything finalized you just do get push hen new origin and then main if you do this and press enter this would update this on GitHub so now you could see that everything is on GitHub which is great. And you could see one other thing that node modules isn't existing here and the reason why I did that was because I put it in this uh you know get ignore and you can see that get ignore didn't include the node modules which is pretty heavy and bulky and you don't want that and you could also put in your you know EnV variables your credentials and all these stuff we could go in the uhv file and then you could put in the EnV in the. Ignore so yeah these are the things um and precautions I'll put this link into the uh description so thank you so much for watching

"WEBVTTKind: captionsLanguage: enthis is a great beginner's course to learn the basics of backend development by building a crud API with node Express and mongodb basically the M Stack without the r Harris ifar created this course he is a popular instructor and experienced developer today we're going to be building a complete restful crud API from scratch using no JS Express and mongod DB so over here we're in Visual Studio code make sure you have it downloaded I already have have it and you also make sure you have node.js installed so just go to nodejs.org and install this latest support version and then just click on all the installation steps and once you have it you could test it out using CMD and just type in the following you could just say node hyphen hyphen version and this should tell you whether you have node installed or not and now once we have everything ready we can now go into our project so essentially what I'm going to do is I'm going to create a simple crud app folder over here and in here where we're going to do is we're going to open up CMD so just go here and type in CMD and go in this and you could just type in code and Dot this would open up into that same directory so now we have everything ready okay so now what we're going to do first of all here is I'm going to zoom in a little and I'm going to actually uh do the following first of all uh what we're going to do is we're going to open up the terminal using control and back tick is the shortcut key and over here you could have the command specified so the first one you want to do is you want to type in init hyphen y so npm in it hen y to initialize uh some package.json now you could see that this has been written and if you open up you could see that this is the package.json file over here and it has the name which is simple crud app it got from my folder name so you can see over here after that you could see if I put it on the right side over here and just open this up um or maybe my folder on the left side so let me just open that up and put that uh over here so let's let's do like this now you can see in the version is 1.0.0 Z and then the description is nothing and then the main is index.js and then we have the scripts so we have right now just test and right now there's nothing here and then only Echo error no uh T Test specified and then exit one keywords are an empty list an author is you know nothing and then license is ISC so what we're going to do is now we have all of this done we can now um basically create our um server.js file which is going to be inside of this place so what I'm going to do is I'm going to open up this into some little maximum screen and over here I'm going to create a new file which is going to be called index.js right main index.js this is going to be actually the brain of our uh backend so over here this is the index.js file and make sure like uh you just created it now if you want to you know write anything like suppose console console.log and you just say let's suppose hello there now if I if I write this right I want to actually run it to run it you could actually use that same terminal and if you just type in node and then you say index.js now one second I didn't save it over here so you see now it's saved the circle is gone and when I do this you can see that it's running but what I want to do is I want to actually use package.json to use this file so to do that all I have to go is within this um you know within this file which is in the scripts um and then it has this object which has test what you could do is you could just add in comma and just add in one more which is going to be called serve and the value to this would be node and then we' have index.js all right so this is going to be for now specifically this command now if I just have go back to my terminal and say npm run serve it would still run this exact file because I actually told package.json that whenever I use the serve npm run serve it should run run that command so you can see there hello there has been run okay and then you can see over here that simple crud app and then node index.js basically it it did the same command that we did before node index.js and it ran this command and you can see the results over there really nice so far so now what we want to do is now that we have our package. js on and index.js we want now to install our framework which is going to be Express for the back end so let's just go to the website and it's actually going to be called um we have to go to the npmjs.com website and you could see over here there's every single thing that you need to know about uh npm uh dependencies and uh you know libraries so what you could do is over here you could have the Express framework just typed in if you type in Express and press search what's going to happen is that it's going to give you this exact match so when I'm going to click on this you could see this is the official Express framework which is actually going to be used to build our node API server and that's why we're now we just created uh this but we have to create this node API server so over here you could see that you could use this command to install this so just copy this from the clipboard and now you could go back to this Visual Studio code and make sure you just right click here and you know um it should press enter and then you know it would install Express now once you see you could see node modules folder populated and also a package lock uh Json so you can see p package hyen lock. Json over here existing and what one more important thing that you saw over here is this node modules so this node modules will contain all of the dependencies and libraries that we'll be installing within our application uh but right at the moment if you go to the P package.json file now you'll see that in the dependencies we have Express and that means we have installed Express successfully so that's amazing now we have Express in our application we could actually use it's border plate code to test it out whether it's working or not so just go in there and you just go in here and you can see this is const Express require Express and then this initialize the app and then what you could do is you could just uh call this hello and then you could essentially do this so I'm going to copy these two lines up here and I'm going to go and replace this with this so I'm going to have on Express equal to require Express and over here we have app and we have Express so right after this is what we're going to do is we're going to just try um we're just going to try to uh run this so running it is pretty simple and simple easy all we got to do is you know we're going to try to listen it so we're going to say listen for which Port so we'll say by default we'll say 3,000 port and we'll say you know for that particular Port what you have to do is you have to console.log one minute so console.log now you might be seeing that there's a lot of things going on here I have co-pilot and that's why uh you know it's giving me suggestions so now you could see over here server is running on Port 3000 when I press tab it just autocompletes it so now we could see that ab. listen 3000 and then you could see whenever I go to this 3,000 it should have this message on the console screen so if we want to do this and you want to see it so let's just go and you know visit so but before that if uh what we have to do is we have to uh run this application so right now it's uh app. listen and uh you know it's going to now say server is running on Port 3000 so we're going to do npm run serve again so npm run uh serve and if you can see it you could see Server is running on Port 3000 which is awesome now our this uh node Express uh back end is running on Port 3000 which is fantastic so right here right after this and we're gonna say app.get and we'll have this forward slash right and in here we have request and response now these are two things whatever the client sends to the uh server is the request and whatever response comes back from the server is basically the response rest so uh the client is basically on the browser and I'll show and explain everything if you're not following along right now so if you could see over here um I actually had app .g get and then over here we had the Slash and over here we had requ request and response now over here we have this bracket right but we forgot to close this round bracket which is right here and now this is all complete but we want to do is that whenever we visit this default page we want to throw out a response from the server right from our server saying response. send and we'll say uh hello and then we'll say hello from node AP if I do this this means that the response is coming from the node API and this is going to appear if we run this uh you know on on our machine so essentially if we go to the Local Host Port 3000 it would work so let me go here and you could just type in Local Host and then we say Port 3000 um and you'll see that right now it's not uh it cannot get the thing so we had to basically turn off our server whenever we make changes and then we have to run npm run serve again because I actually added what I add what just server is running on Port 3000 so once everything was there um and you know this hello from node API with the slash this is what the response is sending but this is just on the console screen so now if I run this you'll see this is on the console screen which is this one but this is coming from the node API and this should come uh through our browser or something so if I go and visit this Port 3000 on Local Host there essentially you could go here and you basically if you refresh to the page with Local Host Port 3000 you'll see hello from node API the response that's coming back from node API this is amazing and if you want to see it through Thunder client or something just type in HTTP and then call in to/ localhost Port 3000 and then if you do this you'll have to make sure you have get and when you press send you'll see that the status is 200 means it's okay and then you'll see the size 19 bytes uh time which is 2 milliseconds and the message hello from node API which is a success now these are some tools that we could use to test our apis which is thunderc a really popular extension in nodejs which is highly recommended you could also go and test out using insomnia so we're going to go to insomnia uh it's a software so insomnia. rest if you go there this is uh the website that uh essentially design debug and test apis locally or in the cloud so get started for free make sure you select this one which is $0 per month and you could actually uh continue with your Google account uh we could subscribe to the free plan and just say subscribe you are one step away from improving your API and then we could actually get started with insomnia to get started debugging and testing API download the latest insomnia desktop application so we just click on desktop for uh application for Windows or download for Windows and over here we're going to click on this it's essentially going to download this insomnia software and also there's one more software which is called Postman so if you go to post man.com you'll have this other software that you could use to test apis and essentially you could sign in or create an account for here as well so over here essentially you could use for Windows so you can just download the 64-bit here and this is downloading as well so that's great essentially you could also uh you know sign up with your account it really doesn't matter um perfect so you could just allow access and this is your Postman interface all right so it's pretty similar and I'm going to explain this throughout this video so don't worry this is just how Postman looks like and also uh we're waiting for the last one which is insomnia so when I click on insomnia you'll see this uh pop up so we'll just wait until this is done and once it's done you could say uh welcome to insomnia the new version is the biggest one ever notable features and these are some of the new features now we continue keep uh storing locally in local Vault and enable Cloud sync and secure Cloud so one one collaboration with Cloud sync now available we're going to keep storing locally in loal VA we could also now uh sign in so we could actually just uh continue with our Google account once you do you could go back hi coding cleverly and then you're back so authorizing insomnia and now you're back to insomnia so this is your personal workspace and this is how insomnia looks like awesome so now that we have all of this out of the way we can actually test our uh you know API so if I test it it's pretty simple we could go and you know to that Thunder client and you see remember this test that we've done exact same URL you just copy and you go into insomnia you could actually go over here and this is your you know personal project and you say new collection new document and what we say is new okay so we say my collection or we'll say node API and over here we're going to create that once we're created we created that node API we could add some uh you know apis here so we have an uh you know add a add new folder and we could call it you know get and then we just create this folder and in here we could just have HTTP request and just paste in this and paste it from here and then you basically have the get option selected and you click on send once you do that you can see hello from node API this is perfect now you could see that this is working and this is our get so we have this new request we could rename this as well so we have this new R uh uh we could just rename this and we could say this as uh get API all right and then we could just rename it and there you go this is our now uh first one so that's amazing uh this is how it works on insomnia and we're going to keep moving on forward with this perfect so right after you know testing it out through insomnia we're going to actually now have to go and download a few important things one thing is we make sure you have git bash because we're going to be actually using uh things that we don't want to actually include so the first thing is we don't want node modules right so we use dog ignore and for this purpose what you're going to do is you're going to go to downloads and you going to go to get bash if you go to get bash you'll see this one and you'll have to download for Windows over here and then also the installation step and everything and once you do you'll have to include it into the path just click on next and everything and then if you go and uh want to check if you have git you could just say G hyphen hyen version and this should tell you whether you have get installed or not so that's perfect after that you could go back here and you could just into the um node modules you could just add in you know into the G ignore you could add in nodecore modules this means means that this is not going to be included when it's going to be deployed on um you know it's basically going to be pushed to the repo so it doesn't include the node modules folder which is pretty bulky with so many things and now um we're we're done with that and now uh we have to do one more important thing and this is basically uh something very important to do so what I'm going to do is I'm going to actually uh you know show the screen we don't uh you know we don't have a mechanism of when changing anything so suppose if I say hello uh hello from node API uh for uh you know hello from node API server if I do this I go try to uh I mean if I try to go back and see the uh and I see the changes so I'm going to have to remove all of this and if I tried to see this uh you know reflection I try to refresh it but I don't see any change from here the problem is that I have to turn off the server and constantly restart it whenever I make a change and then I could refresh the page and you'll see that change but this is not great we want to have this kind of mechanism that allows us to do this automatically and that's why U nodon comes in so we turn off our server and we install nodemon so what we type in is npmi we have nodemon and then hyphen D and if I do this you could go and see that this is would install nodemon and you could see where I uh brought this up from if I go to npmjs.com and if I type in node mod and if I search you'll see this one exact Mass simple monitor script for use during development of a nodejs app if I click on this you'll see this is how you install the npmi nodemon and that's what exactly what we did but we did hyphen D um so once we did that now what you could see is you could actually install it globally or you could also install it you know in a de development part so now you could actually verify whether you have it installed using in the package.json you'll see nodon is in the de dependen IES which is perfect we still have Express in the dependencies and what we're going to do is um we're going to just put it into the script so index do uh so essentially in the package.json where the serve was right so you're going to add another one so over here we're going to have development and in here we're going to say node modon so going to have node man and we say index.js this means now automatically it will reflect the ch es and we don't have to now type in npm run server but we can now have npm run Dev and when we do this it's right now server is running on Port 3000 and you could see right now this is how my you know my response looks like but now even if I do any kind of change it's going to be reflected and it's going to show that you know the server is going to restart so let's see if I had the uh hello from node API server uh and say updated and if I you could see now what's happening is I didn't save anything right now the file isn't saved but when I press contrl s you could see the server restarting due to changes and then starting node index.js and then you can see it restarted and then you can see over here that I didn't refresh this browser page but when I do you could see that the refreshed uh result came here which is perfect now right after this success now we're going to actually try to connect a mongod DB database to our nodejs backend so what we're going to do is we're going to go to no mongod db.com so we're going to go to mongod db.com so mongod db.com Atlas if you go here you'll have to sign in with an account so I'm going to go here and make sure you it loads up we'll sign in and then you can see mongodb Atlas and over here we could sign in you could use your email address I'm going to use Google ooth uh and I've implemented Google oath in one of my videos on my channel so if you want to see how this is implemented using next s or even JavaScript or even MN I could have a video available and you'll you'll see that process so now you could see over here I already had my mongod DB created but for you guys you'll have no projects so you can see that I have a couple of projects going on here so what we're going to see is we could just go and click on this button over here and say new project and once we have a new project you know we'll be introduced with some basic things and we'll just have to set up so now you can see over here we have the name of the project so you could just call it anything you want I'll call it node API all right so now when we have this is uh created we go next and we say add members and set permissions so we'll just give ourselves the uh you know permission and we'll say create project so once we do this it's going to create a project for us and now you'll see loading homepage now it says create a deployment so what we do is create and we'll have to go and deploy your database so we're going to have m0 which is the free one AWS is good uh Mumbai is okay and over here we'll have the name of the cluster you can name it anything you want I'll call it mine as backend uh DP it really doesn't matter you could just call anything you want and now over here we have uh you know create so after that you know it's asking me what are the bicycles here I don't see any bicycle to be honest um so I'm just going to skip it and over here you can see motorcycle so this is motorcycle all over here okay great so now we have that you can see over here how would you like to authenticate your authentic uh connection so security quick start it's basically username and password we could add in our own username and password so I'm just going to have admin or you could just have simply uh this one and you could have your password set so you know you could actually update it yourselves but you know I'm just going to have this password saved somewhere so it's important to save your password somewhere so I'm just going to paste it right here and you'll see that later on we'll actually use this so we'll just create user and then after that we have this uh username so we could just save this username as well essentially it's going to be in the connection string but I'm just adding it anyways so let's just put it over here so right right here okay so now that we have all of that we could go back down here um and we have my local environment and over here we have IP address so connection this is my IP address but we want to allow it to access from anywhere on any device so we say 0.0 0.0.0 and then we could say allow from anywhere now this is uh you know um not compulsory description you could write anything you want and this IP address just had to add that and now when you do this you press add entry and this allows you to uh you know add this IP access uh list you know IP access list is updated with this particular thing and then you could just finish and close congratulations on setting up access rules we go to overview and now we will be able to see our database that's uh backend DB which is good now you could do is you could go to the database part here and over here we'll have to go to connect and over here we'll have to go to drivers or you know connect to your application so basically just go to drivers access your Atlas data using mongodb's native drivers so over here we could do is nodejs and 5.5 or later just get the latest one npm install mongod DB which is very important so let's go and first turn off our server so we'll just have this turned off and then what we do is we just essentially have this in and PM install mongodb this will install mongodb for us and once it's done you know we could have this uh connection string so this connection string is the thing that we need so let's go back here into our application and what we're going to do is we're going to uh try to uh access this so to to do this it's pretty easy and pretty simple what we do is we go to um index.js and over here we have to import mangoose so if I go here you'll uh um into that you know there's a dependency Library which is called Mongoose and if you go to this uh npmjs so basically npmjs.com if you type in Mongoose this is the dependency DB odm so it allows you to access DB pretty easily so just install this npmi Mongoose and we'll just go back here and we'll just paste this in here so now we could have Mongoose there as well now you could see over here um after installing Mongoose you could have you know this thing which is Con M Mongoose is equal to required Mongoose and then after that we have es6 um you know essentially you could use it like this as well import mango from so uh you know you could use any way you want um so I'm actually going to use this one so I'm going to put it right there and once we have Mongoose we could have now the part to just add it so this is the way we could connect the DB so we have over here uh this is the exact way to connect it so we basically say mango. connect and we have the connection string and then you know we allow things like that so let's go back here uh I don't know why this is there so we'll just leave it like that and we'll go back here and now in here what we're going to do is we're going to have we're going to have um so mango. connect and then we'll put in the connection string here so connection string you already uh you know that I already have it so it's in my clipboard Okay so so it's in my clipboard which is this thing and no no it's essentially this is the connection string now let's look at this a little more deeply this is the connection string that was copied from mongodb you can see this is my username and over here they gave me a section to add my password so remember that password that we saved you could actually just copy this one and just overwrite it with this one so you know paste it right here done now after that we have ADD backend DB so this was our you know um cluster name that we created and then over here we have um slash and right here mongod db. slash we could add anything over here so we could just call it as our uh collection name so I'm just going to call it node um hyphen API so I'm just going to call it like this this is our collection name and right after that we have question and then right after that we have question and then we have retry wrs is equal to true and then we have and uh W is equal to majority so that's essentially it um and we could have this in the new line and say do connect so do connect and then when it's connected you could have a response that says if it's connected you could say connected to the database all right and then if it's not connected you could say catch and then you could have this you know the same stuff so essentially you could have uh console.log connection failed so this is how we're going to connect to the database let's see the file is saved let's run the npm Run Dev and let's see if we are connected to the database or not and running and connected to the so server is running on Port 3000 and we are connected to the database now this is really good but the best way to actually uh you know go around this is you can see that first of all we want our uh database connected and then we want the server to be running the database connected and then we have servers running on Port 3000 so that's basically uh the way to go now let's just refresh and turn off the server and restart it again so npm runev so you can see right now we have connected to database and then server is running on Port 3000 it's just another way to do it it's totally fine if you want to do the other way but I actually first try to connect to the database and then you know uh we try to run our server now that we have our database fully connected we want actually a model and a model is essentially something that we could use to store data into our database and we want to store some products all right so everything that you want to store that it has to be a model of some kind our nodejs application could use and then you could use it to send it to the database so that it could store it there so what we have to do is right here we have to create a model so let's go here and we could use uh close all and right here you could just CL click on this to collapse everything and then after that over here you could just create a um you know essentially a folder here so we could just create a folder inside of this directory and we could call this as models so models and then in here we could create our model and we have a particular naming convention and I could and I could say so do model do JS if I use this naming convention it's still all right and this is just good to remember like this is the model so we're going to use Mongoose to create our uh model so Mongoose uh is equal to require Mongoose cons Mongoose isal to require Mongoose and then over here we're going to use our uh you know product schema we're going to create so we're going to say product schema is equal to mango. schema and in here we have uh essentially this stuff okay and this is the thing that we have to put in so over here um okay so over here we have this and I just want to make sure that this is the object and in here it's going to contain everything so like this so over here we're going to have either name all right so we're going to have name and then we could have uh you know something like type and we could say you know it's a string type name of the product and then we could have a required field and we could say that it's true right it's it's required you can't have a product without a name and then you could have um you know a me message and you know like that allows you to enter a product L please enter product name so that's it and then after that we essentially we could have a comma here so you could you don't there's no necessarily need but you could just add it in case uh right here we have is the name and after that we have the quantity so essentially over here we'll have the quantity quantity and over here you could see that we have Colin and then over here we'll have type and here we'll have a number for the product so let suppos number and over here required will be um required will be true and you know we go to also have default value for the quantity as zero and then we could just put a comma here so essentially there's no need of commas at the end so you could just uh for these types but you could only but you should include a comma if you want to add another object here so another property over here like name and then quantity now suppose the price of the object or the product and the price would be a type of number and would be required and by default it could be zero okay the same thing goes for the image of the product so you could say image and then you could have Callin and then uh the type would be string and then it would be required which is true and well essentially you could either have the image or you can't have the image so a product could exist without an image as well so that's basically there and then you could just close that that was an extra right right after this object this entire one we put in the comma here and we put in time stamps calling true this what it does it allows us to have two more extra fields and that's one is created at and updated at so um it's going to tell us whether when we created this the time um and then when we latest you know create the latest update and it will keep be it will be tracked so we you could format document I don't have any uh you know configure I'm going to just use prettier this is an extension again just used for formatting and now you know you could see that this is all my uh you know schema and now we have to just allow mongodb to to use this so we could say product and we're going to be calling this as the model so it's product and we're going to say mango. model we're going to use the model and we'll say now over here this is going to be within the mango database now check this out we have to make it singular because it's going to add an S all by itself in the database and you'll see it and you can see right now it's in capital but in the database it will be all lowercase so it's going to be called products and it's coming from the schema that we created up here so this one the product schema so we can simply say product schema right here and there you go so now we have this product model and we can just import I mean export this so we could say module. exports and we have product like this so there you go this is the entire model for the product and now we are going to be using this to create products in our database now that we have created this model we're going to use this model to save data into our database so we're going to go to index.js and over here we're going to use that model so it's very easy to use and what we're going to do is over here you can see that there's this connection part right so we're going to just leave that connection on the bottom and over here we're just going to use um you know the slash this is just normal but over here right after this slash we could actually have you know the following so we could go here and we have app. poost which is going to be used to save something and in here we could sa you know/ API products right so we could just have uh right now I'll just have SL API SL products and then we have request and response and in here we have this now now check this out um what we do is you can see that there's this um completed but there's still something missing and that's this bracket over here now over here we could we could uh use uh you know response and we could say data received or something we could send a message right now let's just test this uh API out does this work so/ API SL products if I go there so I normally would assume that it's running so if I go to SL API products let's go here and we'll say slash API and then we have slash products if I do this you could see over cannot get/ API products the reason why I cannot get because I actually used post here and when I use post it will say data received we could actually test it out and you know we could just have something really easily uh written now remember whatever we given to the the client it's in the request so whatever the client sends is basically a request so we could have console.log and we have the request. body whatever the client gives and we want to show it in the console screen right here and we also want to show it in our uh browser so we could do in the client end so we could say response. send and we could have the request uh. body right here as well so let's see how this looks like but we can't actually really test it out um through that browser so the best way to do this is either through insomnia or the other uh Thunder client so over here we're in insomnia and what we're going to do is you can see over here get and then we have get API so we're going to have a new folder here I'm just going to create a new folder and I'm going to say post uh right so I'm just going to say post and I'm going to create it so you can see over here we have get and then we have post so I'm just going to have this closed yeah so this is open and this close uh HTTP request yeah so in here we're going to have an HTTP request and over here we're going to select post and over here we're just going to paste in that HTTP call in for uh um to4 sles Local Host Port 3000 and it's called SL API SL products once I do this you could see that you know this is coming there and essentially if I send you'll see nobody return for response the reason why it's no response but the request has been sent is that I don't have any body so what I have to do is I have to give some Json uh values over here I could actually just have you know anything I want so so so over here what I'm going to do is I'm going to say in quotation I could say name and I say coding cleverly okay now what is this this is actually Json and I'm passing it so it says name and then it's also in strings now if I send this you can see this has been sent but the body still hasn't been responded back and you can see that the result over here is undefined the reason why we're getting undefined here is that we're not allowed to pass Json through our nodejs by default we have to use a middleware we have to configure it and uh the configuration setting for this middleware is super easy um what we could do is we could add that middleware part so it's basically app.use and then right after the uh you know initialization of the app so essentially this could be over here we could have app.use and we have express. Json and once I do this this means now it would be able to view it so now if if I passed in coding cleverly you'll see that then return was coding cleverly and when I didn't give anything you can see I could see an empty list and but when I give it you can see I could get this coded cleverly same goes for this now now now this is not going to be anything but you know you you get my point now you know we basically fix that up and now that's basically it now instead of having you know that client but client could have just add in anything he wants like suppose not just the name but you could have let's suppose value and you could say uh you know um 1 2 3 and you don't need uh any string for that and when you're finishing it don't add any comma here so now we have send you can see now there's two things from the response right so this is pretty good but what we want to do now is we want to add multiple things in here so let's go back here and what I want to do is I want to add it through you know basically I want to save the data uh using the mongod DB um model that we created so we use try and catch okay so we remove that and we use try and catch and we use console.log so for the error it's pretty pretty simple you could just use console. log or error this is just going to show in the console screen but other than that I mean right now you could see if uh the you know remember that console log that I was bringing now you could see it over here but now it's changed because I am actually changing it I'm using response when uh you know I'm going to show it on the browser and I'm going to say if uh you know if if it's not done so we'll say a response status of 500 meaning a server error and we'll have uh sponse and we'll throw an out of Json so we'll say Json and in here we'll have an object and we'll say message and the message will be the error message like like like this all right and then for the try now we could actually try to save that data so we're going to have to import our model so remember this model that we created product. model. JS what we could do is we could just import this model here so it's pretty simple and all we have to do is const and we say model so we just say product and then we say require and then we have/ models SL product. model and you can just add in JS so that it doesn't have any kind of issues but uh you know just a good prevention here and then once it's done here and we could add in this over here so we could just say um in the tri part we could have con um you know essentially what we want to do is we want to save this product so you want to save it and the best way to do it is await product. create all right you're using this command and it's essentially going to require a weit because it takes time and whenever you do a weight you have to put a synchronous over here okay so as s YN as s YN okay as s y NC okay async and then you have this await so await product. create um and whatever the requestbody was you pass it in and you basically create an a property here uh an object um you know for that an instance for that model so now over here we have this as a product we could just name it as product like this like like this so we have cons product and then we could just return that product whenever it's created and we could say you know as a as a response Json so we could say response. status and we say 200 meaning it's a success and the Json we could just throw out the uh product like that okay so now this is going to be really awesome because uh for now you can see that we have some fields which are name and quantity and price and image so there's it's more than you know there's extra fields which are time that just automatically being created but these are the three important ones they are like mandatory and this is optional image field because it says required false so that's test this out using our insomnia so over here what we're going to do is we're going to create a new folder and we're going to be calling essentially we're going to create a new folder and we're going to be calling this as add a product okay and when I do this I create this folder you can see this folder is here now in here you can see this is open and now you could essentially have this click and you say HTTP request and you could just rename this you could just rename this over here and just call this as uh add API Okay so just rename now you can see this add API and now you could just have the you know the same thing colon two for slashes uh Local Host Port 3000 and then we have SL API slash so what is it SL API slpr products right so whenever I do this this is that API that what is existing but it's actually a post not a get because we want to add something and in the parameters so in the parameters which is in the body so we want Json right we have click on Json and over here we could just put in something like here so first of all you have to give them uh the fields now look at the fields we have first of all name quantity and price and we have name as only string so we'll have name and we'll say let's suppose we want pizza and then we have quantity so we'll have quantity in know the keys always going to be in string so quantity and then we have colon and then quantity is a number so we'll say 10 and then price so price like this again and then we'll have price as let's suppose uh 5.99 okay so now we have this which is there we could just simply press send and this should add it to it so we could press on send so now you can see over here we have these three you can see this ID has been created and then there's this created at and updated at and then this underscore V this indicates that it's it's successfully saved in the database because mongodb gives an ID unique ID to every single object that's being created and now we can actually reflect and check it in our database so if we go here and we go to browse collections you'll see that remember uh like I told you before that there's going to be um this product and then there's going to be an s in here and then this is going to be lowercase and it's going to say products right and remember that cluster name that we created which was um node API and inside here we have this products right so this was collection uh this is a collection my bad and then in here we have our table which is products and you can see over here that our pizza is there so that's it's been saved successfully which is amazing so this is great we could add some more uh products as well let's suppose we have pancake so pancake okay and we'll have quany as let's suppose 20 and the price for a pancake is $3.99 send it now we have pancakes added let's have um you know donut donut um and uh this would be 40 donuts and the price for a donut is $1.99 press send and then donut is added okay great so we have some products added now if you want to view these products how can you see them well you could see them over here if I just you know click on refresh here so if I click on refresh you'll see the these uh products are being updated here but I want to actually have an API that allows me to you know view these products there so that's pretty simple as well just go here so the post was created now you just want one that allows you to view these um you know listings so what you could do or these you know products so we could app.get and we could just simply say/ API SL products and then this is asynchronous and then we have request and response and then in in here we have this body and then uh essentially this close and that's it so in here what we're going to do is we're going to have the you know the the the area where we're going to have to retrieve it so and we could view it so try and catch again the same thing response. status um 500 and it's going to give a Json you know object and says message error message and over here in the tri Place essentially you use a weit and you use product. find you want to find everything in the database so you could use this um bracket and in whenever you use a weight there's an async automatically that has to be over there and now what you could do is Con and we say products like that because there more than one product uh now what we could do is we could have response. status 200 and we could just J on the products and that's essentially our API that's all that's pretty much it now we could use it and test it out over here when go this we could go and just create a new folder so we could say new folder and we going say uh get so we say get all products and then we could just create this I mean this is not required this is just insomnia so uh so over here we could have you know HTTP request you essentially um having without having it over there you you could just you know you could just close these out so you can see you could close this folder you could close this folder you could close this folder and then this is get all the products one right so you could click on this one and you say I want I want an HTTP so HTP request um so you could click on this so it just created three different https right so we want to delete U the multiple ones that it created this is just an organized way to do it you could use Thunder client it's super efficient and easy but I'm just showing you that there's so many Poss abilities here um and over here what we're going to do is uh you know you could just rename this using this and you could just say instead of new request you could say this as um get API and press enter and then in here what you want to do is you want to call this API which is HTTP call-in Local Host uh two for sles Local Host Port 3000 and then we have slabi SL products okay and if I press get and this press send you could see that mongodb gave me a list of objects so you can see see this is a list and then inside here this is object one object two and then we have object three coming from the database this is amazing and this is how the get one is going to work but suppose if I want one individual product instead of all of them then I could use their ID to get them right so if I want the pizza one I could just use this ID to get it and then this one to there and then this one for donut and this one for pancake how do I do this also pretty simple what you could do is you could just put in an ID there but we have to create that API for it so over here we have this get plain one and in here we could just have one more which is right here on the bottom and this is just a specialized one that allows you to get a specific product depending on its ID so we could have you know now essentially these these double quotes or single quotes don't matter you could add whatever you want but it's just a good practice to have the same thing whenever you're using it so don't mix it up so so over here we have I'm using single codes here for Now so/ API products and then we have slash colon ID and this is how we're going to use it so we're going to have asynchronous function request and response and then we have this um and then we have this and then we have the semicolon and over here what we're going to do is we're going to use the same try and catch method and in the C uh catch we still have response. status uh. status okay status and then we have 500 and then we have Json and then we'll give that you know message saying that you know it's an error okay and then in the try place what you could do is you could actually get this ID how are you going to get this ID from the URL so you might be asking like how can I get an ID from the URL so right now if I do this you could see this is a list of the you know get command that we did and this is giving us the the API uh I mean API for all these uh products and the are three but right now it's really hard to uh read them you could use some kind of extension uh Json reader uh Chrome right so if you just type in anything like this uh it's going to give you some cool extensions on Chrome so this one's good which is featured you just add to Chrome and when you add this extension uh you know this uh Chrome extension should be added so Bon viewer and once it's there okay so we have this extension right and when I refresh this page you can see now it's essentially now in a really nice formatted portion and you can see over here they also gave us like what is the API that we called and this is the retrieval Pro process so it's pretty nice I mean everything is you know pretty well made so great so this is the API and like I was saying is uh like I want I want the URL where I could say slash ID like I want this ID so suppose if I say I want this pizza and I get this ID so I copy this entire ID right here and I have put in the URL which is right here now how can I get this url's ID how do I get this slash and then I get this part for that you have one specific thing and I'll tell you how to do it it's really easy and really cool it's called um prams so essentially we could use it use a destructure and we say we could get the ID from request. prams if we do this we basically got the ID from the URL that's all we had to do and now we could just uh search this in and retrieve it so we'll say await and we'll say product right so product is our model and that find by ID is a command so we could have that and we pass in the ID okay so we pass in the ID but we also have to now just say nothing else and also we just have to find this and we'll put in a cost product like this and then after that we could just ret retrieve that status uh 200 and we'll say you know that Json product like like like this so that's great so now we have this product uh available as well when we go back to our browser you know you could either go through the browser and you could see that automatically it did it I put this and then it gave me the pizza now let's just test it out using test uh you know this insomnia as well so right now this is get all products so again I could just create another one so let me just close this folder and create a new folder which is over here and it we'll say um get one product okay and then create this folder and over here we going to open up this folder and make sure we have an HTTP and in here we could just name this as um single API I just named it whatever I want and then over here we could have HTP colon 2 sles Local Host uh so Local Host Port 3000 and then we have SL API SL products because it's there's an S and then uh Slash and then the ID so remember the pizza ID I have right here in the clipboard and what I have to do is in here it's just get so nothing else and over here I'll just send and there you go we have the pizza so before we move on to the edit or update API what we want to do is I just said let's you know that single API where we get one product let's just have that named as product instead of products because this is multiple products and this will be single product and all you got to do right now is nothing else but just have this call to single product instead of products because if I call it with multiple it will get an issue and it says cannot get this but if I go and say this because it gave me a 44 it says it doesn't find it but when I remove that s and I press enter you know it still works so that was just pretty much what I wanted to do and you know that's pretty good uh you go to refresh and you could get the same kind of issue here uh but when you move on to this part which is this you can see now this is there and you know everything looks really fine which is great it's just super awesome now let's go and now create the edit update API so over here what we're going to do is we could go to this products and right after this uh / API products which is just uh allowed to post a product we could now create update API so I'm just going to put a comment and say this is as update uh a product okay okay so what we could do is here we could have app. put put is the command to update you could also use patch to update and this is HTTP method so put is also used to update and Patch but we're using put and over here we're going to have single quotes and we'll have SL API SL product so we'll have Pro uh product and we want to update a particular product based on its ID right so we could use the ID again and now once we have this we have an async request and then we could have this and then this all right so now when we have all of this we could use try and catch again and over here again over here we could to have response. status um and then we'll have 500 and then we'll have json. Json and then we'll have um you know object and then we'll say message and then we say error message okay that's great but in the try what we could do is we could essentially get the ID again from the con ID destructure it and we can say equal to request. prams so this is the ID that we got and we can use this ID to update the thing but first of all we have to first uh you know uh use that command so it's await and then we say product and there's a specific method which is called find by ID and update so find by ID and update so like this yeah so basically this is the command it's called find by ID and update and and we pass in the ID and we pass in the things that we want to update so whatever the user said in request do uh request. body that's what you want to update it with so essentially this is going to be like whatever the user gave you want to update it with that and you want to put that as the cost uh you know product so this is the product like that so this is going to be the product right after that so suppose if the product hasn't been updated if I say you know if the product doesn't exist or something that means it just doesn't exist you could say response do you could just say return and then this would stop the thing and it would say response. status 404 and say message product not found um which is great we found a reference to public code in a recent suggestion uh but now after that what we have is if this is not existing I mean if that uh now if it exists what we could do we could actually update it and we could just say product like this but before this uh there's a check here because uh normally the product is going to be updated so it's best to have it rechecked from the database so you could use it either using uh a weight again and say uh product and then you say dot find by ID now essentially because it's already updated right it's going to be reflected in the database so it's better to just check it again for so find by ID and and then you put in the ID over here so let's put in the ID okay so you essentially put in the ID and now what you're going to do is const and we say updated product right here and then equal it and then we could just return so basically response. status 200 meaning Json and then updated product so this is the API for updating a particular product based on its ID and now we're going to be testing it so let's over here um we created another folder so I'm just going to close this one and you know you can see all these folders here and now we could have one more folder which is going to be called update a product okay and then we press enter to just create that folder and in here we just have this button and we have HTTP request but in here let's just have update API okay Enter and now over here we could just have HTTP call in to/ Local Host Port 3000 and then we have/ API SL product because it's not going to be S and then it's going to have it ID so if I look into this you could see over here it's product and then it has its ID so we could have the pizza one we had the pizza's ID I believe it's this one and now if we want to update it we have to go and put in put and in here we have to go in the body and we have to go in Json and we have to change the name or whatever we want so let's suppose we want to change the name and we'll say and we'll say it up uh updated okay I'll say updated Pizza okay if I do this and I press send now you could see that this it will return the entire thing the reason why it's returning it is because I'm actually saying give me the entire updated product by finding it again from the database and now you can see that it gave the name updated pizza but other things were just the same if I just uh you know say okay now I want the price changed as well and I say from $5.99 we want the price uh increased to $7.99 okay now if I do this and I press send now you could see it's going to take sometime and there you go it's updated so this is the API to update a particular product within the database and it's updated now successfully and now if you want to view all the products so you could see get all products so you could see over here if I click on this API so this is a particular thing if I just you know press enter you can see this updated Pizza is there $77.99 but the pancakes and the donuts are still existing so this is beautiful I mean we could go back and we can update over here so this is the convenient part of uh this um you know tool which is insomnia or Postman because it's really organized instead of having Thunder client Thunder CLI is very simple and you would just pass it in but you know every tool has its own advantages and disadvantages but you know um just look at for the you know the good things of everything so look at the good parts of insomia and use it all right so next part we're going to do is we're going to have the delete API so now that we have this update product let's have the delete product so right here um we're going to have slash delete okay so we're going to have delete a product like like this and then over here we have pretty simple uh method for delete it's just called app. delete this is the option and then in here we're going to have SL API SL product and then what id you want to put pass in so you pass in the ID like colon ID and then you want to uh put an asynchronous function so as YN C and then you have request and response and then put in this and then this okay and then put in this now if you want to delete something it's pretty again simple you have try and catch and then here you could just have uh response. status uh 500 and then you could say um you know do Json response and then put in the message and then just say you know there was an error like like like this or else if you want to try to delete it again use the con ID destructure it from the request. prams and then um what you're going to do is right after destructuring it from the request. prams you could use it using await and and then um you know a wait and then you say product um so basically oh man what did I open so what if I do product. find uh you know it's basically find by ID and delete so find by ID and delete and then you pass in the ID and essentially you want to uh you know you delete it like this so that's pretty much it and then uh once it's deleted you could just have the product saved again uh just return the response of it it so just say that you know this product has been deleted or just given information that this this was the product that was deleted to the response and then you could just say Okay response okay so for instance we have to put a check first if the you know the product doesn't exist so if the product doesn't exist so so what we're going to do is we're going to throw out the return and then we say response. status and then we have 400 uh you know 404 and then we'll have Json and then we'll have the message of you know process not product not found and then you know if everything is fine we have response. status 200 and then we have product deleted successfully uh which is great um and you could also just have the product uh you know displayed or something but uh you really don't want the product so you could just have you could just have it like like this okay so now we could just format document like this and you could see this is our delete API which is right here so this is our delete API now it's just time to test it so right here you can see that there's um no product here so that's the only uh reason I was putting it so I think it's best to have it uh so cons product wait a minute Pro product okay okay it's okay so now this is okay that it tells us whether it's not and then after that it's just going to delete it great so now let's uh test it so we go to postman uh we don't have to go to postman but insomnia and over here we could just create another folder just close all these things out so you could just have this closed and this closed and just create a new folder here so we say create a folder and we say uh Delete delete a product okay so we have delete a product like this and then in here we have HTP and we have um so delete API right and then over here have HTTP uh colon to/ localhost uh Local Host Port 300 SL API SL product and then we could put any product we want let's put our uh you know famous pizza and we'll have uh you know the post option which is O I mean sorry not the post but the delete option here and then all that's we have to pass in and then press send and you can see over here message says product deleted successfully if you go to get all products you know over here the folder and get API this is the get API saved over here so we just send it once more and now you can see now we don't have pizza anymore but we have pancakes and donuts so let me know how you thought about this if you have any questions uh like if you want to add more products into this just add them quickly with that you know add a product API which is right here and just add them quickly and easily within this and then it would just be updated here let's suppose we want to add one more let's suppose uh cheesecake and if I say um so let's say 100 and then um $4.99 now if I send it now the cheesecake is added there as well okay so now you could see if I you know view all the products uh which is over here which is in the get all products now I send it now you can see pancake donut and then cheesecake is right here which is amazing now this is uh the apis the simple crud apis create read update delete I wanted to say that you know you could actually have Postman insomnia and all these other products so it's pretty simple over here as well so this is Postman and what you do is you basically create a colle you know workspace here so um essentially you could add in you know things like my environment environments and you want to add new http right and then once you do that you could just paste in your you know HTTP so suppose if you want to use this you know this one this is allowed to view everything just paste it right here press send and you could see now a response came here so you could see you got a response here so you can see one two and three over here and one other thing that I wanted to say is that suppose you want to add products right so you have added API but instead of adding it through Json you want to add it through suppose some other format like a form URL like this right so in a form URL you basically put in uh just a key and value so suppose name and then I want so this is like yeah this is the name and then I want something like uh something let's suppose I want uh chocolate chocolate add yeah so add so I have name chocolate and then I have price and I say um you know 3.99 and then I have the last parameter which I guess was um quantity so quantity it doesn't matter which order it's coming from but as long as they match so let's suppose uh 35 okay so this is chocolate I want to send this I can't do this product validation failed name please enter product name now the reason why this is happening in like 500 internal server is that our middleware isn't configured for adding these other things uh you know this form field so you know adding this form way you know you form URL encoded uh method of adding things and you could just configure it within the uh middleware so right here where it says uh express. Json you could say app.use and then you could have uh express. URL encoded and then you know you could have the you would have the round brackets and then the braces and say extended Callin fults if I do this now you could add this thing as well so let's just send it again and now you can add it so chocolate has been added and now to view all these products you could see over here where we view all the products uh add get API so in the get API if I press send now so get pancake donut and then we have cheesecake and then we have chocolate all right so that is amazing which is really awesome now I wanted to do a little bit Advanced and I wanted actually to structure this out a little better because right now to be honest we have everything cluttered within index.js and if we had more and more apis created we would have to make our index.js very very populated which is not which is really not a good practice at all so the best way to do this is essentially have everything separated out in really good uh folders and this is what I uh High highlighted out in one my videos so what I would do is right now I'm in the models which is great we had a separate one but over here we have to have these API so you can see/ API SLG get and they're all the related to products right so you can see this one's related to product this one's related to product this one's related to a product this one's related to a product and this one's so we have all these apis that are related to a product so mostly we have to create a separate route for this so so what I'm going to do is right now uh you could see that um we're going to create one route over here so over here we'll have a folder and we'll say routes so not in here not in the model but let's uh collapse everything and basically we're in the root directory and we'll we create a folder called routes okay so in this we'll create a file uh when it's going to be called um it's going to be called Product route um product. route. JS all right this is going to be the product route okay so essentially this is for the products so basically you could call it um yeah so you can just call it product. route. JS and all the routes uh that are leading to that product will be there existing so what I'm going to do now is I'm actually going to have you know these apis so if you could see over here we have this all there right delete so this delete is there this update is there this uh post is there and then this get is there so there's two post I believe so this one and then there's this one okay there's get and then there's this get okay so yeah exactly so there's just one post because we you have to add one product and then there's two gets okay so what we want to do is we want to add the URL routing here so what what it is like it's basically right after you know this slash this is normal it's really doesn't matter so right after that you could add in so essentially right after this so you don't even need uh you know so app. that right here you could just add in so even after this I mean just right after the middleware configuration what you could do is um you could say app do use okay so I'm just going to call this as routes and what what is this is going to be called middleware so you just have to separate the things out over here so routes and over here app. and we're going to have the route which is basically SL API SL products so you can see over here there's a similarity here so uh that's why we're going to use Pro whatever SL API products go we're going to take it to the product route so product. route dot you know whatever that product route is so I'm just going to call this as product route now right right here now this is not imported so essentially I have to create the route okay to make this file more distributed and more organized I would recommend having product. route.jpg for a single product we'll just have this as well and this is for uh you know adding a product and then we have um basically that's it so this is just for getting the product and then this is there and this is for adding up single product so what we're going to do is over here we're going to use this app.use and then SL API SL products and we have the product route now this is the product route which is right here right so what we're going to do is over here we have in the product route we're going to have to create this router so you see this router and then this router will point to other things like suppose router. getet and then we have uh you know just for slash because right now at the moment what you could see is um SL API SL product so by General um means this one has to be called right this one has to be called which is uh just getting the products and displaying it so I'm saying why General the general one which is the slash one right so this one should have uh should have should have uh you know request response and then you know you could just say something like that it really doesn't matter I mean at the moment uh um you basically could have this thing there so this entire thing could essentially go here and this means like now it's asking for the product right where's this product from so that's all you have to import cons product so we say so product and this is from the model and and this a weight is going to be for this asynchronous over here okay and that's basically it this is how this model is going to be called it's still going to work but a better way to do this is actually add this logic this thing this entire thing is called a controller function and you could add this separately into another f file which is going to be called a controller so if I go here into this folder okay so over here and if I go here and I go in controller controllers and in here we could add in a a separate file which is going to be called product. controller.js all right so over here you could add in this entire thing this entire snippet this one okay so essentially you don't need this entire snippet at all so all of this could just just be cut okay and then over here would just be a function but at the moment I'm just going to tell you what to do just leave it like this as for now but let's go here and in the controller in the product controller what we would do is we have const get products is going to be the function and then we'll have is uh async request response and remember that the thing that I copied so let me just add this remove that remember that's async of request and response we B we basically need only this so rest everything you could just delete so this could just be deleted and right here you could just paste this try and catch now it's going to be asking like where's this uh product coming from you could just say Okay con product uh this is coming from there uh and essentially this is your function if you want to export this function so that you could use it you just have um the following line added at the end so you could just say um yeah so essentially you you could just say module module. exports and then you you could pass in a list of what you want to export so you know right now at the moment we just have this get products being exported so now you could use this get products here so you could just have this uh route and in here we could have um const that product controller and then that's coming from product. controller.js okay and then over here we could use uh that function so essentially you're getting product out controller and you're essentially you could get the the structure and over here you can see that there's this get products so we could just say get products we're getting the function get products from that and we could use it over here here so get products right here okay so now that this is there you know you can see that this has been added which is Success now you wouldn't you wouldn't want to do the same thing process for all the other routes so like like suppose if I uh you know go to this other one which is this was just getting all the products would have router do get and this is so simple like suppose forward slash and then we have uh you know here Callin ID so Callin ID so this means we have now router for a specific ID right we're getting only that and then now you could understand this through this you could see SL API products and then whatever now this is going to be an ID in the URL so right right after that/ API products and then you could have uh you know get single product function instead of get multiple product and what you're going to have to create this function so we have to create this function in here so all we got to do is you know con uh you know get single product so product and then again this is an asynchronous function so asynchronous and then we have request response and then we have you know this semicolon and you could just add in you know the things that you need so essentially the index.js has the single function as well so over here now you basically don't even need this now this API is deleted because you already added it from there and now I'm going just you know one by one finish all of them and then just going to be using this route to do all of this handling so right now product. route is also not being exported so product. route is going to be exported through this function I mean this this line which is module uh module do exports and then we have router all right so once we do this we could actually simply import product route uh on the top so so if I go into this I could just have the you know router imported on the top right here so over here I could say um const product route and then we have routes and product. route. JS and then this is basically the product route which is right here so this is perfect all everything is synced properly and now we just have to create this product get product so we just get this you know get this I this code and we just copy this and we put it in this grit product which is right here once everything is done we could just have this um get product like this okay so once we have that we could now we could now um go back to this route and you could you know you could actually call it from here so get product simple and now there that's populat so now we have these two apis so by that you know we had five I mean we had total five so now three are left so let me just quickly add the last three um and one was simple which was the create one which is router. poost and that's used to create so this is also a for slash just a simple one and what we're going to do is we're going to have a post product so it's going to be basically create product function um so we could have create product over here create product function and we have to uh you know make this product function over here so over here we could just have cost create product okay and then we have is equal to async and then you could see all this populated and you could see that it's basically creating that product here so this uh SL API products this is this is already the old one we could just remove this and this is the creating one so you could see almost it's the same thing but I'm just going to double check if there's anything different I believe it's almost the same thing so I'm just going to copy this it's yeah almost look the same so you could just remove this now safely and you going to essentially go here and this you know create product is created so you just use it right here so comma um create product and then what you want to do here is you want to you know you could just have this there's also this part over here you could just go here and control and space and then press enter that's basically adds the create product here and the same thing goes for the next one which is the update you know update a product so you just add update a product right here remember that uh update a product so essentially it's also router dot now how to update remember put and then um when we want to update something we have to actually have its ID so call in ID and then we have you know the function which is called update a product so update product okay will be the function and we don't have it right now so we'll just have to add it right here so it's pretty simple again all we got to do is Con update product update product and then what we can do is async again is equal to async request response and then we just put in this semicolon right here and then over here we just want to put in the update product so this this thing will just go in which is this entire thing okay just copy this entire thing and bring it in here so like that and now uh once this is there you could just remove this entirely so this update a product is removed successfully from this index.js uh and this should be essentially like that okay so now we have that and you know it's complaining so just go here and press control and space okay it's not giving me any suggestion because it's not exported through that list right so we have to go back here and we have to add this through the list so we'll have comma and then we'll have update product and now it's going to give me the suggestion so it's going to go here and just control space and then there you go the last one is delete one so it's basically called delete delete a product and then over here it's router. delete and then we have its ID so SL Callin ID and then we have the delete product function okay and then normally we have to do the same thing over here so we have to go here um right after this function we have con delete product okay and then we have async uh async request response and then we have this we could put in again the same thing from index.js this this thing will be from here all the way to this catch so copy this entire thing and just paste right here also you could just remove this entirely now you don't need this delete as well and you could just you know clear it all so essentially now we just have the single route so look how clean this index.js has gotten now what we have to do is just finalize everything um so over here we have this delete and over here we're just going to have comma and then we'll have delete product and over here we'll just put in the delete product so we make sure we have um control and space and then delete okay great so now that everything is there I mean it's imported over here and it should be working SL API products um so so everything seems to be fine and you know we have format document just to make it a little formatted and let me just show you a little zoomed out view so this is the uh you know the delete controller this is the update uh product controller and then the create up product controller get product controller get products controller so you can see that this is all requiring model and this is all working so p is a common uh JS module it can could be convered to an es model so it's it's okay I mean you could use the um you yes six way of doing it but you know we're fine with this but you know everything looks good and we have these routes that are configured in the product. route and these are all going to call these functions so you can see if I just normally do get and then I'll call the get products if I normally have get with this ID I'll get the get product function uh different from the get products function and if I have normal uh URL of/ API products uh then it would have with a post method it will create a a product for me and if I have an/ API products with an ID and it's a put method it's going to go to the update product method which is right here and if I go and do/ API products uh with a with an ID and and it's a delete method I go to the delete product and basically it will delete the product for me so this is pretty really I mean really convenient and organized looks very nice and this is how it's professionally done so you can see over here that we have all of this done middleware routes and then this is just normal you know testing hello from node API and this is our connection so let's go and see if this is uh still warning and you can see that the database and server is still running let's go and now what we're going to do is we're just going to add in a product again again nothing has been majorly updated or changed so we could just go to um back to our you know that API let's suppose I go to add one product so add a product so you can see I could go add a product so over here we have SL products right going add through form or we could also through Json so I'm just going to have through form again um and let's have let's say um we have juice okay and juice is for $5.99 the quantity for what I do okay the quantity for juice is uh 100 or let's say 210 I don't know so that's send it and now you can see juice is added I mean nothing happened I mean it's everything is working okay so if if juice is added can we view all these so then just copy this and you could actually view it in their browser as well so if you do this you could see that all these lists are working I mean it didn't basically impact the API performance of pancake doughnut cheesecake chocolate and juice have all been there nothing is being broken and nothing seems to break everything seems to work so you could see if I want to um add uh this is the ad so get API so if I just send you can see now all of these things are being populated here if you want to you know delete anything suppose I don't really want you know cheesecake I could just get this ID first of all let's copy this and we can actually update the ID as well right now try to update Pizza it's not going to allow you because uh it can't find it 404 right we could put a really good message over there and that's something that I want you to put a message using you know response but right now you can see that this is the now I changed it to the updated uh so cheesecake so cheese I'll just write it and then the price and then I press okay so now over here we have slash API products now this is important now whenever when I you know I did the organized way to do the route and controller I changed all of them from now so everything has Now a/ API SL products now if I do this you can see that the cheesecake has been updated and you could view this uh cheesecake by using get API and this would uh basically send it and you could see over here pancake donut updated cheese okay and then we have chocolate if you want to delete the cheesecake uh you could just have this you know from here with the ID passed in but again the product will be changed to products and this is the delete method and now if I press send you can see product deleted successfully and if I want to review all the products so I'll do here and I press send again and you can see pancake donut chocolate and juice so one final thing I wanted to do was to add this into GitHub so this code that I added I wanted all of this to be populated on GitHub so what we could do is we could go to github.com so what we could do is essentially I'll go to my GitHub account so and then over here we have this plus button and create a repository and we'll you know we'll call it simple uh so we'll say simple crud app and we'll say backend okay and then over here we'll have it public and we'll create repository and then over here we could just initialize using this command so make sure you have that so what you could do is this is the uh directory here you could just go and do CMD this opens up the command prompt you could have the following uh commands typed so first of First Command is I would uh you know I'll just turn off this because we don't need this for now and now what we're going to do is we could just go here and uh first man so we could go here okay and what we could do first is we could have git in it which is initializing the git um and then after that we have get add all the files so we just say get add everything okay now I didn't have to do slash but dot okay so this means I added everything then after I do get commit i m and I say first commit all right so you could just do any message you want I just did okay now after that we want get Branch hyphen M and then hyphen M and then we have main branch okay so now this is on the main branch then I want to get remote uh add origin and I have to put in the URL of that repo so basically just copy this uh and you have to put it in the URL path here so right there so get remote add origin and then once you have everything finalized you just do get push hen new origin and then main if you do this and press enter this would update this on GitHub so now you could see that everything is on GitHub which is great and you could see one other thing that node modules isn't existing here and the reason why I did that was because I put it in this uh you know. get ignore and you can see that get ignore didn't include the node modules which is pretty heavy and bulky and you don't want that and you could also put in your you know EnV variables your credentials and all these stuff we could go in the uhv file and then you could put in the EnV in the. ignore so yeah these are the things um and precautions I'll put this link into the uh description so thank you so much for watchingthis is a great beginner's course to learn the basics of backend development by building a crud API with node Express and mongodb basically the M Stack without the r Harris ifar created this course he is a popular instructor and experienced developer today we're going to be building a complete restful crud API from scratch using no JS Express and mongod DB so over here we're in Visual Studio code make sure you have it downloaded I already have have it and you also make sure you have node.js installed so just go to nodejs.org and install this latest support version and then just click on all the installation steps and once you have it you could test it out using CMD and just type in the following you could just say node hyphen hyphen version and this should tell you whether you have node installed or not and now once we have everything ready we can now go into our project so essentially what I'm going to do is I'm going to create a simple crud app folder over here and in here where we're going to do is we're going to open up CMD so just go here and type in CMD and go in this and you could just type in code and Dot this would open up into that same directory so now we have everything ready okay so now what we're going to do first of all here is I'm going to zoom in a little and I'm going to actually uh do the following first of all uh what we're going to do is we're going to open up the terminal using control and back tick is the shortcut key and over here you could have the command specified so the first one you want to do is you want to type in init hyphen y so npm in it hen y to initialize uh some package.json now you could see that this has been written and if you open up you could see that this is the package.json file over here and it has the name which is simple crud app it got from my folder name so you can see over here after that you could see if I put it on the right side over here and just open this up um or maybe my folder on the left side so let me just open that up and put that uh over here so let's let's do like this now you can see in the version is 1.0.0 Z and then the description is nothing and then the main is index.js and then we have the scripts so we have right now just test and right now there's nothing here and then only Echo error no uh T Test specified and then exit one keywords are an empty list an author is you know nothing and then license is ISC so what we're going to do is now we have all of this done we can now um basically create our um server.js file which is going to be inside of this place so what I'm going to do is I'm going to open up this into some little maximum screen and over here I'm going to create a new file which is going to be called index.js right main index.js this is going to be actually the brain of our uh backend so over here this is the index.js file and make sure like uh you just created it now if you want to you know write anything like suppose console console.log and you just say let's suppose hello there now if I if I write this right I want to actually run it to run it you could actually use that same terminal and if you just type in node and then you say index.js now one second I didn't save it over here so you see now it's saved the circle is gone and when I do this you can see that it's running but what I want to do is I want to actually use package.json to use this file so to do that all I have to go is within this um you know within this file which is in the scripts um and then it has this object which has test what you could do is you could just add in comma and just add in one more which is going to be called serve and the value to this would be node and then we' have index.js all right so this is going to be for now specifically this command now if I just have go back to my terminal and say npm run serve it would still run this exact file because I actually told package.json that whenever I use the serve npm run serve it should run run that command so you can see there hello there has been run okay and then you can see over here that simple crud app and then node index.js basically it it did the same command that we did before node index.js and it ran this command and you can see the results over there really nice so far so now what we want to do is now that we have our package. js on and index.js we want now to install our framework which is going to be Express for the back end so let's just go to the website and it's actually going to be called um we have to go to the npmjs.com website and you could see over here there's every single thing that you need to know about uh npm uh dependencies and uh you know libraries so what you could do is over here you could have the Express framework just typed in if you type in Express and press search what's going to happen is that it's going to give you this exact match so when I'm going to click on this you could see this is the official Express framework which is actually going to be used to build our node API server and that's why we're now we just created uh this but we have to create this node API server so over here you could see that you could use this command to install this so just copy this from the clipboard and now you could go back to this Visual Studio code and make sure you just right click here and you know um it should press enter and then you know it would install Express now once you see you could see node modules folder populated and also a package lock uh Json so you can see p package hyen lock. Json over here existing and what one more important thing that you saw over here is this node modules so this node modules will contain all of the dependencies and libraries that we'll be installing within our application uh but right at the moment if you go to the P package.json file now you'll see that in the dependencies we have Express and that means we have installed Express successfully so that's amazing now we have Express in our application we could actually use it's border plate code to test it out whether it's working or not so just go in there and you just go in here and you can see this is const Express require Express and then this initialize the app and then what you could do is you could just uh call this hello and then you could essentially do this so I'm going to copy these two lines up here and I'm going to go and replace this with this so I'm going to have on Express equal to require Express and over here we have app and we have Express so right after this is what we're going to do is we're going to just try um we're just going to try to uh run this so running it is pretty simple and simple easy all we got to do is you know we're going to try to listen it so we're going to say listen for which Port so we'll say by default we'll say 3,000 port and we'll say you know for that particular Port what you have to do is you have to console.log one minute so console.log now you might be seeing that there's a lot of things going on here I have co-pilot and that's why uh you know it's giving me suggestions so now you could see over here server is running on Port 3000 when I press tab it just autocompletes it so now we could see that ab. listen 3000 and then you could see whenever I go to this 3,000 it should have this message on the console screen so if we want to do this and you want to see it so let's just go and you know visit so but before that if uh what we have to do is we have to uh run this application so right now it's uh app. listen and uh you know it's going to now say server is running on Port 3000 so we're going to do npm run serve again so npm run uh serve and if you can see it you could see Server is running on Port 3000 which is awesome now our this uh node Express uh back end is running on Port 3000 which is fantastic so right here right after this and we're gonna say app.get and we'll have this forward slash right and in here we have request and response now these are two things whatever the client sends to the uh server is the request and whatever response comes back from the server is basically the response rest so uh the client is basically on the browser and I'll show and explain everything if you're not following along right now so if you could see over here um I actually had app .g get and then over here we had the Slash and over here we had requ request and response now over here we have this bracket right but we forgot to close this round bracket which is right here and now this is all complete but we want to do is that whenever we visit this default page we want to throw out a response from the server right from our server saying response. send and we'll say uh hello and then we'll say hello from node AP if I do this this means that the response is coming from the node API and this is going to appear if we run this uh you know on on our machine so essentially if we go to the Local Host Port 3000 it would work so let me go here and you could just type in Local Host and then we say Port 3000 um and you'll see that right now it's not uh it cannot get the thing so we had to basically turn off our server whenever we make changes and then we have to run npm run serve again because I actually added what I add what just server is running on Port 3000 so once everything was there um and you know this hello from node API with the slash this is what the response is sending but this is just on the console screen so now if I run this you'll see this is on the console screen which is this one but this is coming from the node API and this should come uh through our browser or something so if I go and visit this Port 3000 on Local Host there essentially you could go here and you basically if you refresh to the page with Local Host Port 3000 you'll see hello from node API the response that's coming back from node API this is amazing and if you want to see it through Thunder client or something just type in HTTP and then call in to/ localhost Port 3000 and then if you do this you'll have to make sure you have get and when you press send you'll see that the status is 200 means it's okay and then you'll see the size 19 bytes uh time which is 2 milliseconds and the message hello from node API which is a success now these are some tools that we could use to test our apis which is thunderc a really popular extension in nodejs which is highly recommended you could also go and test out using insomnia so we're going to go to insomnia uh it's a software so insomnia. rest if you go there this is uh the website that uh essentially design debug and test apis locally or in the cloud so get started for free make sure you select this one which is $0 per month and you could actually uh continue with your Google account uh we could subscribe to the free plan and just say subscribe you are one step away from improving your API and then we could actually get started with insomnia to get started debugging and testing API download the latest insomnia desktop application so we just click on desktop for uh application for Windows or download for Windows and over here we're going to click on this it's essentially going to download this insomnia software and also there's one more software which is called Postman so if you go to post man.com you'll have this other software that you could use to test apis and essentially you could sign in or create an account for here as well so over here essentially you could use for Windows so you can just download the 64-bit here and this is downloading as well so that's great essentially you could also uh you know sign up with your account it really doesn't matter um perfect so you could just allow access and this is your Postman interface all right so it's pretty similar and I'm going to explain this throughout this video so don't worry this is just how Postman looks like and also uh we're waiting for the last one which is insomnia so when I click on insomnia you'll see this uh pop up so we'll just wait until this is done and once it's done you could say uh welcome to insomnia the new version is the biggest one ever notable features and these are some of the new features now we continue keep uh storing locally in local Vault and enable Cloud sync and secure Cloud so one one collaboration with Cloud sync now available we're going to keep storing locally in loal VA we could also now uh sign in so we could actually just uh continue with our Google account once you do you could go back hi coding cleverly and then you're back so authorizing insomnia and now you're back to insomnia so this is your personal workspace and this is how insomnia looks like awesome so now that we have all of this out of the way we can actually test our uh you know API so if I test it it's pretty simple we could go and you know to that Thunder client and you see remember this test that we've done exact same URL you just copy and you go into insomnia you could actually go over here and this is your you know personal project and you say new collection new document and what we say is new okay so we say my collection or we'll say node API and over here we're going to create that once we're created we created that node API we could add some uh you know apis here so we have an uh you know add a add new folder and we could call it you know get and then we just create this folder and in here we could just have HTTP request and just paste in this and paste it from here and then you basically have the get option selected and you click on send once you do that you can see hello from node API this is perfect now you could see that this is working and this is our get so we have this new request we could rename this as well so we have this new R uh uh we could just rename this and we could say this as uh get API all right and then we could just rename it and there you go this is our now uh first one so that's amazing uh this is how it works on insomnia and we're going to keep moving on forward with this perfect so right after you know testing it out through insomnia we're going to actually now have to go and download a few important things one thing is we make sure you have git bash because we're going to be actually using uh things that we don't want to actually include so the first thing is we don't want node modules right so we use dog ignore and for this purpose what you're going to do is you're going to go to downloads and you going to go to get bash if you go to get bash you'll see this one and you'll have to download for Windows over here and then also the installation step and everything and once you do you'll have to include it into the path just click on next and everything and then if you go and uh want to check if you have git you could just say G hyphen hyen version and this should tell you whether you have get installed or not so that's perfect after that you could go back here and you could just into the um node modules you could just add in you know into the G ignore you could add in nodecore modules this means means that this is not going to be included when it's going to be deployed on um you know it's basically going to be pushed to the repo so it doesn't include the node modules folder which is pretty bulky with so many things and now um we're we're done with that and now uh we have to do one more important thing and this is basically uh something very important to do so what I'm going to do is I'm going to actually uh you know show the screen we don't uh you know we don't have a mechanism of when changing anything so suppose if I say hello uh hello from node API uh for uh you know hello from node API server if I do this I go try to uh I mean if I try to go back and see the uh and I see the changes so I'm going to have to remove all of this and if I tried to see this uh you know reflection I try to refresh it but I don't see any change from here the problem is that I have to turn off the server and constantly restart it whenever I make a change and then I could refresh the page and you'll see that change but this is not great we want to have this kind of mechanism that allows us to do this automatically and that's why U nodon comes in so we turn off our server and we install nodemon so what we type in is npmi we have nodemon and then hyphen D and if I do this you could go and see that this is would install nodemon and you could see where I uh brought this up from if I go to npmjs.com and if I type in node mod and if I search you'll see this one exact Mass simple monitor script for use during development of a nodejs app if I click on this you'll see this is how you install the npmi nodemon and that's what exactly what we did but we did hyphen D um so once we did that now what you could see is you could actually install it globally or you could also install it you know in a de development part so now you could actually verify whether you have it installed using in the package.json you'll see nodon is in the de dependen IES which is perfect we still have Express in the dependencies and what we're going to do is um we're going to just put it into the script so index do uh so essentially in the package.json where the serve was right so you're going to add another one so over here we're going to have development and in here we're going to say node modon so going to have node man and we say index.js this means now automatically it will reflect the ch es and we don't have to now type in npm run server but we can now have npm run Dev and when we do this it's right now server is running on Port 3000 and you could see right now this is how my you know my response looks like but now even if I do any kind of change it's going to be reflected and it's going to show that you know the server is going to restart so let's see if I had the uh hello from node API server uh and say updated and if I you could see now what's happening is I didn't save anything right now the file isn't saved but when I press contrl s you could see the server restarting due to changes and then starting node index.js and then you can see it restarted and then you can see over here that I didn't refresh this browser page but when I do you could see that the refreshed uh result came here which is perfect now right after this success now we're going to actually try to connect a mongod DB database to our nodejs backend so what we're going to do is we're going to go to no mongod db.com so we're going to go to mongod db.com so mongod db.com Atlas if you go here you'll have to sign in with an account so I'm going to go here and make sure you it loads up we'll sign in and then you can see mongodb Atlas and over here we could sign in you could use your email address I'm going to use Google ooth uh and I've implemented Google oath in one of my videos on my channel so if you want to see how this is implemented using next s or even JavaScript or even MN I could have a video available and you'll you'll see that process so now you could see over here I already had my mongod DB created but for you guys you'll have no projects so you can see that I have a couple of projects going on here so what we're going to see is we could just go and click on this button over here and say new project and once we have a new project you know we'll be introduced with some basic things and we'll just have to set up so now you can see over here we have the name of the project so you could just call it anything you want I'll call it node API all right so now when we have this is uh created we go next and we say add members and set permissions so we'll just give ourselves the uh you know permission and we'll say create project so once we do this it's going to create a project for us and now you'll see loading homepage now it says create a deployment so what we do is create and we'll have to go and deploy your database so we're going to have m0 which is the free one AWS is good uh Mumbai is okay and over here we'll have the name of the cluster you can name it anything you want I'll call it mine as backend uh DP it really doesn't matter you could just call anything you want and now over here we have uh you know create so after that you know it's asking me what are the bicycles here I don't see any bicycle to be honest um so I'm just going to skip it and over here you can see motorcycle so this is motorcycle all over here okay great so now we have that you can see over here how would you like to authenticate your authentic uh connection so security quick start it's basically username and password we could add in our own username and password so I'm just going to have admin or you could just have simply uh this one and you could have your password set so you know you could actually update it yourselves but you know I'm just going to have this password saved somewhere so it's important to save your password somewhere so I'm just going to paste it right here and you'll see that later on we'll actually use this so we'll just create user and then after that we have this uh username so we could just save this username as well essentially it's going to be in the connection string but I'm just adding it anyways so let's just put it over here so right right here okay so now that we have all of that we could go back down here um and we have my local environment and over here we have IP address so connection this is my IP address but we want to allow it to access from anywhere on any device so we say 0.0 0.0.0 and then we could say allow from anywhere now this is uh you know um not compulsory description you could write anything you want and this IP address just had to add that and now when you do this you press add entry and this allows you to uh you know add this IP access uh list you know IP access list is updated with this particular thing and then you could just finish and close congratulations on setting up access rules we go to overview and now we will be able to see our database that's uh backend DB which is good now you could do is you could go to the database part here and over here we'll have to go to connect and over here we'll have to go to drivers or you know connect to your application so basically just go to drivers access your Atlas data using mongodb's native drivers so over here we could do is nodejs and 5.5 or later just get the latest one npm install mongod DB which is very important so let's go and first turn off our server so we'll just have this turned off and then what we do is we just essentially have this in and PM install mongodb this will install mongodb for us and once it's done you know we could have this uh connection string so this connection string is the thing that we need so let's go back here into our application and what we're going to do is we're going to uh try to uh access this so to to do this it's pretty easy and pretty simple what we do is we go to um index.js and over here we have to import mangoose so if I go here you'll uh um into that you know there's a dependency Library which is called Mongoose and if you go to this uh npmjs so basically npmjs.com if you type in Mongoose this is the dependency DB odm so it allows you to access DB pretty easily so just install this npmi Mongoose and we'll just go back here and we'll just paste this in here so now we could have Mongoose there as well now you could see over here um after installing Mongoose you could have you know this thing which is Con M Mongoose is equal to required Mongoose and then after that we have es6 um you know essentially you could use it like this as well import mango from so uh you know you could use any way you want um so I'm actually going to use this one so I'm going to put it right there and once we have Mongoose we could have now the part to just add it so this is the way we could connect the DB so we have over here uh this is the exact way to connect it so we basically say mango. connect and we have the connection string and then you know we allow things like that so let's go back here uh I don't know why this is there so we'll just leave it like that and we'll go back here and now in here what we're going to do is we're going to have we're going to have um so mango. connect and then we'll put in the connection string here so connection string you already uh you know that I already have it so it's in my clipboard Okay so so it's in my clipboard which is this thing and no no it's essentially this is the connection string now let's look at this a little more deeply this is the connection string that was copied from mongodb you can see this is my username and over here they gave me a section to add my password so remember that password that we saved you could actually just copy this one and just overwrite it with this one so you know paste it right here done now after that we have ADD backend DB so this was our you know um cluster name that we created and then over here we have um slash and right here mongod db. slash we could add anything over here so we could just call it as our uh collection name so I'm just going to call it node um hyphen API so I'm just going to call it like this this is our collection name and right after that we have question and then right after that we have question and then we have retry wrs is equal to true and then we have and uh W is equal to majority so that's essentially it um and we could have this in the new line and say do connect so do connect and then when it's connected you could have a response that says if it's connected you could say connected to the database all right and then if it's not connected you could say catch and then you could have this you know the same stuff so essentially you could have uh console.log connection failed so this is how we're going to connect to the database let's see the file is saved let's run the npm Run Dev and let's see if we are connected to the database or not and running and connected to the so server is running on Port 3000 and we are connected to the database now this is really good but the best way to actually uh you know go around this is you can see that first of all we want our uh database connected and then we want the server to be running the database connected and then we have servers running on Port 3000 so that's basically uh the way to go now let's just refresh and turn off the server and restart it again so npm runev so you can see right now we have connected to database and then server is running on Port 3000 it's just another way to do it it's totally fine if you want to do the other way but I actually first try to connect to the database and then you know uh we try to run our server now that we have our database fully connected we want actually a model and a model is essentially something that we could use to store data into our database and we want to store some products all right so everything that you want to store that it has to be a model of some kind our nodejs application could use and then you could use it to send it to the database so that it could store it there so what we have to do is right here we have to create a model so let's go here and we could use uh close all and right here you could just CL click on this to collapse everything and then after that over here you could just create a um you know essentially a folder here so we could just create a folder inside of this directory and we could call this as models so models and then in here we could create our model and we have a particular naming convention and I could and I could say so do model do JS if I use this naming convention it's still all right and this is just good to remember like this is the model so we're going to use Mongoose to create our uh model so Mongoose uh is equal to require Mongoose cons Mongoose isal to require Mongoose and then over here we're going to use our uh you know product schema we're going to create so we're going to say product schema is equal to mango. schema and in here we have uh essentially this stuff okay and this is the thing that we have to put in so over here um okay so over here we have this and I just want to make sure that this is the object and in here it's going to contain everything so like this so over here we're going to have either name all right so we're going to have name and then we could have uh you know something like type and we could say you know it's a string type name of the product and then we could have a required field and we could say that it's true right it's it's required you can't have a product without a name and then you could have um you know a me message and you know like that allows you to enter a product L please enter product name so that's it and then after that we essentially we could have a comma here so you could you don't there's no necessarily need but you could just add it in case uh right here we have is the name and after that we have the quantity so essentially over here we'll have the quantity quantity and over here you could see that we have Colin and then over here we'll have type and here we'll have a number for the product so let suppos number and over here required will be um required will be true and you know we go to also have default value for the quantity as zero and then we could just put a comma here so essentially there's no need of commas at the end so you could just uh for these types but you could only but you should include a comma if you want to add another object here so another property over here like name and then quantity now suppose the price of the object or the product and the price would be a type of number and would be required and by default it could be zero okay the same thing goes for the image of the product so you could say image and then you could have Callin and then uh the type would be string and then it would be required which is true and well essentially you could either have the image or you can't have the image so a product could exist without an image as well so that's basically there and then you could just close that that was an extra right right after this object this entire one we put in the comma here and we put in time stamps calling true this what it does it allows us to have two more extra fields and that's one is created at and updated at so um it's going to tell us whether when we created this the time um and then when we latest you know create the latest update and it will keep be it will be tracked so we you could format document I don't have any uh you know configure I'm going to just use prettier this is an extension again just used for formatting and now you know you could see that this is all my uh you know schema and now we have to just allow mongodb to to use this so we could say product and we're going to be calling this as the model so it's product and we're going to say mango. model we're going to use the model and we'll say now over here this is going to be within the mango database now check this out we have to make it singular because it's going to add an S all by itself in the database and you'll see it and you can see right now it's in capital but in the database it will be all lowercase so it's going to be called products and it's coming from the schema that we created up here so this one the product schema so we can simply say product schema right here and there you go so now we have this product model and we can just import I mean export this so we could say module. exports and we have product like this so there you go this is the entire model for the product and now we are going to be using this to create products in our database now that we have created this model we're going to use this model to save data into our database so we're going to go to index.js and over here we're going to use that model so it's very easy to use and what we're going to do is over here you can see that there's this connection part right so we're going to just leave that connection on the bottom and over here we're just going to use um you know the slash this is just normal but over here right after this slash we could actually have you know the following so we could go here and we have app. poost which is going to be used to save something and in here we could sa you know/ API products right so we could just have uh right now I'll just have SL API SL products and then we have request and response and in here we have this now now check this out um what we do is you can see that there's this um completed but there's still something missing and that's this bracket over here now over here we could we could uh use uh you know response and we could say data received or something we could send a message right now let's just test this uh API out does this work so/ API SL products if I go there so I normally would assume that it's running so if I go to SL API products let's go here and we'll say slash API and then we have slash products if I do this you could see over cannot get/ API products the reason why I cannot get because I actually used post here and when I use post it will say data received we could actually test it out and you know we could just have something really easily uh written now remember whatever we given to the the client it's in the request so whatever the client sends is basically a request so we could have console.log and we have the request. body whatever the client gives and we want to show it in the console screen right here and we also want to show it in our uh browser so we could do in the client end so we could say response. send and we could have the request uh. body right here as well so let's see how this looks like but we can't actually really test it out um through that browser so the best way to do this is either through insomnia or the other uh Thunder client so over here we're in insomnia and what we're going to do is you can see over here get and then we have get API so we're going to have a new folder here I'm just going to create a new folder and I'm going to say post uh right so I'm just going to say post and I'm going to create it so you can see over here we have get and then we have post so I'm just going to have this closed yeah so this is open and this close uh HTTP request yeah so in here we're going to have an HTTP request and over here we're going to select post and over here we're just going to paste in that HTTP call in for uh um to4 sles Local Host Port 3000 and it's called SL API SL products once I do this you could see that you know this is coming there and essentially if I send you'll see nobody return for response the reason why it's no response but the request has been sent is that I don't have any body so what I have to do is I have to give some Json uh values over here I could actually just have you know anything I want so so so over here what I'm going to do is I'm going to say in quotation I could say name and I say coding cleverly okay now what is this this is actually Json and I'm passing it so it says name and then it's also in strings now if I send this you can see this has been sent but the body still hasn't been responded back and you can see that the result over here is undefined the reason why we're getting undefined here is that we're not allowed to pass Json through our nodejs by default we have to use a middleware we have to configure it and uh the configuration setting for this middleware is super easy um what we could do is we could add that middleware part so it's basically app.use and then right after the uh you know initialization of the app so essentially this could be over here we could have app.use and we have express. Json and once I do this this means now it would be able to view it so now if if I passed in coding cleverly you'll see that then return was coding cleverly and when I didn't give anything you can see I could see an empty list and but when I give it you can see I could get this coded cleverly same goes for this now now now this is not going to be anything but you know you you get my point now you know we basically fix that up and now that's basically it now instead of having you know that client but client could have just add in anything he wants like suppose not just the name but you could have let's suppose value and you could say uh you know um 1 2 3 and you don't need uh any string for that and when you're finishing it don't add any comma here so now we have send you can see now there's two things from the response right so this is pretty good but what we want to do now is we want to add multiple things in here so let's go back here and what I want to do is I want to add it through you know basically I want to save the data uh using the mongod DB um model that we created so we use try and catch okay so we remove that and we use try and catch and we use console.log so for the error it's pretty pretty simple you could just use console. log or error this is just going to show in the console screen but other than that I mean right now you could see if uh the you know remember that console log that I was bringing now you could see it over here but now it's changed because I am actually changing it I'm using response when uh you know I'm going to show it on the browser and I'm going to say if uh you know if if it's not done so we'll say a response status of 500 meaning a server error and we'll have uh sponse and we'll throw an out of Json so we'll say Json and in here we'll have an object and we'll say message and the message will be the error message like like like this all right and then for the try now we could actually try to save that data so we're going to have to import our model so remember this model that we created product. model. JS what we could do is we could just import this model here so it's pretty simple and all we have to do is const and we say model so we just say product and then we say require and then we have/ models SL product. model and you can just add in JS so that it doesn't have any kind of issues but uh you know just a good prevention here and then once it's done here and we could add in this over here so we could just say um in the tri part we could have con um you know essentially what we want to do is we want to save this product so you want to save it and the best way to do it is await product. create all right you're using this command and it's essentially going to require a weit because it takes time and whenever you do a weight you have to put a synchronous over here okay so as s YN as s YN okay as s y NC okay async and then you have this await so await product. create um and whatever the requestbody was you pass it in and you basically create an a property here uh an object um you know for that an instance for that model so now over here we have this as a product we could just name it as product like this like like this so we have cons product and then we could just return that product whenever it's created and we could say you know as a as a response Json so we could say response. status and we say 200 meaning it's a success and the Json we could just throw out the uh product like that okay so now this is going to be really awesome because uh for now you can see that we have some fields which are name and quantity and price and image so there's it's more than you know there's extra fields which are time that just automatically being created but these are the three important ones they are like mandatory and this is optional image field because it says required false so that's test this out using our insomnia so over here what we're going to do is we're going to create a new folder and we're going to be calling essentially we're going to create a new folder and we're going to be calling this as add a product okay and when I do this I create this folder you can see this folder is here now in here you can see this is open and now you could essentially have this click and you say HTTP request and you could just rename this you could just rename this over here and just call this as uh add API Okay so just rename now you can see this add API and now you could just have the you know the same thing colon two for slashes uh Local Host Port 3000 and then we have SL API slash so what is it SL API slpr products right so whenever I do this this is that API that what is existing but it's actually a post not a get because we want to add something and in the parameters so in the parameters which is in the body so we want Json right we have click on Json and over here we could just put in something like here so first of all you have to give them uh the fields now look at the fields we have first of all name quantity and price and we have name as only string so we'll have name and we'll say let's suppose we want pizza and then we have quantity so we'll have quantity in know the keys always going to be in string so quantity and then we have colon and then quantity is a number so we'll say 10 and then price so price like this again and then we'll have price as let's suppose uh 5.99 okay so now we have this which is there we could just simply press send and this should add it to it so we could press on send so now you can see over here we have these three you can see this ID has been created and then there's this created at and updated at and then this underscore V this indicates that it's it's successfully saved in the database because mongodb gives an ID unique ID to every single object that's being created and now we can actually reflect and check it in our database so if we go here and we go to browse collections you'll see that remember uh like I told you before that there's going to be um this product and then there's going to be an s in here and then this is going to be lowercase and it's going to say products right and remember that cluster name that we created which was um node API and inside here we have this products right so this was collection uh this is a collection my bad and then in here we have our table which is products and you can see over here that our pizza is there so that's it's been saved successfully which is amazing so this is great we could add some more uh products as well let's suppose we have pancake so pancake okay and we'll have quany as let's suppose 20 and the price for a pancake is $3.99 send it now we have pancakes added let's have um you know donut donut um and uh this would be 40 donuts and the price for a donut is $1.99 press send and then donut is added okay great so we have some products added now if you want to view these products how can you see them well you could see them over here if I just you know click on refresh here so if I click on refresh you'll see the these uh products are being updated here but I want to actually have an API that allows me to you know view these products there so that's pretty simple as well just go here so the post was created now you just want one that allows you to view these um you know listings so what you could do or these you know products so we could app.get and we could just simply say/ API SL products and then this is asynchronous and then we have request and response and then in in here we have this body and then uh essentially this close and that's it so in here what we're going to do is we're going to have the you know the the the area where we're going to have to retrieve it so and we could view it so try and catch again the same thing response. status um 500 and it's going to give a Json you know object and says message error message and over here in the tri Place essentially you use a weit and you use product. find you want to find everything in the database so you could use this um bracket and in whenever you use a weight there's an async automatically that has to be over there and now what you could do is Con and we say products like that because there more than one product uh now what we could do is we could have response. status 200 and we could just J on the products and that's essentially our API that's all that's pretty much it now we could use it and test it out over here when go this we could go and just create a new folder so we could say new folder and we going say uh get so we say get all products and then we could just create this I mean this is not required this is just insomnia so uh so over here we could have you know HTTP request you essentially um having without having it over there you you could just you know you could just close these out so you can see you could close this folder you could close this folder you could close this folder and then this is get all the products one right so you could click on this one and you say I want I want an HTTP so HTP request um so you could click on this so it just created three different https right so we want to delete U the multiple ones that it created this is just an organized way to do it you could use Thunder client it's super efficient and easy but I'm just showing you that there's so many Poss abilities here um and over here what we're going to do is uh you know you could just rename this using this and you could just say instead of new request you could say this as um get API and press enter and then in here what you want to do is you want to call this API which is HTTP call-in Local Host uh two for sles Local Host Port 3000 and then we have slabi SL products okay and if I press get and this press send you could see that mongodb gave me a list of objects so you can see see this is a list and then inside here this is object one object two and then we have object three coming from the database this is amazing and this is how the get one is going to work but suppose if I want one individual product instead of all of them then I could use their ID to get them right so if I want the pizza one I could just use this ID to get it and then this one to there and then this one for donut and this one for pancake how do I do this also pretty simple what you could do is you could just put in an ID there but we have to create that API for it so over here we have this get plain one and in here we could just have one more which is right here on the bottom and this is just a specialized one that allows you to get a specific product depending on its ID so we could have you know now essentially these these double quotes or single quotes don't matter you could add whatever you want but it's just a good practice to have the same thing whenever you're using it so don't mix it up so so over here we have I'm using single codes here for Now so/ API products and then we have slash colon ID and this is how we're going to use it so we're going to have asynchronous function request and response and then we have this um and then we have this and then we have the semicolon and over here what we're going to do is we're going to use the same try and catch method and in the C uh catch we still have response. status uh. status okay status and then we have 500 and then we have Json and then we'll give that you know message saying that you know it's an error okay and then in the try place what you could do is you could actually get this ID how are you going to get this ID from the URL so you might be asking like how can I get an ID from the URL so right now if I do this you could see this is a list of the you know get command that we did and this is giving us the the API uh I mean API for all these uh products and the are three but right now it's really hard to uh read them you could use some kind of extension uh Json reader uh Chrome right so if you just type in anything like this uh it's going to give you some cool extensions on Chrome so this one's good which is featured you just add to Chrome and when you add this extension uh you know this uh Chrome extension should be added so Bon viewer and once it's there okay so we have this extension right and when I refresh this page you can see now it's essentially now in a really nice formatted portion and you can see over here they also gave us like what is the API that we called and this is the retrieval Pro process so it's pretty nice I mean everything is you know pretty well made so great so this is the API and like I was saying is uh like I want I want the URL where I could say slash ID like I want this ID so suppose if I say I want this pizza and I get this ID so I copy this entire ID right here and I have put in the URL which is right here now how can I get this url's ID how do I get this slash and then I get this part for that you have one specific thing and I'll tell you how to do it it's really easy and really cool it's called um prams so essentially we could use it use a destructure and we say we could get the ID from request. prams if we do this we basically got the ID from the URL that's all we had to do and now we could just uh search this in and retrieve it so we'll say await and we'll say product right so product is our model and that find by ID is a command so we could have that and we pass in the ID okay so we pass in the ID but we also have to now just say nothing else and also we just have to find this and we'll put in a cost product like this and then after that we could just ret retrieve that status uh 200 and we'll say you know that Json product like like like this so that's great so now we have this product uh available as well when we go back to our browser you know you could either go through the browser and you could see that automatically it did it I put this and then it gave me the pizza now let's just test it out using test uh you know this insomnia as well so right now this is get all products so again I could just create another one so let me just close this folder and create a new folder which is over here and it we'll say um get one product okay and then create this folder and over here we going to open up this folder and make sure we have an HTTP and in here we could just name this as um single API I just named it whatever I want and then over here we could have HTP colon 2 sles Local Host uh so Local Host Port 3000 and then we have SL API SL products because it's there's an S and then uh Slash and then the ID so remember the pizza ID I have right here in the clipboard and what I have to do is in here it's just get so nothing else and over here I'll just send and there you go we have the pizza so before we move on to the edit or update API what we want to do is I just said let's you know that single API where we get one product let's just have that named as product instead of products because this is multiple products and this will be single product and all you got to do right now is nothing else but just have this call to single product instead of products because if I call it with multiple it will get an issue and it says cannot get this but if I go and say this because it gave me a 44 it says it doesn't find it but when I remove that s and I press enter you know it still works so that was just pretty much what I wanted to do and you know that's pretty good uh you go to refresh and you could get the same kind of issue here uh but when you move on to this part which is this you can see now this is there and you know everything looks really fine which is great it's just super awesome now let's go and now create the edit update API so over here what we're going to do is we could go to this products and right after this uh / API products which is just uh allowed to post a product we could now create update API so I'm just going to put a comment and say this is as update uh a product okay okay so what we could do is here we could have app. put put is the command to update you could also use patch to update and this is HTTP method so put is also used to update and Patch but we're using put and over here we're going to have single quotes and we'll have SL API SL product so we'll have Pro uh product and we want to update a particular product based on its ID right so we could use the ID again and now once we have this we have an async request and then we could have this and then this all right so now when we have all of this we could use try and catch again and over here again over here we could to have response. status um and then we'll have 500 and then we'll have json. Json and then we'll have um you know object and then we'll say message and then we say error message okay that's great but in the try what we could do is we could essentially get the ID again from the con ID destructure it and we can say equal to request. prams so this is the ID that we got and we can use this ID to update the thing but first of all we have to first uh you know uh use that command so it's await and then we say product and there's a specific method which is called find by ID and update so find by ID and update so like this yeah so basically this is the command it's called find by ID and update and and we pass in the ID and we pass in the things that we want to update so whatever the user said in request do uh request. body that's what you want to update it with so essentially this is going to be like whatever the user gave you want to update it with that and you want to put that as the cost uh you know product so this is the product like that so this is going to be the product right after that so suppose if the product hasn't been updated if I say you know if the product doesn't exist or something that means it just doesn't exist you could say response do you could just say return and then this would stop the thing and it would say response. status 404 and say message product not found um which is great we found a reference to public code in a recent suggestion uh but now after that what we have is if this is not existing I mean if that uh now if it exists what we could do we could actually update it and we could just say product like this but before this uh there's a check here because uh normally the product is going to be updated so it's best to have it rechecked from the database so you could use it either using uh a weight again and say uh product and then you say dot find by ID now essentially because it's already updated right it's going to be reflected in the database so it's better to just check it again for so find by ID and and then you put in the ID over here so let's put in the ID okay so you essentially put in the ID and now what you're going to do is const and we say updated product right here and then equal it and then we could just return so basically response. status 200 meaning Json and then updated product so this is the API for updating a particular product based on its ID and now we're going to be testing it so let's over here um we created another folder so I'm just going to close this one and you know you can see all these folders here and now we could have one more folder which is going to be called update a product okay and then we press enter to just create that folder and in here we just have this button and we have HTTP request but in here let's just have update API okay Enter and now over here we could just have HTTP call in to/ Local Host Port 3000 and then we have/ API SL product because it's not going to be S and then it's going to have it ID so if I look into this you could see over here it's product and then it has its ID so we could have the pizza one we had the pizza's ID I believe it's this one and now if we want to update it we have to go and put in put and in here we have to go in the body and we have to go in Json and we have to change the name or whatever we want so let's suppose we want to change the name and we'll say and we'll say it up uh updated okay I'll say updated Pizza okay if I do this and I press send now you could see that this it will return the entire thing the reason why it's returning it is because I'm actually saying give me the entire updated product by finding it again from the database and now you can see that it gave the name updated pizza but other things were just the same if I just uh you know say okay now I want the price changed as well and I say from $5.99 we want the price uh increased to $7.99 okay now if I do this and I press send now you could see it's going to take sometime and there you go it's updated so this is the API to update a particular product within the database and it's updated now successfully and now if you want to view all the products so you could see get all products so you could see over here if I click on this API so this is a particular thing if I just you know press enter you can see this updated Pizza is there $77.99 but the pancakes and the donuts are still existing so this is beautiful I mean we could go back and we can update over here so this is the convenient part of uh this um you know tool which is insomnia or Postman because it's really organized instead of having Thunder client Thunder CLI is very simple and you would just pass it in but you know every tool has its own advantages and disadvantages but you know um just look at for the you know the good things of everything so look at the good parts of insomia and use it all right so next part we're going to do is we're going to have the delete API so now that we have this update product let's have the delete product so right here um we're going to have slash delete okay so we're going to have delete a product like like this and then over here we have pretty simple uh method for delete it's just called app. delete this is the option and then in here we're going to have SL API SL product and then what id you want to put pass in so you pass in the ID like colon ID and then you want to uh put an asynchronous function so as YN C and then you have request and response and then put in this and then this okay and then put in this now if you want to delete something it's pretty again simple you have try and catch and then here you could just have uh response. status uh 500 and then you could say um you know do Json response and then put in the message and then just say you know there was an error like like like this or else if you want to try to delete it again use the con ID destructure it from the request. prams and then um what you're going to do is right after destructuring it from the request. prams you could use it using await and and then um you know a wait and then you say product um so basically oh man what did I open so what if I do product. find uh you know it's basically find by ID and delete so find by ID and delete and then you pass in the ID and essentially you want to uh you know you delete it like this so that's pretty much it and then uh once it's deleted you could just have the product saved again uh just return the response of it it so just say that you know this product has been deleted or just given information that this this was the product that was deleted to the response and then you could just say Okay response okay so for instance we have to put a check first if the you know the product doesn't exist so if the product doesn't exist so so what we're going to do is we're going to throw out the return and then we say response. status and then we have 400 uh you know 404 and then we'll have Json and then we'll have the message of you know process not product not found and then you know if everything is fine we have response. status 200 and then we have product deleted successfully uh which is great um and you could also just have the product uh you know displayed or something but uh you really don't want the product so you could just have you could just have it like like this okay so now we could just format document like this and you could see this is our delete API which is right here so this is our delete API now it's just time to test it so right here you can see that there's um no product here so that's the only uh reason I was putting it so I think it's best to have it uh so cons product wait a minute Pro product okay okay it's okay so now this is okay that it tells us whether it's not and then after that it's just going to delete it great so now let's uh test it so we go to postman uh we don't have to go to postman but insomnia and over here we could just create another folder just close all these things out so you could just have this closed and this closed and just create a new folder here so we say create a folder and we say uh Delete delete a product okay so we have delete a product like this and then in here we have HTP and we have um so delete API right and then over here have HTTP uh colon to/ localhost uh Local Host Port 300 SL API SL product and then we could put any product we want let's put our uh you know famous pizza and we'll have uh you know the post option which is O I mean sorry not the post but the delete option here and then all that's we have to pass in and then press send and you can see over here message says product deleted successfully if you go to get all products you know over here the folder and get API this is the get API saved over here so we just send it once more and now you can see now we don't have pizza anymore but we have pancakes and donuts so let me know how you thought about this if you have any questions uh like if you want to add more products into this just add them quickly with that you know add a product API which is right here and just add them quickly and easily within this and then it would just be updated here let's suppose we want to add one more let's suppose uh cheesecake and if I say um so let's say 100 and then um $4.99 now if I send it now the cheesecake is added there as well okay so now you could see if I you know view all the products uh which is over here which is in the get all products now I send it now you can see pancake donut and then cheesecake is right here which is amazing now this is uh the apis the simple crud apis create read update delete I wanted to say that you know you could actually have Postman insomnia and all these other products so it's pretty simple over here as well so this is Postman and what you do is you basically create a colle you know workspace here so um essentially you could add in you know things like my environment environments and you want to add new http right and then once you do that you could just paste in your you know HTTP so suppose if you want to use this you know this one this is allowed to view everything just paste it right here press send and you could see now a response came here so you could see you got a response here so you can see one two and three over here and one other thing that I wanted to say is that suppose you want to add products right so you have added API but instead of adding it through Json you want to add it through suppose some other format like a form URL like this right so in a form URL you basically put in uh just a key and value so suppose name and then I want so this is like yeah this is the name and then I want something like uh something let's suppose I want uh chocolate chocolate add yeah so add so I have name chocolate and then I have price and I say um you know 3.99 and then I have the last parameter which I guess was um quantity so quantity it doesn't matter which order it's coming from but as long as they match so let's suppose uh 35 okay so this is chocolate I want to send this I can't do this product validation failed name please enter product name now the reason why this is happening in like 500 internal server is that our middleware isn't configured for adding these other things uh you know this form field so you know adding this form way you know you form URL encoded uh method of adding things and you could just configure it within the uh middleware so right here where it says uh express. Json you could say app.use and then you could have uh express. URL encoded and then you know you could have the you would have the round brackets and then the braces and say extended Callin fults if I do this now you could add this thing as well so let's just send it again and now you can add it so chocolate has been added and now to view all these products you could see over here where we view all the products uh add get API so in the get API if I press send now so get pancake donut and then we have cheesecake and then we have chocolate all right so that is amazing which is really awesome now I wanted to do a little bit Advanced and I wanted actually to structure this out a little better because right now to be honest we have everything cluttered within index.js and if we had more and more apis created we would have to make our index.js very very populated which is not which is really not a good practice at all so the best way to do this is essentially have everything separated out in really good uh folders and this is what I uh High highlighted out in one my videos so what I would do is right now I'm in the models which is great we had a separate one but over here we have to have these API so you can see/ API SLG get and they're all the related to products right so you can see this one's related to product this one's related to product this one's related to a product this one's related to a product and this one's so we have all these apis that are related to a product so mostly we have to create a separate route for this so so what I'm going to do is right now uh you could see that um we're going to create one route over here so over here we'll have a folder and we'll say routes so not in here not in the model but let's uh collapse everything and basically we're in the root directory and we'll we create a folder called routes okay so in this we'll create a file uh when it's going to be called um it's going to be called Product route um product. route. JS all right this is going to be the product route okay so essentially this is for the products so basically you could call it um yeah so you can just call it product. route. JS and all the routes uh that are leading to that product will be there existing so what I'm going to do now is I'm actually going to have you know these apis so if you could see over here we have this all there right delete so this delete is there this update is there this uh post is there and then this get is there so there's two post I believe so this one and then there's this one okay there's get and then there's this get okay so yeah exactly so there's just one post because we you have to add one product and then there's two gets okay so what we want to do is we want to add the URL routing here so what what it is like it's basically right after you know this slash this is normal it's really doesn't matter so right after that you could add in so essentially right after this so you don't even need uh you know so app. that right here you could just add in so even after this I mean just right after the middleware configuration what you could do is um you could say app do use okay so I'm just going to call this as routes and what what is this is going to be called middleware so you just have to separate the things out over here so routes and over here app. and we're going to have the route which is basically SL API SL products so you can see over here there's a similarity here so uh that's why we're going to use Pro whatever SL API products go we're going to take it to the product route so product. route dot you know whatever that product route is so I'm just going to call this as product route now right right here now this is not imported so essentially I have to create the route okay to make this file more distributed and more organized I would recommend having product. route.jpg for a single product we'll just have this as well and this is for uh you know adding a product and then we have um basically that's it so this is just for getting the product and then this is there and this is for adding up single product so what we're going to do is over here we're going to use this app.use and then SL API SL products and we have the product route now this is the product route which is right here right so what we're going to do is over here we have in the product route we're going to have to create this router so you see this router and then this router will point to other things like suppose router. getet and then we have uh you know just for slash because right now at the moment what you could see is um SL API SL product so by General um means this one has to be called right this one has to be called which is uh just getting the products and displaying it so I'm saying why General the general one which is the slash one right so this one should have uh should have should have uh you know request response and then you know you could just say something like that it really doesn't matter I mean at the moment uh um you basically could have this thing there so this entire thing could essentially go here and this means like now it's asking for the product right where's this product from so that's all you have to import cons product so we say so product and this is from the model and and this a weight is going to be for this asynchronous over here okay and that's basically it this is how this model is going to be called it's still going to work but a better way to do this is actually add this logic this thing this entire thing is called a controller function and you could add this separately into another f file which is going to be called a controller so if I go here into this folder okay so over here and if I go here and I go in controller controllers and in here we could add in a a separate file which is going to be called product. controller.js all right so over here you could add in this entire thing this entire snippet this one okay so essentially you don't need this entire snippet at all so all of this could just just be cut okay and then over here would just be a function but at the moment I'm just going to tell you what to do just leave it like this as for now but let's go here and in the controller in the product controller what we would do is we have const get products is going to be the function and then we'll have is uh async request response and remember that the thing that I copied so let me just add this remove that remember that's async of request and response we B we basically need only this so rest everything you could just delete so this could just be deleted and right here you could just paste this try and catch now it's going to be asking like where's this uh product coming from you could just say Okay con product uh this is coming from there uh and essentially this is your function if you want to export this function so that you could use it you just have um the following line added at the end so you could just say um yeah so essentially you you could just say module module. exports and then you you could pass in a list of what you want to export so you know right now at the moment we just have this get products being exported so now you could use this get products here so you could just have this uh route and in here we could have um const that product controller and then that's coming from product. controller.js okay and then over here we could use uh that function so essentially you're getting product out controller and you're essentially you could get the the structure and over here you can see that there's this get products so we could just say get products we're getting the function get products from that and we could use it over here here so get products right here okay so now that this is there you know you can see that this has been added which is Success now you wouldn't you wouldn't want to do the same thing process for all the other routes so like like suppose if I uh you know go to this other one which is this was just getting all the products would have router do get and this is so simple like suppose forward slash and then we have uh you know here Callin ID so Callin ID so this means we have now router for a specific ID right we're getting only that and then now you could understand this through this you could see SL API products and then whatever now this is going to be an ID in the URL so right right after that/ API products and then you could have uh you know get single product function instead of get multiple product and what you're going to have to create this function so we have to create this function in here so all we got to do is you know con uh you know get single product so product and then again this is an asynchronous function so asynchronous and then we have request response and then we have you know this semicolon and you could just add in you know the things that you need so essentially the index.js has the single function as well so over here now you basically don't even need this now this API is deleted because you already added it from there and now I'm going just you know one by one finish all of them and then just going to be using this route to do all of this handling so right now product. route is also not being exported so product. route is going to be exported through this function I mean this this line which is module uh module do exports and then we have router all right so once we do this we could actually simply import product route uh on the top so so if I go into this I could just have the you know router imported on the top right here so over here I could say um const product route and then we have routes and product. route. JS and then this is basically the product route which is right here so this is perfect all everything is synced properly and now we just have to create this product get product so we just get this you know get this I this code and we just copy this and we put it in this grit product which is right here once everything is done we could just have this um get product like this okay so once we have that we could now we could now um go back to this route and you could you know you could actually call it from here so get product simple and now there that's populat so now we have these two apis so by that you know we had five I mean we had total five so now three are left so let me just quickly add the last three um and one was simple which was the create one which is router. poost and that's used to create so this is also a for slash just a simple one and what we're going to do is we're going to have a post product so it's going to be basically create product function um so we could have create product over here create product function and we have to uh you know make this product function over here so over here we could just have cost create product okay and then we have is equal to async and then you could see all this populated and you could see that it's basically creating that product here so this uh SL API products this is this is already the old one we could just remove this and this is the creating one so you could see almost it's the same thing but I'm just going to double check if there's anything different I believe it's almost the same thing so I'm just going to copy this it's yeah almost look the same so you could just remove this now safely and you going to essentially go here and this you know create product is created so you just use it right here so comma um create product and then what you want to do here is you want to you know you could just have this there's also this part over here you could just go here and control and space and then press enter that's basically adds the create product here and the same thing goes for the next one which is the update you know update a product so you just add update a product right here remember that uh update a product so essentially it's also router dot now how to update remember put and then um when we want to update something we have to actually have its ID so call in ID and then we have you know the function which is called update a product so update product okay will be the function and we don't have it right now so we'll just have to add it right here so it's pretty simple again all we got to do is Con update product update product and then what we can do is async again is equal to async request response and then we just put in this semicolon right here and then over here we just want to put in the update product so this this thing will just go in which is this entire thing okay just copy this entire thing and bring it in here so like that and now uh once this is there you could just remove this entirely so this update a product is removed successfully from this index.js uh and this should be essentially like that okay so now we have that and you know it's complaining so just go here and press control and space okay it's not giving me any suggestion because it's not exported through that list right so we have to go back here and we have to add this through the list so we'll have comma and then we'll have update product and now it's going to give me the suggestion so it's going to go here and just control space and then there you go the last one is delete one so it's basically called delete delete a product and then over here it's router. delete and then we have its ID so SL Callin ID and then we have the delete product function okay and then normally we have to do the same thing over here so we have to go here um right after this function we have con delete product okay and then we have async uh async request response and then we have this we could put in again the same thing from index.js this this thing will be from here all the way to this catch so copy this entire thing and just paste right here also you could just remove this entirely now you don't need this delete as well and you could just you know clear it all so essentially now we just have the single route so look how clean this index.js has gotten now what we have to do is just finalize everything um so over here we have this delete and over here we're just going to have comma and then we'll have delete product and over here we'll just put in the delete product so we make sure we have um control and space and then delete okay great so now that everything is there I mean it's imported over here and it should be working SL API products um so so everything seems to be fine and you know we have format document just to make it a little formatted and let me just show you a little zoomed out view so this is the uh you know the delete controller this is the update uh product controller and then the create up product controller get product controller get products controller so you can see that this is all requiring model and this is all working so p is a common uh JS module it can could be convered to an es model so it's it's okay I mean you could use the um you yes six way of doing it but you know we're fine with this but you know everything looks good and we have these routes that are configured in the product. route and these are all going to call these functions so you can see if I just normally do get and then I'll call the get products if I normally have get with this ID I'll get the get product function uh different from the get products function and if I have normal uh URL of/ API products uh then it would have with a post method it will create a a product for me and if I have an/ API products with an ID and it's a put method it's going to go to the update product method which is right here and if I go and do/ API products uh with a with an ID and and it's a delete method I go to the delete product and basically it will delete the product for me so this is pretty really I mean really convenient and organized looks very nice and this is how it's professionally done so you can see over here that we have all of this done middleware routes and then this is just normal you know testing hello from node API and this is our connection so let's go and see if this is uh still warning and you can see that the database and server is still running let's go and now what we're going to do is we're just going to add in a product again again nothing has been majorly updated or changed so we could just go to um back to our you know that API let's suppose I go to add one product so add a product so you can see I could go add a product so over here we have SL products right going add through form or we could also through Json so I'm just going to have through form again um and let's have let's say um we have juice okay and juice is for $5.99 the quantity for what I do okay the quantity for juice is uh 100 or let's say 210 I don't know so that's send it and now you can see juice is added I mean nothing happened I mean it's everything is working okay so if if juice is added can we view all these so then just copy this and you could actually view it in their browser as well so if you do this you could see that all these lists are working I mean it didn't basically impact the API performance of pancake doughnut cheesecake chocolate and juice have all been there nothing is being broken and nothing seems to break everything seems to work so you could see if I want to um add uh this is the ad so get API so if I just send you can see now all of these things are being populated here if you want to you know delete anything suppose I don't really want you know cheesecake I could just get this ID first of all let's copy this and we can actually update the ID as well right now try to update Pizza it's not going to allow you because uh it can't find it 404 right we could put a really good message over there and that's something that I want you to put a message using you know response but right now you can see that this is the now I changed it to the updated uh so cheesecake so cheese I'll just write it and then the price and then I press okay so now over here we have slash API products now this is important now whenever when I you know I did the organized way to do the route and controller I changed all of them from now so everything has Now a/ API SL products now if I do this you can see that the cheesecake has been updated and you could view this uh cheesecake by using get API and this would uh basically send it and you could see over here pancake donut updated cheese okay and then we have chocolate if you want to delete the cheesecake uh you could just have this you know from here with the ID passed in but again the product will be changed to products and this is the delete method and now if I press send you can see product deleted successfully and if I want to review all the products so I'll do here and I press send again and you can see pancake donut chocolate and juice so one final thing I wanted to do was to add this into GitHub so this code that I added I wanted all of this to be populated on GitHub so what we could do is we could go to github.com so what we could do is essentially I'll go to my GitHub account so and then over here we have this plus button and create a repository and we'll you know we'll call it simple uh so we'll say simple crud app and we'll say backend okay and then over here we'll have it public and we'll create repository and then over here we could just initialize using this command so make sure you have that so what you could do is this is the uh directory here you could just go and do CMD this opens up the command prompt you could have the following uh commands typed so first of First Command is I would uh you know I'll just turn off this because we don't need this for now and now what we're going to do is we could just go here and uh first man so we could go here okay and what we could do first is we could have git in it which is initializing the git um and then after that we have get add all the files so we just say get add everything okay now I didn't have to do slash but dot okay so this means I added everything then after I do get commit i m and I say first commit all right so you could just do any message you want I just did okay now after that we want get Branch hyphen M and then hyphen M and then we have main branch okay so now this is on the main branch then I want to get remote uh add origin and I have to put in the URL of that repo so basically just copy this uh and you have to put it in the URL path here so right there so get remote add origin and then once you have everything finalized you just do get push hen new origin and then main if you do this and press enter this would update this on GitHub so now you could see that everything is on GitHub which is great and you could see one other thing that node modules isn't existing here and the reason why I did that was because I put it in this uh you know. get ignore and you can see that get ignore didn't include the node modules which is pretty heavy and bulky and you don't want that and you could also put in your you know EnV variables your credentials and all these stuff we could go in the uhv file and then you could put in the EnV in the. ignore so yeah these are the things um and precautions I'll put this link into the uh description so thank you so much for watching\n"