Create Complete Rest API Using Nodejs Express MySQL Sequelize In One Video πŸ”₯πŸ”₯πŸ”₯.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys if you like my awesome gaming videos my programming and web development courses my editing courses and editing videos and my tech talks and tech guidance then you can consider subscribing to my channel and click on this bell icon to stay connected with me all the time uh so hello everyone welcome to my channel again so this is a very interesting and amazing video for you guys so in this video i'm going to talk about the especially the node.js how can you create a rest api using the node.js suppose i have as you can see the api reference of the cqlise.org so it's master if you go to that secularize you will get all of the documentation how to create model based on it how to query it how to do raw queries how to do association lot of things so basically the sequel is a very interesting idea how to create orm which is object relational mapper in node.js as you can see we can do this and we can use mysql postgres and many sql based database but normally we don't use node.js with sql we normally use node.js with mongodb but here most of the people like to use node.js with mysql at least in my area or in my country bangladesh so most of the cases people always like to love and use mysql databases with the note and in many cases even in the laravel they use that and even in the node.js and the django platform also the django respraymark also they use those kind of things so i'm going to create a complete guide how to you can make from start to finish and a complete finished product with two part this is the part one i'm gonna show you only the crude product and the crude application of the crude api of the part and you don't need to write raw queries like in the mysql that you do so if you go to uh w3schools see what we are here as you can see we can connect mysql with the node as you can as you can see you need to write those kind of thing you need the sql library packages and all of those thing and give your credentials and connect it so this is another way you can create a database you can create a table so all of the things you can you can insert some data as you can see you can insert some data based on it and you need to write raw queries as you can see sql insert into the customers name address value all of those thing so i don't want to write raw sql because this is a very huge pain when you want to join one or two tables so for that we gonna use the node.js with secularized orm so that we can do it very easily very fastly so we can do all of the things very properly so let's jump into it and let's see how we can do this so for that you need to use the postman as you can see i already set up the postman as you can see the api should look like this kind of thing and you need to install that postman which is very useful software i need to use xampp as you can see if you use xampp as you can see if you use xampp and make sure you just start the server uh otherwise it will not work and make sure you are admin okay just to go and create our node application so let's go to our file structure as you can see node express sql as mysql api uh i know this is a perfect name because we're going to use node express equalize mysql and complete rest api so i'm going to open it with our visual studio code and by the way i'm using windows 11 so it is giving it a as a nice performance and i really like the animations it's super smooth as you can see it's super awesome so let's go and let's start and this is our main root directory um new terminal we need to create our node application so for that you need to write npm init so this is very important if you write np in it it will just make a node application for you and this is the package name this is the version name and give some description suppose node express crude api with cq sqlize okay so our main entry point should be server dot js file and notice command no git and keyword should be node express mysql okay author give your name okay license and it is okay so perfect so as you can see our node application is created with this kind of information like keyword author and those kind of information okay now we need to install some packages so if you go to the npm.js so let me go to the npm.js and here you will find all the packages that you need to create your node application and whichever library you want to use suppose i want to use express which is a very popular package as you can see if you can if you go see express if you click on this it is a very famous library as you can see thousands of crores of course download and just install it npm i express and we also need sequel eyes we need that uh so so that we don't need to write the raw queries we can create our object relational mapper so that we can do a model creations and queries very effectively and perfectly so as you can see multi dialect vrm for node.js for now it is especially for the node.js if you click on this as you can see we can see this equalize and you can install this npm icq lice okay so these are the thing we need and we also need mysql2 which is another package to connect with our mysql okay my scale to just search mysql to just make it comfortable and as you can see and pmi mysql to all of the packages maybe look really difficult for you but when you work with node.js you need to install those kind of libraries very effectively and very easily and you can go to the npm js as you can see in this url and you can get a lot of information how you can do that so as you can see we we just use raw queries all of those things but i don't like to do dog queries i just want to do it more efficient and more professional way and i'm going to use the mbc pattern as well in this application and here npm i that receive if you want to do it for the production level because we want to install that in the production as well there because we need this kind of library very important which is express and then we knew we want sqlize as e q u e l i z e c lies and we also need course to across origin access to all of the client because we want to add our react project and he consumed this api in the react in the third part which is also we need for course which is cross origin resource management actually so that we can get this information onto the third party web browser we can show this information otherwise there would be some huge uh error course error okay so we need that and we also need mysql2 to connect with the mysql if you want some other some postgre sql you need some that post the xql instead of mysql too but express secure scores my and my sql those are the four important things which is very essential so let me install and if you want to install nodemon you can do this but i'll just keep it simple so as you can see our express seculize uh course mysql all of those thing is installed properly so now what i have to do i need to just create some folder and file structure so our first file structure should be server dot js ah this is our main file that will run our server and what we're gonna do we need to create some folders first we need some models and then we need some controllers so our models and controllers are important for mvc and then we need our router or you can say routes and then we need our config because of the config information some database information which is also very important just i'm gonna name it as new folder which is db config.js let's work with our server.js file so in the server.js file what i'm going to do we just import our express which is const express is equals to you just write a require express and you need course which is a cross energy resource management which is also very important and this is also uh required which is course then we just instantiate our app which is um express okay this is the same expert that we just uh required but what we have installed and now we can access many app app.get or app.listen and app.use all of those things so that's why it's important so now what we have to do we have to just write course options is equals to origin is equals to we need to write http slash local host and localhost and the port should be 8081 so this is the port and the this is the url that we giving access to all of our information of the api if third party api like some other people want to use it they can go to this url this origin and you use that so what i'm going to do app.use and this is the middler that you are using and the modular should have the course and course have some course options and app.use so we need to use express and json because in the api we are getting all the information in the api in the json format as you can see in the body in the json as you can see we are we are putting all the information in the ap and the json format so we must need that express.json otherwise it will never work okay then app.use and we also need to write express dot url encoded and you are encoded we need to pass extended is equal to true for those information as you can see returns medullary that only parses url encoded bodies and only looks at request while content type header match the type of the options so these are the things suppose we this is the header we have this information like title uh price description all of this information is coming from the request.body as you can see so for that uh to get all this information we need to do that so this is very important and these are the simple middleware so you can write a comment middlewares you can write a testing api which is app.get so this is just a dummy to test our application so nothing serious so app.gate and slash and you just need to write request and response because it has two information which is request and response and response.json and we're gonna send the response in the json format and we're gonna write message hello from api so this is a simple when you hit this url slash you you're gonna see some response with the message of this okay and it should be port is equals to process dot env dot uh port which is for the production level and if it is not working just just use 8080 port and then you need to run this hardware and server is simple constant uh you just need to write app.listen based on onto port number and then what you're gonna do and console.log after the port is you connected to the port you just need to console.log and in the backtick you just write server is running on port and you can write port okay so we are using backtick so that we can access the variable as you can see this variable because we want to know which port it is running so we are creating a port and just listen creating a server and we're just testing this api and this this is the simple modulars for accessing the jsons from the body and these are the course and express app so this is the simple basic structure what i'm gonna do so now if i gonna test this app and we just need to write save it first and this is server.js okay so this is important so you need to write node server.js and as you can see server is running on port 8080 so if you go localhost 8080 18 you're gonna see hello from api then you can go to the so remember this is just a part one so we will do all the hectic thing in part one and go to db config and put some configuration okay so this is the very important thing database information and just write uh module dot export because we want to use that in another file that's why you need module dot export in node we use that okay this is very very important and you just write host is equals to uh it should be colon and it should be local host it should be localhost and then you're gonna use user and user should be root okay and then you use password password should be null here in case you can use it and db should be node underscore secure lives uh underscore api db okay so this is the local host this is the user and this is the password and this is the dp so these are the four information which is very important then one more information which is uh dialect and this is dialect means what kind of sql or the database that you're using it can be postgres it can be mysql whatever you like and then it should have a pool and pool should have some information which is maximum connection is 5 with a maximum pole you can use 5 okay and minimum should be 0 by default this is not very important but you need to use that because in the documentation there is having this kind of information if you go to the secure documentation you will find it i highly suggest you just look out for if you want to get the better knowledge then you need acqui acquire and acquire should be 30 000 so this is the this is the time that we want to acquire it for and the idle time for this database should be 10 000 milliseconds so this is all the information max mean pool is important if you don't want to use it you can skip this part also okay this is not that important but ah this is a good to know then another part which is in model we will have our three part so first one is index.js okay so this is inside the model so we need to just get the db config here is equals to required and required dot or slash config slash dbconfig.js we are importing this file this is the information host user password db dialect pool inside this file as you can see in the indexed in the model okay we need that we need that very often very important it is and just write const secularize uh sequel lies and we're gonna get the secularized from required secularize that we just install because this is super important now you also need one more thing which is data types so types of data i'm going to use these data types in the model creation you will see it within a very short time so don't worry about it so it is very important then you wish to write uh const secularize this is that we are creating the instance of the secularize uh constructor as you can see we in the sequel is constructed we're going to pass some information and in this file information should be config dot which is database information and we can get this information because in the dbconfig we just export the host userdb all of those thing and we are accessing each of this first is the db okay we are getting all of this information then we just dbconfig.user and dbconfig.password and we need one more should be host um is equals to dbconfig.host because the host is the localhost here and dialect is equals to dbconfig.dialect so all of the information is coming from the config file and then you can use upper records liss is equals to false all of the error that you have in your information if you have some errors suppose when running this application it will overwrite all the errors using the operator liss so this is it is not very important but just write it whatever right because i i will use the best practices because uh because i like to show you the code with the best practices which is very important to understand you just instituting all the database information user uh password co-host dialect operator analysis from the dbconfig file we are getting all of this and in the secularize we need this information database information so those are the very important fields that we want here is pool and in the pool and you need to use max and max is coming from actually db config dot pool dot max if you go to the db config file as you can see it will go to db config that pull and max okay then it will put tpconfig.pull.min this way so you just just you can copy this thing and you can write it should be mean and this should be mean and this should be acquired and this should be equal and then we have idle which is dbconfig dot pool dot pool dot idol okay so this is the information is super important then we need to authenticate it so how can you authenticate we can authenticate using secularize dot authenticate as you can see very important thing we can use secularized authenticate and when you authenticate we can use then uh when we authenticate then we need to use the fat error function then what will happen after uh we just want to console dot log this because what will happen we need to identify that you just need to write connected or anything connected and then you need to catch some error if there is some error what will happen okay so you need to identify that as well and then to write console.blog and you just write error plus error okay so this is very important they need to initialize const db is equals to so empty object in the empty object i need to put something and before that uh i need to just make sure in the config i need to just get the database name properly uh node secularize api.db and you need to go to the localhost to the database phpmyadmin because we connected the xampp properly as you can see and let me create a new database for you and just go to the new and just give the same database name okay uh node sql secularize api db it is created and you don't need to do anything else so this is the api node sql is apatp so you don't need to do any other thing everything would be done automatically just give the database name so after do all of those things go to index and here you can write uh db dot cq lies is equals to secularize this is the constructor that we are sequalizing as you can see we need to in do that and we need to secularize the instantiation which is ck this equalizes actually this cq less this is the instance so you can do that and we need to securize that as well so those are the two information and then we need to write db dot products and this is the table name of your database products okay so always remember that if it is post you need to write posts like that utilize and we just direct database so this is very important are the data types and these data types is actually the starter types okay and then you need to do the same thing for reviews okay and in this place you're gonna write dot slash review model dot js and you need to secularize that as well so this is done so these are these two part is important this stupa is important because you want to secularize all those models and based on those data types that you're going to put and the data type should be like a title price description those are the data that you're going to see okay in the reviews also you're going to see those kind of information so these two information is super important then what we can do we can db dot uh secularize dot sync and the sql is have a sync method and in the sync you can just write force is equals to false so there is a big meaning in here which is also really really important to understand for the beginners so if you write is force is equal to 2 every time you run the server or this run this application or this api you will lose all of your data and if you don't have a particular table uh with that name it will it will create it and if it is does not exist it will create an even if you just make sure it is force is equal to true uh it will recreate every single information and you will lose all of your information in the database in the tables but if you create it false it will not create again and again okay so this is the important you can do it true as well but normally i just keep it false uh sequel is equals to false and what you're gonna do and you're gonna use then and then what you're gonna do and you're just gonna console.log which is also very important it means that yes write yes resync done okay so when the sync is complete you just need to write uh yes sync is done every time you run the server it will run this thing and then you need to just module dot exports you just export this db and this database actually has a has a name of the table names products and reviews and this products products table will contain everything that we specify in this product model and the review table will do every single thing that we specify in the reputable and secularize them based on their data types so that we can do object relational mapper so these are the information which is so important in the index.js file and we're going to use this db again and again uh in many different well now i need to work with our main midi part which is the very easy and easy to understand and very fun stuff which is product model and the review model so our third step is very easy we need to write module dot exports and in the module dot exports you just need two information which is secularize because you need to secularize and the data types and the data types is also important and we don't need to import that by the way we just need secularize and data type and here we're gonna do our main thing which is const uh product because our main model name is product if you have a post model you can use post and this will use cqlize.define and this is the main part if you see the documentation of secularize you will see that kind of thing this is the product model name this is the the capital letter product and secular defined and it will have a name of product because the is the product is our main entity name product it will have a different kind of attribute and you can give it any name but i will give it singular very important and in the inductor js i just name it products because for database the table name should be products and reviews including the s okay so normally you indeed secularize to define you just use the normal without s okay so this is very important and here you're gonna define all this information in the constructor yeah suppose we need to have a title and title should have a type of it should be data types dot string is very important and make sure is equals to allow null allow null is equal to false because you cannot keep this field this allow null is equals to false means you cannot keep this field uh null which is very important okay so this is very important is our price and price is also important and make sure you just write the type is equals to data types dot integer you can use float as well integer or float whatever and then you need to write the description description should be type of data types dot text but if you search on the documentation of this equalize in the data types you can see a string for the title uh price for the float or integer for the price on and for the big text as you can see in the description we'll write a lot of things uh we can use text so we don't need to specify that this should be auto incremented this should be uh integer of value this this would be character type this would be virtual type all of this thing in the mysql which is really pain but you don't need to do that you're just creating a model with the title price description all of those things gonna keep it as a published and published and make sure it's type of boolean because you want to know how many types of data you can put suppose i want to show a string for the integer for the text okay and the last part is for the data types uh should be a data shape should be boolean so one is a string one is integer one it text and one is boolean so these are the for the product and here we need to return that product model and this is the same information we're gonna do this for the review model and in this part we don't need to use product and secure as a defined product here we just need to change our name to review and this is the and this this name should be review in the upper case and you're gonna name it sql as we defined and give the proper name for your models and here you need to write the rating okay so we don't need to write this we just need first one is the rating and its data type should be integer because rating is integer and description should be data type should be text okay so these are the two information which is very important and this is for the uh and you just need to in the review model as you are naming it review it just to name it return it as a review so these are the two important things and make sure you just use module dot export because you need that thing later okay so we just created our model this is our another part so now we're going to go to our next part and see what we can do so now it's our main duty to work with our controller because we just created our models created our index file and secularized all of those thing as you can see we sql as the product model and with the data type that we passed in and this data type is passed in into this data type as you can see and so and now you understand why it's important and we just secularize both of those things so now we can work with our application really first and easily so if we go to the controllers and controller we need to create two controllers so first is the product controller.js so let's write to controller because we went to next part we're gonna work with our another part which is review controller dot js product controller and review controller.js so two controllers important and two models uh we also created and then we need to create our routes and the routes we need to create our norm first we need to create our product without uh dot js suppose product router.js we're gonna handle all of our routes here and controller to control everything to work with our application so this is the functionality that we're going to use in the crude functionality in the product for the product model with first we need to import our db because it will handle all the information so constant db is equal to require and dot slash models because the models we have the product model and the review model all of those thing you can ah require all of this thing from the model and in the model we have index.js file so we need to use this db to connect with our products and the reviews model okay so we need that kind of thing so that's why i need to uh use that uh constant db is equal to required models and because models have all the information you can get all the information using this variable so now we need to create our create our main model and how we can do that you can just write const product okay so this is the main model because in the controller we need to work with our model so this is very important to pass our data and get our data from the database so this is the main uh mbc pattern in every single information in every single framework you you can use this in laravel django all of this thing um so you're gonna use uh db dot and in the db you can have two uh tables as you can see products table and reviews table so now i'm gonna work with a product table and let's import another thing which is our uh review as well but i'm not gonna work with it now but just add it so we have our products we have our reviews all of those thing and then we're gonna main work main work starts from here and the first step is create we can do that we can just write const um add at product is equals to request dot response and here so make sure you just use sync here because this should be an asynchronous function and this is the add product and first we need to use make sure it is information and then information should be coming in an object from the api request dot body and first one is the title and it will be coming from the request dot body dot title and then we have a price and price should be coming from request.body.price and then we'll have some information which is um description and this should be coming from request dot body dot description because all those information as you can see uh it is coming from the body as you can see this coming from the body title price description the title it will set it in the title and whatever you write in the press it will be present the description as well so whatever you write in the pub list and make sure it is true or false and it will coming from request dot body dot publish and it is if it is published you're gonna write dequalted body dot published otherwise you just set it to false it means that if if it if you were setting it to published is equal to two it would be published otherwise use the turner operator you should you just write it as a false and the deposit the default value now you need to use um suppose const product that you're gonna create and you can just write a await and the product model is actually as the db dot products you need to go to the product table in the database okay so and the product is actually your model name so this is the product okay so you just need to use functions and we in the sequel we have some functionality so that we can use so we don't need to write insert into product dot dot this kind of value this hectic thing we don't need to use that here you just write dot create and it would be created for you and it needs some object as you can see in the create you need to pass some object and this object is actually this object that we're gonna pass which is info so you don't need to need anything else then you're gonna response dot status uh because the status quo is important because it is uh you can understand if it is created or not and you're gonna send your information uh whatever you have so a weight means that it will create the product based on this object information that is coming from the api which i i'm gonna write and it you just uh create the product based on those information and save it and put it onto the database and these are the just this is this is the variable that holds all the information and in the ap you're gonna see all the information response dot status code of 200 and sent as you can see in the api we can see the status code and and this is the status code and this is the response that we get so this is very important to understand so this is just to create uh those information okay so in the product information so this is your adding your products so number two number two is to get all products so you need to write const get all products okay and to get all products and make sure it is taking request and response and make sure it is asynchronous function because we don't need to write a lot of code with that and here you just very simply just use products and contain all the information in the products variable using await and go to the product models and product model have a function built in function in the secularized now when if you want to get all the information from the products table we just write product dot find all okay and just write empty object inside so that we can get all the information and response dot send it should be sent respond or send all the information the products information that we coming from the database very simple and this is just a simple click you just don't need to do anything else and you can use uh status as well so let's make let me copy and paste this thing and this should be products because we are holding all the information in this products variable uh you can just write here inside the find all you can write attributes whichever attributes you want suppose i want only the title and in the title and price so this is the two information you want from the api not all the thing like in the api will have title price created updated um by default and published all by id all of those information but here you just need title and price from the apa and you're gonna get all the information only two information is required which is title and price from the apa this this is the two information that you want to see okay so that you can use that thing but i'm just keeping it simple i'm not gonna use it later you can explode it more okay so just giving all the information from the database with another part and this is for the third one and just this is about the get single product and get single product which we're gonna do uh get one product okay so this is our this is our function name get one product and here you need to write find instead of find all you just need to write product one and product one we need to find it by id because uh we need that id so let id is equals to uh request dot params dot id we're gonna get the id from the parameters in the url the suppose we want to information of id of product id of one product id of two whichever you use that uh user c and get it dynamically from the requested patterns dot id and then to find those information based on this id and here you need to write inside the curly bracket you can write um logic like where id is this id is the database id of this product information and id that you want to search and this id is the uh url id as you can see where it is id is equal to this id uh we want to get that information you can write where to find the you can write other information like title is equals to uh some title okay so you can do this but normally i'm just finding it for the id so that you can understand it very easily so you just need to write product dot find one and instead of products just write product it is very important and get all the information based on this product and the then we need to use uh for the update okay so for the update we we just need to use this is the number four and it should be update product and this should be update so now we are using to update products so how can we do that okay so let me delete everything let uh let me do this from the scratch but we need that um uh id request.parameter.id because we don't want to update everything we need to update something based on that id okay so this is very important so let me write const product to be updated and use the await and let's go to product dot and we have a functionality to update it and this is the update method that you can use to update similar to find one and find all to get everything point one to get single and find one to update and we want to update whatever we write in the request.body okay in the api with that we want to update and then we need to write where id is equal to id so this is very important just write it like this this way just update every single information in the database where id is equals to whichever id that i passed in i want to update that information and then we're gonna get response dot status of 200 that it is updated and then you're gonna send uh that product information that is updated okay so this is very easy and simple and the last one is very simple and just you can use this one as an example and this one should be number five and you can delete request delete product by id and in this case we just name it uh delete delete product and in this case also we need that parenthood i do we need to get the idea of the product that which one want to delete and this is the same thing we don't need to hold it in a variable anymore we can write it a weight and we're gonna write product dot destroy so this if you go to the documentation you're gonna see all of those things so let me go to the documentation and as you can see in the more in the api references you can see all of those things so if you write this model queries you can see all of those thing as you can see user dot create and you can see user dot model dot find all to get every information and if you zoom in you can see all of those thing you can find everything you can find all where id is equals to something and you can use uh id is equal to something status is equal to active you can use how many types of thing you want you can do all of this if you understand it and read it properly you will get all the methods that whichever you want suppose to delete it i just use product dot destroy because it just destroyed the information okay and the destroy where you need to use otherwise it will delete everything make sure you use the id whichever i passed in or whichever the url you just need to delete that thing where i product id is equal to that id that we passed in so this is very important so destroy every single thing where id is equal to that id and then you just make sure you do it properly 2 200 and sent you don't need to send those thing that you just deleted you just need to send something like uh product is deleted okay now we need to as you can see it is not grayed out it is not using properly so we need to export that which is module dot exports and you need to export them first is to add product and then get all product and get get single or get one product and get uh update product update product and then we have um all of this thing we have in the five we have delayed product delay product and then we can also use one of the method which is published product so let me create one of them the number six you can use this kind of thing and this is the published and this is number six and get published uh published product information is published we want to see all of them so you should be ah get published product okay ah and we need this should be ah forms products is equals to a weight product dot it should be find all where we want to see every single product find all uh where we just use where clause where published you need to write the uh published is equals to true on the end when the product information is published i want to see all of this information which are published in the product table okay and then we're gonna just send on the products as a response button and just you need to write get published product so these are one two three four five six so these are the six methods that you're going to use in the product controller which is really really important to understand so normally our controller creation is done now to work with our product routers and product routers need to have some controller because without uh this functions the routers is nothing so we need to route uh const product router um or the it should be product controller sorry it should be product controller is equal to required dot dot slash controller slash product controller dot js so we are getting all this information in the product controller and we use this kind of functions to work with our router so here we need to use uh const router is equals to require we're going to require express and in the express we can get the router and now we can use those routes the first route is uh router dot post dot post which is uh add product if you go to add product what we'll do if we go to the add product uh we can use add product method from the post product controller so if you go to product controller and need to use add product and this ad product is coming from this ad product because we exported that and it will add some product into the database so this is very simple just um model view architecture with the route and the dow don't have anything just a simple line of code so this is a very clean architecture how you should um structure an application you can copy those things multiple times three four five six and uh next part is to getting all the information the first one is to create some products and then we get all those products so for that you need to use get method which is router.get and you're gonna just write it all products so if you're going to hit the all products router or the url uh in the ap you just need to use um get all products so our add product is done and we use post and then we have the all products if you go all products you get all the products information from the database that we just specified as you can see in this functions to get all the uh information on the products from the database so if we go to the router it is done properly and then we need to just go to router dot get okay and you need to just write published so in this way you can get all the published products we need to go to the product controller and get published product so whichever products are published is equal to and to get that informations and then we need to get each product so you can copy or you can use any of this you can use router.get and this all should be router.get okay and suppose here our publish is done let's do it another way and it should be it uh colon id because it is dynamic it can be anything it can be one it can be a product of two which can be protected of three because based on that we want that particular product information so particular product objects so we need to get one product we only get one product if you get a particular product idea of one we get the one with no information or product that you want and if you go to two you don't get the protected information of two all of those things and then if we just go to dynamic id again and in this case we need to use put because you want to update that and you need to use uh update products okay then we need to use delete so get put and delete get each product by its id uh and update each product by its um id and delete each product by its id so uh id delete update and get by id all of those thing needs some dynamic id and you need to use delete products okay so one is to get one and then update and then delete and one up for update you need to use put and then for delete you need to use delete and then what you can use uh you just export it module dot export and the router okay and this router at this router okay so you just need to export the router so that we can use it inside our server.js routers okay so you can use const router is equals to require and you just need to write it dot slash dot slash uh routes and product router.js and we're gonna importing this file uh this product router this file okay into the server.js we because we want that okay so this is one of the most important thing that you need to understand then you can use what you can use app.use okay and here is our main api slash api slash products okay and if you go to the slash api slash products uh you can enable this router so this is very important okay so make sure you just comment it out and write it proper way so that people can understand and these are all the middlewares and these are all the routers you can use the all these routers uh here as well okay inside the testing api okay inside after the middlewares okay here you can use all of those thing because otherwise it will not if you go to the slash api slash products and then what you're gonna do you're gonna go to uh slash api slash product slash add product it will add it and if you just write all products it will get all the products and if you go to apa slash products published it will give all the published product information all of this thing just write it like whichever format i write otherwise there would be some problems okay so normally that all the dynamic id should be stay in the bottom side and all the other route should be in the top side so this is just the convention which is the best way so these are um all the things that you just imported all this router if you go so this is for the all the routes this is just a common thing and you just imported that router from the product router if you have other router you can import other router as well suppose you can use uh suppose for other router suppose api slash comments okay so you can use comment router and import all this router from the comment as well so you can use multiple thing using this this is the simple case and the best practices so let's try and understand if it is working or not and let's check it out so node server.js and yes as you can see if you run that if we go to the index.js file after the secular sync it will give you some information yes resync done now you can work with our api now we need to go to our server.js and if you go to the slash api slash products and if you go to the slash api slash products and then you're gonna go to add product you're gonna add protect it okay so if you go to the uh slash api slash products and here i'm gonna use add product so to add some product and make sure it is a post and make sure your title is anything suppose iphone 12 and give you some price of 9.99 some description some and just copy this thing because you need it and just send it cannot post ap slash product slash ad product but why it's not at product it's add product okay make sure it is add product and our title is iphone 12 the price is integer description public is equal to two so these are the uh four information which is important an id would be added uh dynamically okay so just copy this and send it as you can see it's worked really fine and let's do it for the iphone 13 pro and this should be expensive iphone is uh so expensive okay and make sure it's price is 999.99 so this is super complex so let's do this and as you can see iphone 13 pro and let's do it for the samsung as well because the samsung guy would be really angry with me and make sure this price is 999.2 and the samsung something is not that is balanced okay anything is balanced suppose this price is 8.99 okay 8.99 and it should be note 20 suppose okay send it as you can see all the information is going now if we go to get get request okay and get all products all products so all products let's check it out our routers let's go to our product routers and just check all products and let's check all products and hit it and as you can see we get all the products information so our ap is working really fine if you want to get the published information as you can see all the published just write it published i think it will work really fine so all of the things are published so make sure that one of them is not published is equal to false so let's go to our database and refresh it node sqldb and the products as you can see all the information is published so let me and let me make sure that the last one is equal to zero because it's i this is uh this is not published the samsung one is not published suppose so let's get this and now we get all the the iphone information the samsung information is gone so let's it is working perfectly now let's check it to one as well so our api is working really fine and this let's check it by id of one and it should be id update and delete let's check it out suppose um let me copy this thing and just go to id of three because this is samsung one and as you can see you get the samsung one and if you go to the id of two it is iphone 13 pro which is super expensive and if we go to iphone one it will go to the iphone 12 and let's check it out delete and first check it out with the put request and the put request suppose in that uh samsung information these are all the samsung information i just want to update that information okay based on that id and here i need to update some information suppose it is 8.99 suppose i want to update it to and just write it uh it is updated and make sure it is also updated okay so make sure all the thing is updated just make this now copy the same thing unless i want to update because the samsung is actually the id of three that you want to update so whatever you write it um you can update it so that's why if we go to the product controller in the product controller if you go to the update section as you can see you need to update whatever you write in the request.body what id is equals to whichever id i passed and i passed the id of three and i had to update that if you send it as you can see uh is updated and it will show the update information if i go uh to uh put request okay it will update the information as you can see and to update it and here you just send it as you can see it's updated because it returns one returns once means it is updated if we go to get this id information of three now as you can see it's updated see all that information that i write here is showing here so all of this thing is updated properly so everything is working properly and let's just copy the same thing because i want to add it later and uh you just need to use the delete functionality because then to delete it as you can see product is deleted properly so i don't don't want to delete it i just need to add it again okay okay i need to add it again and just make sure it is a post request and is added with an id of four so if you want to get every single information like all products so this is how you test your api so it's really really nice as you can see number three is gone totally um so we have two tables the products and tables so products and reviews as you can see uh we had id rating description and in the product we have id title price description published all of those things so this is the simple api how you can use a node sequencer with work with mysql too to create a complete rest api with the mysql database and the node and express so these are all the things that need to understand using config and use all of your database information use controllers and write all those functions properly and get a proper documentation and then you just write sql all of the thing as you can see we just import seculize and sql is instant of sql is and pass up the database information authenticate it and if you just securize whatever the model that you create and on the model and make sure to sync it and export that database because you want to securely see this is a very important thing sequel is defined based on this product you secularize and keep the all the database models information whichever you like so you just sql as all of them thing and you just create our controller and then use the routers and so whatever you know in from whatever you do and whatever you want you are passing in so this makes it really really clean and in the server just you just use that routes it's very simple and easily so these are the mini structure how you can equate a complete a node rest api using this eq list okay so thanks for watching i'll be coming with a lot of amazing video the next part i'm going to use the one-to-many relationship and uh work with our review model as well so which is really important and the review controller so next part i'm going to invite you to watch the next part which is also very important and thanks for watching i'll become with the next video with awesome awesome and beautiful content for you guys and thanks for watching i'll be coming the next video till then take care bye bye
Info
Channel: Great Adib
Views: 103,072
Rating: undefined out of 5
Keywords: nodejs, mysql, nodejs crud api, api, rest api, how to create rest api with nodejs, mysql sequelizer with node, how to use nodejs ORM, nodejs rest api, api testing complete tutorial, how to test an api, node js api crud with mysql, nodejs with mysql rest api guide, sequelize mysql database with nodejs, postman api testing, model creation, database ORM queries in nodejs, nodejs complete tutorial api building, what is nodejs, learn nodejs, nodejs tutorial
Id: tpso18ghda4
Channel Id: undefined
Length: 59min 3sec (3543 seconds)
Published: Mon Nov 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.