How to Build a REST API with Node.js and PostgreSQL - Complete Step by Step Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
as you know my name is kind of tech pro and today i'm going to show you step by step how to build a complete rest api using node.js and postgresql now if you can build an api and also set up the database then you can also build a ui possibly using angular that means you've done full stack software development and that is exactly what you need in today's class we are going to simply cover how to build the api using node.js and also create our database and put scroll sql so we are going to be setting up postgresql setup.js i'm going to do that quickly and also we are going to be using postman i'm going to show you how to set it up to do post requests to our api so if you are joining me for the first time hit on the subscribe button below so you don't miss any update from me so let's get started as usual i used to create a step-by-step for you in my website with all the source codes so in case you missed something you can actually copy and paste so if you go to my website basically if you want to find my website simply go to google and type kind of kind on the genius so this takes you to my website as you can see and simply go to kinds on the genius and simply scroll down uh and you go to i wrote this recently so you can also always find something i wrote recently i can see it right here build the rest api with node.js and postgresql we're going to be doing gate pause put and delete uh easily it's going to be step by step and it's going to be very simple so the first thing we are going to do i'm going to try to follow the step as much as possible let me increase the size of my screen so the first thing you want to do is to install postgresql so you can download it from here so if you go to the downloads uh uh for postgresql you can actually download so i use mac so i can actually download for mac so once it downloads you simply run the installer to install postgresql along the line it's going to ask you to put in the password or the admin password so once that completes you should be able to set up postgre sql since i've already done it i'm going to just show you uh how it's going to look like so if you go to see this is pg admin that is what you are going to have once you install it so this is quite easy so i'm not going to be going through our installation of postcard sql at this time so once you've succeeded in setting it up you have in your browser you are going to have this page so simply enter the password you specified when you are setting it up and you are going to come here so at this point you have postgresql however we can actually create a database but for now i recommend let's not create a database let's use the existing database in postgresql so if you go to databases right here you see databases you can see postgres so this postgres comes by default once you install postgresql so if you drop down now you see a lot of things that is a bit confusing but simply go to schema and go to public so basically this is your database so actually don't worry about all these things you see simply go down to tables and right click on tables and click on create table okay so once you click on create table give it a name so i'm going to call it users also you can also call it users leave everything at default and go to the columns now you need to define the columns by clicking on the plus sign right here so i'm going to click on plus sign the column is going to be id and the data type is integer in teacher and uh this is gonna be my primary key so it's not gonna be null and it's also going to be the primary key i'm going to add another one so let's say first name and it's going to be uh text and let's also add another one last name and it's going to be text as well and let me just add one more let me call it location and it's going to be text okay so i'm going to save everything at this point we've created our database in postgresql just to play around let's just insert one data let's insert one record into this database so simply right click and save view edit all rows so at this point you have at this point okay so you can actually double double click here and enter the data so i'm gonna enter my record at this point kinds on okay cancel so sometimes um this behaves this way okay okay so i'm entering it wrongly okay so this is fine and i'm going to enter location as well so if you've come this far you've completely setting up postgresql okay um so i'm going to actually set my browser in this way so the next thing i want to do is to set up node.js okay so that what you're going to do if i go back to my website so the step by step is here so you have to download node.js and install if you download and install node.js you are not going to have any uh icon on your programs or on your desktop so it's simply a runtime available in your system so how do we know that node.js is installed the next thing we are going to now do is to open up vs code visual studio code if you don't have it download and install it so i'm not going to be going through how to download vs code i'm sure by now you should be able to have some ide maybe intellij it may be vs code for me i use vs code so i'm going to open vs code right now so here is my vs codes so basically we are going to simply create two java files so it's that easy so i'm going to say new uh file so let me just come here maybe i'm gonna just keep this two windows side by side so let me put this one this way and this one this way great okay so at this point i'm going to save file uh normally i like to say so this in chrome okay so i'm going to save file and say uh it's actually new folder um okay let's just say open so once you go to open you have to actually create a new folder i'm like i would like to call this folder um nodes pj tutorial all right so this is the name of the folder or basically this is going to be our workspace so if you have existing folder you can also open that folder via open folder so let's go back to the steps at this point all right so the first thing you want to do is to install postgresql here so what you're going to do for vs code is simply go to i think you this is getting back to chrome okay go to timeout and say new terminal so the new terminal you can see right here you have your thumbnail again let's check if node.js is installed i'm going to say node version and now you can see it tells me version 14.4.0 so i have node.js installed in my system so now i'm going to install um rpg so pg helps us is a library that helps us make a connection from our application to postgresql so i'm going to say npm install pg minus minus and it's going to actually install uh pg and also also initialize the node modules right here so i'm going to kind of increase the fonts if i can do that perfect i think it's better now all right so the first thing is to create a folder a file so a file one file is gonna hold our connection the second file is going to actually hold our codes so i'm gonna create one file so i'm gonna write i'm gonna click on this icon here i'm going to call this file connection.js so all the files we are going to be working with is going to be javascript file now to save time i'm simply going to copy from my website this piece of code and paste because so this piece of code actually defines the connection to your postgresql on your local host on your local system so i'm going to paste it so basically what is happening here is saying that we are creating a new client and that client is going to connect to localhost the user suppose grace the port is 543 to the default ports if you have not changed anything the password is a root user and database is supposed christ so these are things already defined in your in your postgresql database so this is fine i'm going to just save it ctrl s to save now we have our connection and the next thing is to create our server and client now this server we are going to be creating will is going to use this connection so let's go let's see so we are going to be doing that using a different file so i'm going to create a new file i'm going to call this file um api dot js all right so the first thing you want to do is create a client by using the connection we already have we create a client using the connection file and also uh creates express applica s price uh variable so express i think we need to also set it up so i'm going to say npm install express minus dash save because express is needed to actually make uh rest api calls so i think i forgot that i'm gonna update that in my website okay so so what is happening here is that we are creating a client is going to use this connection to make connection to the server to the database and we also have to create a server which is up so once you say cons app is equal to express you are creating a server you're going to specify the port it should be listening to and then say what is going to uh display the console once it starts running the server is up and it starts running and the next thing says client.connect okay so basically if you are working with node.js application you're always going to create a server at some point all right so the the the next thing we want to do is write a query to get all the users but before we write this query that gets all the users i'm going to tell you about this body password body pass is in charge of handling format conversion between json and a from anto json of data coming from the body of the request coming onto your application so that's why you need body process so i'm going to install it npm install body parser i think that should be the right spelling it says a complete log okay i think i got the spelling correctly let me see okay so his body passer uh like this let's see all right so you actually say body on the skull uh body dash passer okay that we have the next thing we want to now do is to do the fun part to write the get uh code to actually make a get request to the database to fetch data from our database and to do that we are going to use app.gates now i'm going to take some time to explain it although i'm going to copy and paste it and also i'm going to explain it so let me just copy and paste and then explain it to you as well so the app you already have here we created a variable called app which is set to express an instance of express which is used to listing uh to create a server that listings for connection coming okay now the connection is coming we are we need to figure out the routes or the url where this connection is coming from in this case the connection is expected to come from slash users so if you go to ports 3300 where this server is listening to and go to slash users it is going to come to this piece of code that says app.gate slash users and the next thing you want to specify is an arrow function that contains requests and response these are the two items needed for for for a request coming from the client so this request response uh is a is the parameters to a function that is going to execute once the request comes in so that's clear right here so in this function you are going to use the client to query the postgresql database simply write a get a select statement that's going to return data now client.query this query function also takes two parameters the first of the first is a string which is actually the query that is going to execute against the database and the second is the arrow function that is going to handle the response from the query so in this case the arrow function takes to parameter is an arrow for error err or the result so if it tries to run this query and has some issues it's going to uh we are going to have error okay and that is why when we say we say here if not error then we are going to say areas dot signed areas is the parameter from the app.get okay now the result that rows is the result coming from the query okay and then when you say area is assigned is going to send these results coming from the database query on to the client making this request let's just try to see how it goes so i'm going to save everything and we are going to execute this we are going to run this code so i'm going to say to run it i'm going to simply say node api dot js so it tells give some error client has already been connected you cannot reuse a client okay so okay so we are having a challenge here we are having client.connect two times so what i'm gonna do let me just take out the second client.connect from api.js so i'm going to simply save everything and at this point i'm going to clear my terminal and i'm going to run the same just give it a second okay so i'm going to say node api.js i can now say says sava is running at portrait300 i don't know okay so i think i'm i'm blocking this okay so i'm going to go to por3300 and let's see http localhost at port3300 slash users and you can see that it returns an empty array okay to check that everything is working i'm going to go to postgresql it seems this is not saved let's see so let me see um for some reason this seems it's not saved so let me see okay so for some reason it didn't add any data so let's just uh insert a new record it seems i'm blocking this um okay i'm simply going to swap the displays like this okay think it's better this way all right so i'm i'm simply going to add data to this table so i'm going to right click just cancel this so it's going to be right click uh and say um scripts is going to be inside script okay so basically i'm going to add some items kinds on um actually the first one is id of one and the last one should be location all right so we have a script so i'm going to just run it by clicking here so it's run successfully so now if i go back to this place and refresh you can see that we have some data coming back you can see that's the data we started okay so we have the first part of our api working perfectly so i like to swap back sorry give me one second i like to swap back this display like this so i'm going to go back here the next thing we are going to write is a query that gets a single user by the id it also works in the same way so again i'm going to uh write i'm going to copy and paste because it will save us a lot of time so i'm going to paste so in this case take note of how we specify the id parameter in the url so if you look at the url they have slash user slash column id so that means that the id coming here is you have where id is equal to you have request the parameter id and specified using dollar because uh this is template literal because i use backtick so enclose everything so we have the id here so you use requested parameter id and you get your id now i'm not going to say client.connect again i think this is error so please remove it i'm going to update what i have on my website so this is about uh fetching one single id and if you wouldn't mind we are also going to test it so i'm going to stop my server and then and then we run it once again so if we go to localhost and i refresh this is fine if i go to look for id of two it doesn't get anything from idea of two and i'm going to check for id of one and you can see that the gate by id of one so we've written uh get by id and the next thing we want to write is add a new user so there's a code for add new user and also in this case take note that we are now using ap app or app.post because before now for get by id and get all we use uh app dot skates so in this case we are using app.posts now it's basically the same thing however we are now getting not only we are not getting our parameters from the url but we are getting parameters from the request body okay so here we are saying constant user let me just increase the font yeah so we're having const user is equal to requested body so in this case request body contains the user we want to insert and then we run the same in south query basically the semi-soft query inserted inserts into users values you specify all these take some time to go through it if you don't understand it and where and then we run the query again and do client response dot signed so response assigned signs a response now in this case of insight we are not actually sending uh sorry let's just we are not actually sending any data but we are simply telling the user that they're in session was successful so basically i like always testing my application once it's once i'm doing it so i'm going to stop it and to stop and run this server so it's working now we cannot sign a request a post request via the browser so we need to have postman now what is postman so let me just show you postman rest client so you need postman to actually uh make requests so you need to download it and install it that's all another another tool you can also use is advanced rest client either of these two so this is an add-on to google chrome simply launch it and it's also okay for me i'm using postman i also have advanced rest client arc but i prefer postman all right so we have our postman i think he's opening up let me just drag the window on here okay so you can see now that we have okay i already wrote the url right here so if i go to slash users you have an item already there in this case now what i'm going to do i'm going to create a new user i'm going to copy the let me just paste this one so now i'm going to create a new user with an id of two whose face name is um uh let's just call it nicos nichols mark mill and the the location is germany all right so we are creating a new user at this point we are trying to insert into the database so i'm going to sign this request okay we are doing a get request so you're going to change here to post okay so i'm going to click on send cannot read property undefined let's see undefined oh so i'm having arrow es dot body i think it should be req the body is going to be requested body sorry req dot body so i'm going to save everything i think this should solve the problem once and for all so let me just claim my screen and i'm going to all right so let me run the application okay so if i go back to postman we are posting new uh this item i'm going to send i can say it says insertion was successful i'm running out of time but i think i should complete it so the next thing we are going to do is to do uh put and then delete so i'm going to copy this so basically this works exactly the same way like post but in this case we are going to get the url the id from the url so i'm going to copy this and i'm going to uh use it right here so in this case we have slash user slash id and we also specify the body so i'm going to also use the delete at once so that we we just complete everything at once so i'm going to copy for the delete and also put it right here the complete code for this application is quite easy find it in my github repository and use it if you want if you have challenges please let me know in the comment box below or reach out to me from any of my social network profiles so now see that we only have two two uh files here connection.js and api.js that's all you need to be able to build an api perfectly working api connecting to pg our postgresql and after now i think we are going to do connecting to mysql meanwhile let's go ahead to test this api uh for delete and also update so i've started it and it's running on port 3300 so i in this case now if i say a gate i can get you can you can see we have two items so assuming we want to change number two we can now say users slash two and we go to port i'm going to change the last name from mcmills to let's say they can use i don't know i mix up this thing sometimes and location now should be hungary so if i send you can see that update was successful and if i do a get to everything we can see that we have two items right here permit me to just make one post request and uh the id is gonna be three a friend of mine is called mile mila i'm going to just use okay so stays in hungary as well so i'm going to do a post and we have three items at this point we have three items available let me now do a get to everything and at this time i would like to delete the the second one the item two so i'm going to say slash user slash two and do delete and send deletion was successful and if i do get to all i can now have only two items left so we build a complete api to do get post put and delete and also get by id and i hope you find it very easy again i make this update on my website kind on the genius.com so quickly always if you miss out something go to timezonethegenius.com and you will always find uh me right here and you can open and check for the latest content so if you open you can always see the latest content by going down you can also find free materials maybe lessons maybe some books videos so this is what i wrote recently about this very one so i'm going to stop here the next thing we are going to now do in the next lesson we are going to build the same thing with mysql after that we now do the ui using angular angular 11 to be able to do this in the ui you know entity in a form and then post by clicking a button so i'm going to stop here if this has been informative for you please subscribe to my channel right now leave me a comment and tell me this was okay for you or you learned something from here and also if you have challenges do let me know i'm always there for you and i remain kind of the genius
Info
Channel: Kindson The Tech Pro
Views: 54,348
Rating: undefined out of 5
Keywords: Node.js, NodeJS, REST API, API, Tutorial, Step by Step, Postman, Express, body-parser, PGAdmin, VSCode
Id: HO5iiDaZO2E
Channel Id: undefined
Length: 28min 16sec (1696 seconds)
Published: Sat May 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.