Building a REST API With ExPressJS, Sequelize And MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome to this video so today we're going to be doing something that haven't been doing on this channel we're going to be talking about javascript so i'm going to build a simple rest api using express ks which is a framework that allows you to build web applications with javascript hds is a framework that is so similar to how you may see flask or fast api as in it doesn't give you a specific way of doing whatever you need to do so it is a fast and opinionated minimalistic framework for node.js i'm also going to be using sqlize which is basically an orm that helps you to build node.js applications and it acts as an org it helps you to be able to write classes to be able to describe the database schema of your database so without further ado let us get into the video so right here in my vs code i have a blank vs code folder right here which i called express to do feel free to call whatever you may want i have insomnia which i'm going to be using as the testing client for our small application i also have xampp which is going to provide for me the mysql server the database i'm using is mysql but feel free to choose the database you may want so the first thing we're going to do is to try to set up an environment where we called our application application in so the way we do that is by initializing npm npm is the node package manager and all it does is to help you manage and install dependencies that you may need for your project so i'm going to begin by pulling up my terminal i'm using git bash but feel free to use your cmd or any other timing so the first thing i'm going to do is to check whether i had node installed i'm going to leave the links down in the description for how you may install node and npm however if you're on windows when you install node you are having higher chances of having npm installed so when i say npm i basically have npm installed and i'll be able to see a lot of commands but i don't know it's kind of taking a lot of time on my computer but when you have npm installed then you can be able to install the dependencies for applications so when you say npm we basically see all the commands that come with npm access add user but what you are basically interested in is npm init which is going to allow us to initialize a new node.js project or a new javascript project in this case so what i'm going to do is to ctrl l to clear my terminal and i'll initialize our project with npm in it and then i'm going to press y i'm going to give the y flag because we do not want to fill in everything and by saying minus white it's basically going to fill it to fill in the details for us so when i press enter this is basically going to create some very nice uh way of defining our project so right here we see a certain file has been created within our project and that is called the packet.json file now this packet.json file is a special file that npm creates for us and it allows us to manage the dependencies as well as how we're going to run different comments about our application this may be similar to your requirements to a txt file but it's different in a way that it also contains other things as regards to your application so the first thing we shall see is our name you can feel free to change this the version of our project which is version one description so we can basically put in a sample description for example this is our first express js app and once you're done with this so the main is basically the main file or the root of our project which is index.js but you can feel free to have any file we have scripts now this is a very important part as scripts is risk aware right the different commands that you may want to run for our application now we have one that is called npm test actually the way we do this is by saying npm run test and then this will go ahead and basically run that test feel free to comment down in the description for better explanations now what i mean to do is to basically create a file in neutrality called a simple application so i'm going to call this file index.js and it will be the main file so i'm just going to come and say touch index dot js so this will be index.js however i can also create a file usually like how you create it because i mean bash that's why i'm creating it with touch now the first thing i'm going to do is to go ahead and install express now the instructions are all on the website and i'm actually going to leave everything down in the description so that you can check it out for yourself so the command for installing express is npm install express and then we also have the save flag and the save option helps us to basically save this as our dependency for our project and it will also be added to our package.json file so let us go ahead and install this file press this command in here and press enter so this will go ahead and install our express js and then we shall continue building a simple web server and building api endpoints with this tool now that our express case has been installed let us go ahead and see what has happened to our project so what we have here is a node modules folder and within our mods modules folder is right every dependency of our project is going to be kept now think of this as a virtual environment in python but it's not like a virtual environment because actually it's like a virtual environment because all the packages are installed within our virtual environment but this is a folder that basically keeps all our project dependencies and we also have a package.json.clock that is basically necessary for keeping track of all the dependencies of our application now let me close this now the first thing we're going to do is to start building a simple web server so what i'm going to do is to go to our index.js and what i'm going to do is to start by importing our express.js so i'm going to create a constant and this is going to be a variable which is going to be i'm actually going to call this express and this is going to be importing our express so we shall be able to access everything about the express within this variable called express so i'll do that by requiring express i'll say the express is going to be required then we shall require express now what this is going to do is destroy everything we shall need about express.js library within the app within the express variable and after doing that then you're going to create a simple app instance that's going to allow us to create a server so the way i mean to do that is by saying cost up and in this case it's going to be equal to express now once we've done this we shall be able to access everything about our app within this simple app variable now we're going to go ahead and basically start our server now we start our server is by basically listening on a certain port so we need to just come and say app dot listen and in this case we shall basically specify where we want our app to listen or the port of mutual wind are up to run so i'll just basically create another constant here and this constant is going to be the port so we specify that output is going to be port 3000 now this camera right here and all i have to do is to specify that you're going to run on port 3000 and then we shall provide the callback function that will run after our app has listened on this specific port so the way i'm going to do that is by creating an our function now i'll go ahead and create a javascript request so that you guys can be able to understand javascript and me also so the other way to do that is basically console login so i'll just come and say console.log this is similar to the print function and all it's going to do is to just print out uh so in this case you may want to just show that that server is running on the port so shall just say server is let me actually use these strings and i'm actually going to use this template triple strings and what i'm going to do is to say server is running at now i'm going to specify the url which is going to be localhost and in this case i'll just basically use this syntax so that you'll be able to write to replace or write the format string in here now this will be our port and this is just enough for our server to start now just like i explained uh our packet.json file is very important and it's going to help us dictate the way to run our app so the way i'm going to do that is by coming to our package.json file and within our scripts right here i'm going to add one command and that is going to be a stat command now what this stat command is going to do is going to dictate how we're going to run our app now when we when we have a stat i mean to specify the command that's going to allow us to run our app so in this case we can just say node and then node is the node.js runtime and here i will specify the file that you're going to run which is going to be index dot js i'm going to save this and once i save this i'm going to go to my terminal range first clear it and i'll come and say npm start so this is basically going to go ahead and run our server and right now we see that our server is running at localhost 3000 which is beautiful so now let's go ahead and try to create our first api endpoint so the way we create our api endpoint is so i'm going to go to our node.js or index.js folder it's actually a file and what i have to do is to basically create app then we shall have to call the specific http method now this is very similar to how flask and festive pi work nowadays and in this case you'll have to call that method so in this case let's just call the simple get method we specify the url and this is going to be our url you can decide call it whatever you want and then a callback function so this callback function takes in the request as well as the response now the request is how you basically making your request and the response basically how is the server going to return your response so we're going to basically provide this so we just write this as full statements for example request and then response or you can choose to keep it short so i'm going to do is to just keep it to request and then response and then this is going to be on our function so what you want to do is to basically return a simple json response and then we're going to do that is by using our response object right here so i'm just going to come and say response dot so in this case we can call the different methods right here just like you see now i'm going to call a response to a data status which is basically going to help us return a status quo and in this case our return is the task code of 200 now if i want to return any kind of data i may return the data so let's say i may just return response.json i may also say response.send which is going to basically send any type of response but in our case we want to return json so i'll just return response.json so right after doing this i made save now if you're noticing here we have to restart our server so i'll have to press ctrl c and then say npm start so this will run our server localhost 3000 so i'm going to go to our testing tool insomnia and all i have to do is to create a simple request collection and i'm just going to call this express to do and once i have this i'll create a simple http request now this is going to be what we're going to provide the url so can you say to name a request now this is going to be hello world we haven't actually specified our response now i'm going to provide the url here which is going to be our localhost 3000 and since it's on our route url i'm not going to provide the url so what i have to do is to go right in here and specify the json that you may want to return now i'm simply going to create an object here and i'll provide a message and then i'll come and specify that you're going to say that this is going to be hello world let me save this now if you have noticed there is one problem that you have here the problem is every time i make changes to our index file we have to restart our server or we have to stop the server and then restart it again now there's a specific package that allows us to run our server and reload in case we have file changes and that is called nodemon so i'm going to install nodemon and set it up so that when we make changes our server is going to restart so that you don't have to stop it and then restart it so i'm going to press ctrl c and then what i have to do is basically install nodemon so i'll do that with npm install save and in this case i'll just say note mode so this will go ahead and install node mode and after installing nodemon now i'm going to go to our packet.json file right here and all i have to do is to change this from node to node mode so when i go ahead and save when i say npm start our server is going to run and now we see that this will look for any changes it will watch for any changes within our js files and then we load our server which is good so now each time we make a change for example in a save in this case we see that our server is restarting okay let's go ahead and make our first triples so what i'm going to go and do is to go to our locals 3000 when they make this request we now see our response is being written and our response is hello world yes so we've been able to make our first api request with node and express so now we're going to be building a very simple cred api the next thing we're going to do is to set up our api endpoints that you'll be able to do the crowd functionality so i'm going to do is to go ahead and create a router which is going to separate our logic for our cred from our index to this file so i'm going to do that is by coming right here so i'm going to create a new file and i'm going to call this routes dot js and this javascript file is going to contain whatever routes or whatever routes that you shall have for application now inside here i'm going to first create an express variable so this is going to allow us to access all functions within express.js and i'm just going to come and say const express is going to be equal to express which is going to require express and once we are done in this i'm going to go ahead and create a router and this router is basically an instance from which we can be able to create our routes so the way i'm going to do that is by creating a variable which i'm going to call router so this router variable is going to be express dot router and once i've created this instance i'm going to go ahead and create the various endpoints so the various url are not simple to do apa so they want me to do that is by coming right here and saying up dot in this case it's actually going to be up so shall call get now the rate our url so we're going to start with the one for returning all our to do's so i'm just going to come here and say have dot yet to do this then we're going to do the same thing like we did we're going to write we're going to write basically a callback function that has a request and the response has the params so in this case shall have request and then response and then this is going to basically return all our to-do's so i'm going to do the same thing for all the other routes so just going to come and copy this so that is for getting this will be for posting so we're just going to come and say app.post and then we shall have one forgetting how to do by its id so i'm just going to come here and say app.gate and this will be slash to do's then i'm just going to come and provide an a parameter so this is going to be a path parameter that's going to allow us to access how to do better id and then we do that with with express is by specifying it like colon and then we shall just provide the id of that specific to do now we're going to do the same thing for updating so just going to come here and basically set up the one for updating now this will be put so just going to come and say upload put however if you're using a patch request you can also do the same thing upload patch now i'm also going to write the one for deleting i'll just come and say app dot delete and this will be the same thing actually for good i'll just copy paste it and then say app dot delete and one mistake i've actually done here is all these are supposed to be on the router not the app because you don't have a we don't have an app instance now we need to basically use ctrl d to remove this and then we shall have all our routes on the router now the next thing we need to do is to make our main application file be able to know that we have these routes now i'm going to export our router so that we'll be able to access it within our index.js file and register it and register all our routes on a specific url so let's do that so i'll just come right here at the bottom of our code and all i have to do is to say module dot exports and then i'm going to equal this to our router instance so that you can be able to access it within our index.js i'm going to come within our routes and then all i have to do is to just export our router so we're going to access this within our we're going to access this within our index.js so the one way to do that is by requiring it or importing it so i'm just going to come and say cost and in this case we're going to basically call this our route so i can basically say uh cost i'm just going to say api let me just call this api routes and this is going to be equal to require and in this case we shall require our routes so we can just basically say slash and then access our routes so this is basically requiring a file that is in the same folder as our index.js file all right now that you have this the next thing is to register our routes from our router into our application so that we can access them via a specific url so to do that we're going to basically come and use these functions i'm going to call the app.use method and what this does is to basically help us to register things such as routers or add middleware and any other thing to our application so i'm going to say app.choose and then the first thing i'll do is to specify a url so it's going to be on our api and then i'm going to go ahead and what i have to do is to basically register our api routes so once we are done with this now let us go ahead and try to test this so every time we're going to make a request to our api routes we are going to be using the slash api url so the way i'm going to do that is by coming to our routes now let's try to send a simple response i'm just going to come and say response dot json and in this case shall provide a message so let us just provide a simple message and this message is going to be let's say all to do's for now when i save our server is restarting thanks to nodemon so when you go ahead and make this request i'm going to go to insomia so it's going to be our first get request so just going to say get all to do's enter so i'll just go to our localhost 3000 slash apa slash to do's and when i send in this case we see that's returning a message of all to those so at this point we've been able to register our api endpoints to our main application now the next thing we're going to do is to set up a database so that you can be able to persist as well as be able to query for all our to-do's now this is where sqlize comes in sqlize allow us to allows us to basically create a way we can write javascript to describe how our database is going to be now i have mysql right here it's running i'm instead the mysql service so it says that i'm also going to uh start apache service and i'm going to use phpmyadmin to create for us a simple database so i'm going to go to my browser i'm using chrome right here and i'm going to go to my localhost i won't have to specify any port and this will take me to the examp dashboard now on the xampp dashboard i'll go to the php my admin i'm going to make sure i leave a link to this in the description as well so that you can check it out so and to access that phpmyadmin that is taking some time all right so right here is my php admin so i'm just going to create a very simple mysql database and i'm going to give this a name of express tasks so call it whatever i want and i'll go ahead and create this so our database has been created we don't have any tables yet so the next thing we're going to do is to set up our database connect to it and be able to create a model that's going to define our database schema using sqlize all right so let us go ahead and connect to our database so the first thing we're going to do is to go ahead and install our sql list so i'm going to go within our project folder and what i have to first do is to install has equalized with npm install sqlize so it's going to be sqlize and then we are going to save this so i'm just going to pass in the save flag so this will go ahead and install our sql sqlite has been installed let us go ahead and set it up so that we connect to our database i'm going to first create a file right here and i'm going to call this our db to js feel free to call it whatever you want now links our db.js we're going to begin by importing our sqlize class now what this class is going to help us to do is create a connection object and this connection object will be the one to help us connect to our database so i'm going to begin by creating a const and i'm just going to basically require in this case we're going to require equalize now the sqlize class is going to be from sqlite so just come and say we're going to import us equalize and right after creating our sql is actually importing our sqlite so we're going to go ahead and create an instance of sqlase that you show us to connect to our database so i'm going to do that is by coming right here and i'm just going to come and say cost sql s and this is going to be you go to a new simple lase object or instance call it whatever you want now here we're going to provide a bunch of options so in this case we're going to first provide that database we need to connect to and let's go back to our phpmyadmin and see the database we created so our database is express tasks i'm going to head over here and basically say it's going to be express tasks or whatever you call your database now the next thing we shall have to provide are going to be the username and the password so in this case we need to come and provide our username and we're also going to come and provide our password now actually this username is going to be root so i have a root user that used to graduate user doesn't have a password but you need to have a password for your application so since they don't have a password they have a plain a plain text string or a plain empty text as a password now go ahead and provide the options so in other options are going to be the dialect so the dialect basically specifies the type of database you are connecting to in this case we are connecting to my sql or my sql whatever i want to call it so another thing which i have to define is going to be the host and in this case our host is going to be logos since our database is running on our local mysql server so in this case i'll just come and say localhost now that you've created the connection object let us go ahead and use it to connect to my sql so the way we're going to do that is i'm going to create a simple asynchronous function and when you call this function we're going to be able to access our mysql so i'm just going to come here and say const connect and in this case we're going to call it connect to let's just call it connect to db and this function is going to be an asynchronous function so we're going to say async and what this is going to do is basically going to connect to our database so we need to create a tray catch statement here so a tray catch statement to have a try and catch this is similar to try and accept in python so here shall have catch and then we shall catch an error so we shall try to connect to our database using our connection object so i'm just going to try to await since it's uh it's also a nursing function so we're going to call sqlize dot in this case we're going to call the authenticate method and what this is going to help us do is to connect to mysql so in case we've connected successfully i'm going to bs basically console dot log so we shall just say successfully connected to our database and right after doing that then let's also catch some errors so in case we have an error we shall just console console.log that error so i'll just come and say console.log and then i specify that we're going to console.log that error so now that we've done this let us go ahead and export these two things we're going to export our sqlize object because it will help us to create models we're going to also export our connect to database uh function here so i'm just going to come and say in this case we're exporting two new two things so shall just basically say module dot exports and this is going to be equal to an object now this is going to have sqlize as well as our export it's actually going to be connected to db so i'm going to basically save this and once i save now let us go ahead and connect to our database so we need this to run on our server starting now we need to basically go ahead and require this now index.js so i'll just come right here and say cost i'll just say db and in this case we shall basically go ahead and require this so can actually also say that you're going to require them like select this so i'll just come and say it's going to be required then we shall call the file so in this case it's going to be a db so let us go ahead and access this so we're going to require sql s object we're also going to require our connect to db function and this is so far so good now what we do what do we need to do we need to actually connect to our database now we're going to connect to that database is by calling the connectdb function and we're going to call it asynchronously so what i'm going to do is to change this callback function right here and i'll basically make it a synchronous and another thing i'm going to do is to also call the function after starting our server now the other way to do that is by coming and saying await and here we shall just say uh connect to db all right so let us try to save and try to run this when i save our server is actually restarting because we are using sql s now we have an error right here so what this era is telling us is telling us that we do not have the required database driver to run our application now mysql uses the mysql tool package to help us access mysql with node.js so i'm going to go ahead and stop our server and install our mysql to package so i'll do that with npm install and in this case shall install mysql too and then we also save it so it's going to go ahead and install my sql tool and shall connect to our database [Music] all right so now that our mysql2 has been installed let us go ahead and connect to our database so i'm going to go here and press ctrl l so this will control actually clear our terminal and then i'll start a server with npm stat now you see that our server has been started and now we also see that our database has been connected to so we've been able to connect to our simple database we've created now let us go ahead and create a model that's going to allow us to create database tables which we shall persist our data all right so let's begin by creating a models.js file so this is going to be a file i'm going to click here and we're going to create a new file so i'm going to call this file modus dot js and within the models.js file we're going to describe a simple database model so we're going to pull down my terminal so the first thing shall have to do is to get access to a sqlize object or instance so i'll just come and say const and then we shall basically require it so just come and say require and right here i'm going to require our db.js because that's where our sqlize object was created so i'll just come and import it and once i've got access to it now we're going to use it to create our model so the way i do that is by just calling a simple method called a define method and what this does is to basically help us to create a model now the first thing which i have to describe is the model name and in this case we're going to call this the tasks actually it's going to be a test since it's a database model it will just have a singular name and then we are going to go ahead and describe the different fields this model is going to have now this is going to be an object and it's going to have each of the fields described as an object now let us first describe the first field so the first field they're going to describe is going to be our content and the content of each of our tasks is going to be a field so we're going to describe the various attributes that a field is going to have now the way the first attribute is always going to be the data type of that specific model field so in this case i'm going to access certain class that has all these uh data types already described and that is the data types class so i'll have to import that from sqlite so i'm just going to come and say cost and this case shall say data types oh let me actually be sure what i'm importing so i'll just come and say require and then i'll say sqlize and right after i've done that i can just come and say here that you're going to import our data types class so once you have data types now we're going to go ahead and describe so the first argument is going to always be the type or the data type of that specific model field and in this case we shall have our data types so this is going to be a string now you can go ahead and basically describe other things such as unique constraints or let's say you want to have a max length or you want it to have anything like validation so let's say i wanted to validate this we may come and say it's going to be an object and to have a max so let's say the max length of each of the content should be let's say 150 and then we can describe another field so let us go ahead and say that we're going to have our field and let's say this field is going to be our hour so i'm going to basically call this our description of that specific task so in this case we're going to call this a description and description will also be a field so it will be of type and in this case shall just say uh data types do what now let's say i wanted to describe this as text and we may also do some validations or leave them out and let's say i also want a boolean to show that each task is complete so i'll just come and say is complete and here which i'll have an object this object is actually going to be a type it's going to have the tape and here we shall just come and see that's your tapes dot boolean and yeah this is basically a simple field now we also have some timestamps or basically fields that are added for the data created a certain uh object or a certain database entry and stuff like that as we're going to see now this is just enough for us to describe a simple database model using sqlize now the next thing we shall have to do is to go ahead and basically sync our database or sync our module to our database now to do this we are going to use our sql s to go ahead and create our module in the database now the one we need to do this is by coming right at the top here and what i have to do is to call sqlize dot in this case we shall just come and say sync and this is going to take in the sync options so once you have this method we are basically going to sync or create the data base using the model we've described so i'm going to go ahead and save this and once i save this i'm going to come and pull up our terminal i'm going to go to the second batch time you know i created right here and all i have to do is to run our motors file and by doing this shall have our database created so that's going to come and say node and i'll call our modus dojs and this will go ahead and basically create our tasks table within our mysql database now right here we see that our database table is created using this sql statement that you see down here so let's go ahead and verify this with our phpmyadmin so i'm going to go to our web browser so macro right here i'm going to go to our express database express test database and now we see that have one table which is the tasks table when you go to our tasks table we have it having all the fields that we've defined such as the content the description and this complete we also have created add and updated at now in this case let's say we wanted to add some extra definition of our fields i may just come right here so let's say i wanted to describe our default for our is complete the way i do that is just come here and say default value and let's say our field is going to have a default value of false now i'm just going to come here and say nodemodels.js and this will go ahead and basically create that change within our database and then we shall go ahead and build api endpoints to create read update and delete our tasks so when you go back right here we see that our fields have been created now let us go ahead and use our orm to basically create read update and delete all our tasks all right so i'm going to go back so what you need to do is to go to our routes.js where we actually have the various endpoints for doing all our cred functionality so the first thing i will have to do is to first import our model so they're going to import our model so we basically defined one right here so we defined one here so actually i just called sqlize.defined and defined the model but i did not put it in a variable so if i want to do that i'll just have to come and say cost and in this case i'll just come and call this a user class and once you have this user i'll just have to export it and i'm going to remove this line since it's only going to help us when we are creating our database tables so i'm just going to save and once i save i'll first export our user model so i'll do module.export and in this case we're going to export our user model so i'm going to go ahead and save now that i've saved i'm going to go ahead and access that in arrows.js so i'll just come and do that with cost user in this case our user is going to be require shall access our modules so this case going to be modules so let us go ahead and access all our tasks now what i'm going to do is to first query for all our tasks and then i'm going to do that is by just coming here and what i have to do is to just come and call await so we shall have to make our request handler function or our callback function and asynchronous function now now that you have this so she'll just come and say await and user let me call all methods so you have all the methods that are available via our model class so in this case we want to find all this is one that's going to return all objects for that specific query and in this case i'll just have to put this within a variable so let's say cost and this is going to be tasks and it's going to be equal to await user dot find all now what we need is to return this within our json so just going to come and say response dot status so she'll have to first specify the datas the status which is going to be 200 and then we shall have to specify what you're going to return within the response body so i'll just come and say json and we shall provide our tasks now that we have this let's go ahead and save and when i go back to my insomnia right here we've been returning this simple message and now we expect to return an empty list of tasks or to do so when i send we now see that this is returning and you an empty list of tasks now let us go ahead and create tasks so the first thing i'm going to do is to get data that you may send via any client or any web app or a mobile app and stuff like that and we're going to do this is by passing a request body to our api so to do that i'm going to begin by accessing those so since we have a request object here can give us access to our request body now let us create our data so i'm just going to come and say const data is going to be go to our request body so shall access that priya request dot body and once you have this now we're going to just simply return this in our response for demonstration i'll just come and say response to status the response that has got to expect to return for creating an object is going to be 201 now i'm just simply going to come and say json and we shall return whatever we shall basically send on our client so let's go ahead and test this out when you go right here we are going to create a new request and this new request is going to be for creating a task so just come and say create a task and once you have this i'll just come and say that you're going to have a post request and shall have our url as localhost and this is going to be a 3000 slash api slash to do's and i'm just simply going to provide a body that's going to be json and here i'll just have to provide let's say a name and then let's say jonah when we send this we now see that we have a 500 internal server error and what this is doing is to actually tell us that request.body is not a function actually called it as a function so i have to remove those parentheses let us try to call to basically make this request once again so when i send we now see that nobody is being returned for the response and that's because we cannot be able to access our body as we send this now this is where the body passer comes in but the person is basically a packet that will ask to have middleware that's going to allow us to send our that's going to allow us to send json to our application now the we're going to do that is by coming right to our index.js and adding it via app.choose so i'm going to stop our server and what i'm going to do is to basically uh call our body parser so install it with npm i and then we are going to install body parser and we shall also save it so this will install body passa and now that you've installed body passa let's go ahead and basically enter it or register it or use it within our application so i have to first import it so i'll just say cost uh body cost body parser is going to be equal to require and in this case we are going to require our body parser and now once we have this then we can go ahead and use it so i'll just come and say app dot use and in this case we shall use our body parser all right so once we have this then we can go ahead and basically try to make the request once again so when you go back to our api introduce i'll actually start our server with npm start now that our server is started let us go ahead and try to send our request so when i go back here and send we see that she seems like we have an error still and right now we see uh body passer is let's actually see what this is telling us but the passer is deprecated so we can use individual json or url encoded with os so i think the way we need to solve this is by removing body passes so let's try to remove this and then what we can do is to basically use the json middleware that's provided with express so i'm going to do that is by saying app.choose and here we shall just come and say express dot json and i think this will be helping us so when i save as i've explained to restart now let us go ahead and try to make the request once again so i'm just going to come and send and now we see that our body has been returned wow this is good all right so let us go ahead and try to create a new task so there we're going to create a new task is by getting the data that is sent from our client send it to our database and then be able to store it so there you need to do this is by coming right here and then we're going to head over to our routes so within our routes shall access whatever value that we shall get from our data so we can use this very interesting uh destructuring syntax and then be able to get whatever you want to get out of the request so i see the fields that we have we have uh our model.js we have the content the description and this complete so what i can do is to go ahead and maybe access the content and the description since we have a default value for our is complete attribute so i'm just going to come here and say content and then i also come and say description and once you have this thing i'm just simply going to come and all i have to do is to go ahead and maybe set up our database so that you'll be able to create a user using these credentials so oh i actually see it creating here user it's not creating a user it's basically going ahead to save a task having these credentials so to do that i'm just going to come and create a new user or a new task [Music] week sorry for that simple distractions what i was saying is after getting the content from our client the next thing is going to be to create a new task so i'm just going to come and say new task and this is going to be creating a task using the task class what did we actually call it when we go back to our models we call this user but it's supposed to actually be tasks so just going to come and access this and shall have tasks so sorry for that so i'm just going to come and say task and when i save this i'm also going to change how we call it right this side in our routes.js so just going to come and just control this so highlight user and then say ctrl d here shall access task so and now we are safe so i'm going to go ahead and save this and go ahead so in this case we're going to create a new task so let me put my headsets back on so they were going to do this is by coming here and i'll go ahead and basically wait oh actually i'm not going to await what i have to do is to go ahead and basically use a task class to create a new user so i'll use the dot build method and it's going to take in a record or an object with the various fields that we need to store so in this case i'll just come and say that we're going to build an object containing our content and the content is going to actually be the content has got it from our client and then also going to create a description so it's going to have a description and that description is going to be the description has got it from our clients all right now that we've created our new task let us go ahead and save this task to our database so the way that we're going to do that is by awaiting a method so meaning that this will have to be an asynchronous callback function and then i'm going to come and await new tasks so since this is a task object we shall just come and say dot save and that will save it to our database and since we've saved it we are going to basically return the new task as the response data so what you can actually do is to just come here and use a try catch block so i mean just try to say try and then try to save the task and in case you have any errors we shall catch those errors so i'm just going to come and say catch and then errors and then i'll just basically return those errors as json so i'll just say return or basically return them as json with response dot json and then return the errors so once you've done that so let us go ahead and try to create a new task so now go to our installment right here we're going to create a new test so task is going to have content and let us just call this a sample a sample content and then we shall also have a description and let us go ahead and call this our sample description so let us go ahead and save this in our database so when i say send in this case you see nobody is being returned for our response let's go ahead and see because it did not return it so i'm just going to come here and say uh we're going to return our new task so when you go back seems like this has been created so let us go ahead and maybe call this sample content 2 and sample description true so when you send this now see that our task has been created so when you go to get to allow to do's when you send in this case you see that all uh to do that being returned to us now this is working so let us go ahead and also try to create other tasks so i'm going to create a task i'm going to call this uh learn javascript and then i'm just going to come and say [Music] i will have two hours of learning yes now this uh not complete so that's why you see it's complete paying for so when i send now see that these are actually working so let's go ahead and try to create another test so let me say uh edit a video so i'm just going to come and say edit a video and in this case i'll say i will edit so in this case i'll say i'll edit a video on express js so when i send this a task is going to be created so when you try to get all to those in this case you see that you now have four tasks or to do this in our database good so let us go ahead and try to get an individual tesco to do let me actually create this and call it get all to do so i'm just going to come and say get create add to do so now that you have this let us go ahead and try to go ahead and access one task so to do that that's going to be simple so what you shall have to do is to just come and say cost task and this is going to be equal to so shall i have to call an asynchronous method so in this case shall just come and say i wait and what you shall have to do is to say task dot find so in this case since we are finding one can just come and say touch dot find one and in this case i have to specify the options so just going to provide an object and in this case we shall specify where the in this case you'll have to specify that specific field so in this case where the id is equal to so shall have to access the request parameter which is our id so to do that shall just come and say request dot param it's actually say request.param actually it's going to be crystal.param.id which is our id provided as our request all right in our requests so once you have that then let us go ahead and try to return this so shall just come and say response dot status which will be a status code of 200 and then to json then we shall provide that task so okay so let us try to access this so i'll go to our insomnia right here try to create a new request so this will be get uh to do by id and then we shall create this with the url of localhost and in this case it will be 3000 and then slash apa slash so this will be slash to do's which is going to be to do and let's try to access that i that to do with an id of one so when you provide that and you say send now this is returning an error let us go ahead and try to find out so when i go here we're going to see that our weight is only valid in our sync functions it seems like this is not an async function or we forgot to basically i think a weight so let me try to fix that so when you go back here and this is end we now see that our to-do has been returned and now let's try to update so for updating we're going to need to get data from the server we're also going to need to query for that specific test we need to update and then go ahead and update it all right so the first thing we're going to do is to implement an endpoint for marking a task as complete so before i use output method right here i'm going to write the same method but it's going to be a patch method because it's going to update on the one field which is that is complete field so i'm just going to come and change this and i'll copy this so route dots so in this case we're going to have route.patch and what you shall have here i'm doing fast square for that specific task so we already have that here so i'm just going to copy this uh this specific expression and once you have that then we shall need to now go ahead and access that different data that we want to update now since this is a patch method what you want to update is a tasks is complete status so to do that shall access this so just going to come and say uh is complete and this is going to be access via request dot or sorry for this it's going to be request dot body and once i have this then i'm going to go ahead and i'm going to go ahead and update that task so what you can do is just come and say task dot in this case this is actually supposed to be an asynchronous function so i'm just going to come and say async and then here shall have to await then oh it's actually a wait the so this is going to be task dot in this case what you can do is to update so we can just say task dot is complete so and in this case you can set it to true and once you have this then what you can do is to go ahead and save and we can just go ahead and say task so just going to go ahead and say await task dot save so that we change our task in our database so i'm just going to go ahead and say how it does task.save we call this method and then after doing that we shall just return our response so i'm just going to come and say response.json or let's first specify the status so shall have our status as 200 and then specify the json and the json in this case is going to be the task that we've updated so let's go ahead and do this so when i save seems like we are having an error here array test. is complete or there are two methods to do this so let me try the the one of settings so i can say how it task.set and then we provide the object containing the specific field one to update and this field is complete so so i have is complete and then this will be true all right so now that you have this uh i'm going to go ahead and try this out so when i save let us go to our insomnia or create a new task and this is going to be uh update to do status so it's going to be update to do status and once i have this i also change the method from get to patch and then we are going to have a localhost so this will be localhost of 3000 localhost port 3000 and initial access to do so let us try to access to do slash one and it's actually going to be on the slash api role so slash api slash to do slash one and once you have that then you're going to go ahead and specify the tablet is going to be json and in this body we shall just come and say actually this is going to be since we are getting this complete from the request body now we'll have to set this to true so what shall i have to do is to just come and say it's complete now that is complete you're getting is the one from the request body so to have to set this to true so when you go here shall just come and say is complete and here we shall specify that this is going to have a value of true all right so let us try to see we are going to set this to is complete which is going to be true meaning we can even turn one that is complete to false and stuff like that so let us go ahead and try to save send this request and now we see that that task has been updated and now it's complete yeah stuff is working right now so let us go ahead and try to update the entire task using a port request so i'm going to do that is by coming on our route.put and we're going to go ahead and basically write the code for updating now this is going to be so similar to what we have here i'm actually going to copy this entire code and paste it right here now uh i'm going to go ahead and try to update all the fields so we have is complete we have the content we also have the description now these are all fields that you need to update now task.set then you have to specify those so we're going to specify content it's going to be content and then we are also going to specify the description which will be equal to the description that comes from our client and then we go ahead and save so it will return the same the same status code as well as the task that has been updated so i'm going to go ahead and save hoping that our server is running no errors so we are having an error we need to ask we need to first set this uh callback function to be asynchronous and then our server is running so let us go ahead and try to update an entire task so when you go back here when you go back to our to do so in this case i'm going to go create a new request and this is going to be let's say uh update an entire task so it's going to be update an entire task so i'm actually spelling it wrongly so this is going to be update entire task so once you have this then i'm going to change this request from get to port and then also provide the url so it's going to be localhost 3000 slash api slash uh to do's slash maybe one social provider but it's going to be json and this json is going to have all fields that you use to create a task and even just try to get this and update an entire task with it so i'll paste it in here all we need is the content the description and this complete so we need to change this and then maybe remove this comma and try to make things a little bit true so our id of one is having a task of sample content now let us change this to something like play basketball and now we are going to change our sample description to let's say play some ball with the boys okay so once we have this then we can go ahead and maybe send and this will update our entire task of id one so in this case you see that the task has been updated even when you try to get the id of one we're going to see that our task has been updated when we get all to do's we are going to see that our task has been updated now stuff is working yes all right so let's go ahead and try to delete to complete our cred functionality so to delete this we are going to need to go in a fold in a code right here on a delete route and the first thing we shall have to do is of course get that specific task one to delete so i'm just going to come here and get this task and once i get it i'm going to paste it in here so i just come and paste that right in here so i do that now shall i have to just await task dot delete now since this is an asynchronous function i have to change this and add a sync and once i have that then let us go ahead and try to to do this or to return a response so since this is a delete request we expect nobody to be returned with a request to the response so shall have to just come and say response dot status and in this case our status is going to be 204 no content meaning we have been able to delete our tasks so let us go ahead and try to delete a task so when i go to our installment right here i'm going to create a new request and this request is going to be one for deleting a task so once you are done in this i'm going to call it a delete request so let us delete a task from you and say localhost thousand slash apa slash to do slash one and when you try to delete we see that error returning receiving data from the peer i don't know what's wrong but let's try to see so that's the delete is not a function sorry for this it's actually task don't destroy i'll leave a link to the description i leave the link to the docs in the description so this is going to be destroyed and when i save hopefully it's going to work so when i send it seems like we have a problem let us see so the problem we are having it's actually deleting that is taking a lot of time so let us try to see so are we actually returning the response i think let's try to return sam json here run empty object and when we try to run our server again our server is now running so let us try to delete that specific task so we don't have any data for that specific so cannot read properties of now which means the task does not exist but since the test does not exist what we need to actually do is to return a 404 status code in case we don't have it so we're going to create some error handler functions and what they're going to do that's going to help us to basically catch these errors or basically return a response in case we have an error on the server side so what i'm going to do is to go within our index.js and then i have to do is to describe this function so i'm going to use app.use before you describe any routes so i'm just going to come and say app don't use so we're going to basically create these functions that will be running before any other request and they'll basically help us to catch the errors so this is going to be a function and it's going to be taking in the request well as the response and what you shall have here is we're basically going to return the response so shall just come and say response but in this case this is actually going to be requested and response sorry for that so it's going to return the response and in this case that the test will be 404 in case a task is not found and then we shall return the appropriate data so in this case she'll just come and say response.json then you shall return a message of let's say to do not found or resource node found so let's just say resource not found and once you're done with this now i'll do the same thing for our 500 status code so i'm going to just come and say updates use request response and in this case it will be 500 and let's say in this case we just need to return an error and let's say oops something went wrong which is a server-side error so i'm going to go ahead and change this to wrong so what you have here is this request handler so let us try to make a request again so we are not getting data from the peer i don't know why this is happening i need some help if you can help me so what's happening is i think the task has been deleted so let us try to confirm with our let's actually try to confirm with getting all tasks so get all to do's we try to send it seemed like we could we can't connect to the server let us just try to restart our server so we need stop our server and then restart it with npm start so let us try to send this request again and now we see that our task has been deleted so let's try to delete a task with id of true so i'll try to delete a test once again and this is going to be having an idea of true so in this end we now see that the task has been deleted and no and no body is being returned for the response so let us try to get it to do by id so when you try to get it to do it's going to be responsible now so i wonder if it did the same thing feel free to uh correct me down in the description but for now we have our to-do routes implemented so if you're good at javascript and you watch this video please leave a comment down in the description about what you can do to better api i really had fun building this simple api with express mysql and sql lanes thank you for watching guys and i hope to create more videos about javascript see you in the next video bye
Info
Channel: Ssali Jonathan
Views: 9,098
Rating: undefined out of 5
Keywords: expressjs, nodejs, nodejs api, sequelize nodejs, mysql
Id: X5PygyNG71g
Channel Id: undefined
Length: 75min 59sec (4559 seconds)
Published: Wed Sep 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.