Build Restful CRUD API with Node.js, Express and MongoDB in 45 minutes for Beginners from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to Destiny welcome to node API development Series in this tutorial I'm going to show you how to develop node.js API with mongodb and Express framework first of all I want to recommend you to install node.js on your computer so if you don't have node.js make sure that you install node.js on your computer go to nodejs.org and download long-term support Edition and double click on the files when it's finished download and click next until it's done when it's done you can go to Powershell to check if it's installed on your computer completely okay you can type node-b it will show the version that's installed on your computer all right this means you you have successfully installed node.js on your computer okay The Next Step let's create a new folder over here I view name is node API and then just drag and drop this folder on Visual Studio code to open it with Visual Studio code all right the next step what you have to do is to go to new terminal and then you can type npm init Dash y to create package.json you will see package.jsons over here all right let's create new file server.js this is node.js files because it's JavaScript files so actually you can run these files with node command for example I put console log over here I type hello and then I can run these files by type node and then server.js okay server.js is the file's name which is over here I can hit enter it will show hello over here you will see hello here I want to run these files with package.json so over here I can put serve and then I can follow with this command note server.js so if I want to run this files I can type npm run serve serve is this command which is specified over here I can hit enter it will show hello over here alright The Next Step that we are going to do we are going to install Express in our projects okay what is Express Express is node.js framework which will help you to create node application easily all right you can go to npm js.com and then search for express you will see the information and the detail about Express framework over here or you can go to website express.js.com and then go to get start and then hello world you will see the code that they recommend you over here all right you can learn from this documentation as well let's install Express on our computer the commanders you are going to use is this command npm I express or npm install Express because it's over here install it after you have installed it you will see node underscore modules over here so that's mean uh Express Frameworks has been installed in our projects and when you go to package.json you will see Express has been included in dependencies over here so the next step let's create our express application or node.js application so you can use this code directly over here to create express application on node.js application okay let's code it together okay con first you have to create Express variable and then you have to record the package from Express so Express over here where is this one this one has been included in here you will see Express over here here you don't have to know about all the code but if you know how to use it it is okay okay The Next Step let's create is Con and app variable then we call Express over here so this structure of code I get from this documentation okay all right the next step we just create an app okay app listen so we listen to Port 3000 and we put callback function over here and then put console log and what do you want is to show on the command line you can put like node API app is running on Port three thousand okay something like this so when you run this application you can type npm brand serve see you will see this one node API is running on Port 3000 but how can you access this website through your web browser you have to declare route okay let's declare route together you can declare multiple routes on your application for example over here app dot get that's mean get route and then you can put URL you can put root directory and then you can access to callback function in callback function you will get two parameter which is request and respond so request is the thing that clients send to you respond is what you respond back to the client okay so you can access to a parameter right here I'm going to access to rest because I'm going to send something to client okay put send and then what we are going to send hello node API something like this if you access to this one if you access to localhost Port 3000 you cannot get anything because you have to stop this terminal press Ctrl C and then press Y and then run it again npm run serve and if you go back here and you refresh the page you will see Hello notes API over here you can choose application like Postman or insomnia to interact with our application as well Postman here this application you can download it and install on your computer or you can use insomnia okay download this application go to price and then download now and just download it and install it on your computer I have installed it on my computer okay over here you can type http and then localhost Port 3000 and hit enter see you will see Hello note API over here all right the next step I'm going to use git to keep tracking of our code so I recommend you to install git bash on your computer make sure that you install git bash on your computer you can get this from this URL download is for window if you use window the installation is pretty simple you just install it and click next next until it's done and if you are a window user and if you have installed kit already when you make a right click on your computer you will stay kit batch here over here after you have installed git on your computer I recommend you to stop this terminal Ctrl C then press Y and then type git init over here to use git with our projects you will see the color over here has been changed to green and you will see you icon over here so okay right click and no files let's create git ignore over here because we don't want to save everything in git okay let's put git ignore okay and then we want to ignore this folder node module after we ignored you will see the color over here has changed to Gray in instead of green The Next Step what we are going to do we are going to type git add to add every files in git and then we have to use git commit to save every files that has been chained or has been added to git add okay we change to we can name we can comment this one M mean command we can comment is like initial project okay all right we have committed these projects already next we are going to install node mon I'm going to explain why Node 1 is really important for node development over here for example if I want to create another route like uh block something like this and if I put here hello block and I save it and if I go back to insomnia and I go to Route block and I click Send I will get nothing because I have to stop my application and run it again every time I make change in the projects I have to stop the application and run it again so this is time consuming so that's why I have to use notemon to solve this problem let me stop my application and then let's install node mon npm install nodemon so Dash D we installed is in depth dependency you can search for node mon in this website npmjs.com node mon okay okay this is how you install node mod you can install it globally or install it in depth dependency okay just hit enter over here and then you go to package.json you will see nodemon over here alright The Next Step you have to do you have to run this application by notemon go over here and over here I will put Dev I view call node mon over here I use node mod to run server.js for me okay I save it and then over here instead of run npm run serve I just run npm run Dev npm run there okay over here if you go back to insomnia and I click Send I see Hello block and if I make change the files over here put hello blog my name is Dave to mean I saved you will see it's restart due to change so it's restart the application due to change just for me and if I go back to insomnia and I click Send I will see Hello blog my name is Dave to mean so when you use node mod you don't have to stop your application and run it again when you make a change the next step we are going to learn how to connect our application with database like mongodb in order to connect our node.js to mongodb you need to use the package name Mongoose let's go to npmjs.com and search for mongoose here is the package here how you can install it just copy it and you have to stop your server and paste this over here then let me run npm run Dev again okay what is the next step so you need to use this code to connect to the database first of all you need to include this one to your projects and create a variable mongoose and then you can use Mongoose to connect to mongodb by using connection string over here all right let's go to mongodb mongodb okay let's sign right here I'm going to sign with my account okay let me create new account submit all policy and click on submit okay what we can do over here we just build a new application or learn about mongodb it's fine okay over here I prefer language JavaScript finish let's click on I will do this later and then you just click on view a database and then create free version over here I picked Amazon and Virginia because I live nearby cluster you can name it whatever you want I can name is uh dip to mean API something like this and then click on create you can create a new user over here I will create root user or admin and password you can put whatever you want okay let's create a user we have a user over here and then for IP you can put zero zero zero zero zero zero something like this any one can access for description you can put whatever you want description add entry okay and then click finish and close go to database close right now and you have to wait a few minutes until it's finished create a database for you also make sure you check uh network access over here make sure your IP address is similar to this because we want to access to the database from our localhost okay let's go back to database and then you will see something like this and then you click on connect we want to connect it with our application just click over here mongodb support many languages over here we need a driver for node.js and the version it can be 4.1 and then just copy this one and then let's take a look at the code the code that we need is over here we need to create Mongoose variable all right let me create con mongoose equal to recoil mongoose okay and next we just uh over here we just put mongoose dot connect and inside the parenthesis you put a URI what is UI UI is this one okay just copy it and put this over here okay my username is admin my password is one two three four five six admin and then what else you have to put over here is collection name that you want to create you can put a collection name over here for example I will put uh node API something like this you can name it whatever you want and what else and after you connect it here we can make a new line I can put then when we connect to the database I want it to show some message on Terminal console log connected to DB okay and if there is an error we want to show the error out callback function over here and then just console log error so this error we get from this one okay save it over here authenticate did fail I think I put the wrong password so I'm going to fix it so let me check it out let me go to database access let me edit password over here because I can't remember password exactly okay let me put a new password over here okay let me update user password let me change a password right here if you put the correct password it won't have a problem save we successfully connect to the database you will see the message over here connect to mongodb here okay actually we want our application to connect to the database first before it's running so we can move app.listen over here into uh then over here okay we save it then you will see that it's connected to the mongodb first I move this up here and then is start our application okay this is the right way to connect to the database you can just remove this one if you don't want it but I'm going to leave it over there and if you don't want is to show Mongoose straight kill the option over here you can put it to be false let me show you how to do it you can put this like this Mongoose dot set what strictly just said this to be false save it then you won't see this message okay and you will see only connect to database then node.js application is running on Port 3000. all right this is how you connect node.js to mongodb next step we are going to create a model for data in database previously we've learned how to connect our application to database like mongodb next we are going to create a data model for database let me draw you a picture so you can understand the process easily here is our application let me draw you here our application and this is node right node app okay we have database over here which is like a mongodb previously we learned how to connect our application to the database okay and how about we want to save data in the database how can we do it we do it by using model for example if I want the same information about product I create product model yeah so I can save information about product in database here this is product information okay product data and this product data will interact directly with product model via the connection that we've created before let's take a look what is inside product module so you can create it easily for example here is inside product model and inside the product model we have product schema okay product schema so product schema will determine what kind of data you have in product model okay it's quite complicated right now let me show you the code okay let's create a folder over here in our projects name is models okay we are going to create models inside model folder for example product model dot Js okay in order to create products model we need to include Mongoose in these files require mongoose okay everything that's interact with database we use Mongoose as I said before in order to create product model we need product schema let's create product schema variable product schema equal we call Mongoose to create schema okay schema and then in schema we will determine each field okay over here is the objects for example if I want the field name I put name over here and then I put object so we can determine the data type of this field I need is to be string and then is required so required should be true and over here you can put validation message if user doesn't input a name so you can show this message please enter a product name and then we can put another field which is quantity okay type could be number this can be required can be true and default value you can put it as zero or put whatever you want and what else we can do over here we can put like price okay it can be type number okay required should be true and you can even put like image okay type should be string and and require can be false okay uh we won't put default value over here and over here you can put timestamp so timestamp is used to create two fields which are created at an updated ad it used to track when data is saved to the database and when data is modified okay we have schema right now we can create model okay let's create product model con product okay and then equal mongoose dot model okay and then we put product over here and we use schema over here okay now we have model name product so we can export this out maju dot export dot product okay we successfully create product model the next step we are going to use the model that we just created to save data in mongodb when you want to save data into database you have to use data model for example I want to say product data into database I have to use product model previously we've learned how to create product model now I'm going to teach you how to use it for example when a user hit this route let me create a route for saving data into the database okay app method should be post and then when they hit product route I will save data to database okay let's put request and respond over here so we can access the data I guess they sent to us via request over here over here let me console log a request.body okay okay let's go to install near let me create a new folder over here okay no folder over here and I will name it uh node API lets me create a new request over here let's type is TTP localhost Port 3000 okay we will hit product okay and this method should be post because we specify method goes over here and this is the route that we want to hit which is product over here okay if it sends something over there oh I forgot to put respawn we had to respond back respond we can respond uh send and then request.body okay save for example if I go back and hit again it's respond nothing because we didn't send anything to this URL okay let me send something to the route in body over here let's put Json over here and then let's create data that we want to send for example name we want to send like so and quantity should be five and then price should be five and image let me get the image from Google let me go to pixel right okay let me buy soap over here yeah this image is fine let me let me copy the URL open image in new tab let's copy this URL okay let's put this over here as string so I put double code over there and let me send oh over here we have to put double code it needs to be double code okay let's send again we get nothing because I forgot to use middleware so we have to specify middleware Json Midwest so our application can understand Json so over here I put app.use so this is how we use middleware so we are going to use express middleware and put this Json so right now our application can access Json data type let's go back again and send again see this is what we get okay The Next Step let's save data that we get from uh request or get from the clients to the database all right let me remove this uh let me put try catch okay if that is an error I want to show Ella console log Heller Dot message okay and then I can respond back please State us 500. and Json I can put object message and then error message okay and over here I can create a variable con product and then okay over here I will import product model con product equal to require OKAY model and then product model and then over here because we want to save data to database we need to save it through product model okay let's put our weight over here because we interact with database so we should put our weight over there so when we put our weight over here we should put asynx over here so all right and then we put product we call this product model okay and then we wanna create new product in database with requests body data okay and we want to respond back with the data that we saved into database so we put rest and then status 200. and the data that we want to respond back is product as we said to database okay save okay let's try this thing again let's send it okay you will see we get the new record which is ID and created at and updated that this means we successfully save data into mongodb okay let's go to mongodb and check if the data exists over there okay see you will see the data is over here and it's in products and is in node API collection the next step we are going to learn how to fetch or get data for mongodb alright let me save more data into the database for example over here I will save shampoo okay then we send a post request to our application over here we get shampoo let's add more data to the database for example soy sauce okay let me change the quantity to 2 and change the price to two and then let me send a post request to our application okay we get more data over here we should have three items in our database all right let's check it together okay I go to refresh over here and then you will see three items over here it should be okay all right let's go back to our application so how can we fetch data from the database or how can we get data from the database to get data we use get liquid as over here so we put app get so this gate is get request and then we put route over here it should be product I think we should put s over here and this should have S as well okay and then it should be async's callback function because we have to interact with database and then we put request and respond and over here I'm going to use try catch to handle error and over here we respond back error rest dot status 500 and then dot Json and message is early message hello Dot message next step we declare variable product okay cons product equal to await because we are going to interact with database so we use our weight over here to wait for the data from the database then we use product model to access data in the database then we use function find to get our product five with empty Curry braces that means it will get all products next we are going to respond these products to clients rest the status 200.json and then we put the product over there over here to Json okay this should be all right and then let's test this router together okay let's go back to insomnia over here I'm going to create a new request so I'm going to rename this one to be get all products okay and this one I should rename is to be save a product okay let me go to fetch product oh let's go back here let's grab this URL and let's put it over here it should have S at the end okay and then we send to our application all right we get all the products from the database and how about we want to get a single product form ID so we do pretty much the same with get all products but we have to specify the ID over here I put app dot get because we are going to use get request and then we put products over here then we specify the ID of the product we put colon and ID then we use async because we have to handle something from database and we use request and respond callback function and then we use try catch to handle error and we copy respond hello over here and put it over here okay the next thing because both of them is pretty much the same so we copy the code here and paste it over here and I'm going to change this to product because we want to find only one product and instead of find all products in the database we use find by ID and we put ID over here so how we get ID from here from here we can deconstruct ID from the crest.param over here con IDE equal to request.params okay and then we drop ID over there to over here and then we respond back the products when we get to the database to the client okay let's test this route together okay let's go back to insomnia and let me duplicate this one and then the changes to fit a product by ID or form ID okay let's create it okay and then let me grab ID from here and put the ID over here in the URL okay and then let's send it over here you will see we get a single product from the database the next step we are going to learn how to update or edit data in database all right let's go back to code okay let's scroll down I'm going to create a new route over here let me put app dot put so when you update thing you have to use a put or patch method let me comment over here update the product okay and then the URL that we need it should be the same URL as fetch a single data okay let me put products and then ID okay and then we've used async because we have to connect to database so asyns is really important in this case and then use try catch to handle error okay and then we can print out the error if there is an error we just respond back message aloe Dot message okay next we are going to get ID from param and get this ID right we destructure this ID from request this parameter and Dot params okay get ID now the next step we are going to find this ID and update it okay let's put con product okay and then put a weight to access to product model and then we find an item by ID uh Phi by ID and update okay over here then we have to put two parameter the first one is ID okay and the second one is the data that we want to update we get is from request.body and this data send form clients the next step we check if product is updated or not if it's not updated okay not product then we return rest status 404 and then we use Json message and we put object and message and then we put backtick and we find like we cannot find product cannot find any product with ID we just return the ID this ID these case mean we cannot find a product to update we cannot buy any product in database If the product is updated successfully we are going to respond with status 200. and then Json will return back the product okay sorry okay save it this should be done okay let's go back to insomnia we can duplicate this one and change this one to update a product form ID and over here we have to send put requests instead of get because in our route we specify put for update data let me put body over here let me put Json over here and then I put object okay and code name I want to change a shampoo to like baby shampoo okay how about if I send to the server they say like products ID cannot find put oh I forgot to put backslash over here put backslash and I save it okay let me try it again okay let me send back here why is this an update actually shampoo over here should change to baby shampoo okay let me do it again let me send it again it's updated right now okay let me change this one back to shampoo and then why is return the own data and let me check over here and if we back to here and we refreshed you will see that the data in database has been changed to shampoo but it's returned to own data okay we are going to find product by ID again because we want to get the latest information from the database okay let me create a con variable over here updated product equal to a weight and then product dot Phi by ID and we pass ID and this one should be updated product okay save it okay let's go back to insomnia and update the data again for example I submit it and for example I change this one to baby shampoo and click on send you see the data updated all right and what is the next step how about if you don't want to use Json over here if you want to use like form UIL in code and you can put name over here and then put shampoo over here let me send here it doesn't update you see how can we solve this one if you want to use form instead of Json so you have to set the middleware over here you can set middleware app use and over here you put Express dot URL encoded an object extended equal to false you save it and then go back to insomnia and if you send it you will see the data updates from baby shampoo to shampoo and you can add more feel over here for example I'm going to add field price here is price equal to 10 you will see the price is 10 right now the next step we are going to learn how to remove or delete data from database over here I'm going to comment delete a product and then I call app and then I put delete method okay and then we specify route product and then program ID we use asynx and request response callback function okay the next thing we do is we use try catch okay and then if there is an error we just respond back earlier to clients okay then we deconstruct ID from request Dot params and then we have to find the ID that we want to delete we put con product and we use our weight to weight information from database okay and then we call product model and we find by ID and delete okay here we have to put ID okay and if there is no product no product okay we return respond and with status 404 not found and then we put Json and we put object message and then we tell them cannot find any product with ID this ID from clients and then if product is successfully deleted we are going to respond back to client with status 200 and then put Json object and then we just return the product let's be fine okay all right let's go to insomnia all right let's right click over here create new HTTP request I can copy the URL form fetch okay copy it and put it over here for example if you fetch our product over here you will see our product in database okay if you want to delete delete and if you want to delete so okay you get grab the IP and you put at the end over here method over here needs to be delete because we have specified delete method over here and let's send it you will see over here is returned soap to us that means the soap is successfully deleted and if I send a request again you will see show this message you fetch all the product over here you will see only two products in the database alright this is all about this video If you like this video feel free to click like subscribe and comment down below thank you so much for watching
Info
Channel: Devtamin
Views: 111,397
Rating: undefined out of 5
Keywords: code, programming tutorial, web development, Node.js, MongoDB, Express JS, Express Framework, Restful API Node.js, Node.js Restful API, Build API with Node.js, Create API with Node.js, CRUD Node.js, CRUD Express Node.js, API Node.js for beginners, Node.js for Beginners, build API with Node.js for beginners, Node.js MongoDB Express, API Express Node.js MongoDB, MongoDB Node.js API, Build CRUD API Node.js, Node.js Tutorial, Node.js API from scratch, Node.js API Tutorial
Id: 9OfL9H6AmhQ
Channel Id: undefined
Length: 46min 24sec (2784 seconds)
Published: Wed Jan 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.