NodeJS: Building Your Own Backend & Rest APIs using Express & MongoDB Atlas In Just 40 mins.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back in this video we will learn how to create our own back-end using node.js so we will start writing our API is in node.js the reason I chose node.js because of JavaScript and because I you know that I make mostly tutorials on flutter so flutter uses Dart and you know dot does not have any good framework for server-side work or to write your back-end or there are couple of them but you cannot rely more on them and you can stuck somewhere in the future so that's why I thought about creating like there were a lot of requests from you guys that I should make come back in tutorials so now I will start with node and like god you just need a basic understanding of JavaScript and once you do that then you can start with node and also try to explain things because it's very much similar to how dart works or dot is also inspired from JavaScript so yes there are a lot of things which will be similar so now first of all you need to install the node.js so you have to go to node GS dot o-r-g from here you can install this version I will say star twelve point sixteen point two which is the IITs version and once you install it it will come up it will come with so if in your powershell if you will search node or - - for then not search but right so twelve point sixteen point two it will also come with something called NPM so NPM version if we check so it's six point fourteen point four so NPM is basically node package manager so node package manager is similar to what in dart we have pub so it is basically a place way from where you can fetch all the packages or libraries so this is what we needed so now we will start our journey of node.js back-end development so first of all I'll I am already in my node dev folder so I'll create one directory mkdir and I can name it north tutorial and once I do that I have one folder now node tutorial and I can go inside node tutorial and then I will open visual studio code and now I am inside Visual Studio core so first of all I have why what I want is packaged or Jason and to use package dot Jason basically you can use NPM to create it so you can say NPM in it and it will create it will ask few questions and it will create a your package dot JSON file but I just want to skip those questions so I can just say NPM in it that's this yes which means I say yes to all these things and it will give me some default information so if I open package dot Jason then you can see I have name version description main script keywords author license and all that stuff now what I want to do I will install Express to write API is easily so I can say NPM install Express and basically what it will do it will actually install Express in this application and if you just say - G somewhere then it will it will mean that it will install it globally so we don't want it talked to install globally we just want to install it locally for this project and now you can see we have node modules package locked ortho JSON file now I will create a one file which we will use to work and that will be app dot GS inside this file first of all we want to import our Express library which we just installed so I can say Const and why Const because you could say war but then es lint if you are using is linear it will give you warning that you know you should use Const a and you can say Express so we have expressed no and now we need the application from Express so basically an object so we can say Const app and we can use like this and now we have the access to this now first API which we will write would be on the root address which will be app dot let's say get so it will be a get request and let's say the slash is how we can go to that URL and then it also gives us two things one is request second is response and this is how we write syntax in JavaScript so if you don't know about JavaScript then you probably need to understand JavaScript first now we can here say just console dot log and let's say first request so rather than sending any requests it will just print this sort to our console and then guys we have to listen to this particular server on a certain port so we can say app dot listen on port 3000 so this this will work but when it is listening I just also want to print that on my console that da you know and now I'm sure that it is listening so I can say console dot log and I can say listening to listening to 3000 okay so this is what we can do now how to run our server we can just say node app dot J's and you can see listening to 3000 here now we can go to our browser and we can just say let's say localhost 3000 and it will show us here in the console first request it will not do anything so rather than doing console dot log let me just send rest dot sense so I will use rest dots and let's say first request we are not printing it to console and once we do that now if I just go and refresh it let's say so you will see that nothing happens it is still showing in the console firstly because the reason is that we need to restart our so and we have to try it again now this time you can see first request and we don't have anything in the console so every time you do something let's say if I add or after first request if I add two exclamation signs here then also if you see if i refresh nothing will appear here because we need to again restart over so on to fix that what we can do we can install another package which we can say npm install node mon-sol Nordmann is basically another library which can help you in you know watching all these changes and it will restart your server by itself so for that what you have to do you have to go to your packages and inside script you can say let's say something like start and to start it what we can say let's say we say Nordmann and the name of the file so that is AB dot J s and yeah we should be good to go now rather than running and for note app dot t-- s we will say npm start and it will start our server although we can see that it is giving me uh some error that you know this app is already listening some app is already listening to port 3000 because we actually created a new terminal here on the previous terminal we haven't stopped our server so let's stop it first and now we can say npm start and now it is listening on 3000 as you can see and now if i will refresh it you will see this two exclamation but now let's say if i change it to let's say three explanations let me just write it then also you will see three explanation this time so why it is crashing let me just see let's stop this one first of all okay so we stopped one of them and now let's restart once and yes now it's listening three exclamation let's change it to two and you will if we will refresh you will see two now let's add few more five or six and now if you will refresh five or six so this is how it works now this is this is a normal or get request let's say we can have a good example for that and let's say rather than slash we can say slash users and here we can say here are your users and now this looks good and now let's try rather than hitting slash we can say this time slash users and you can see it shows us here are your users so this is our looking fine but we cannot see any user here right so for that when you are sending or some response let's try to have some users so let's we can say let users and we can have some users let's say one is power and self second one is let's say some guys I'm random guy super guy something like that then we can have got a guy anything like that case and then let's say max something so now rather than sending this hair or your user we can directly send users here and now if I go back and refresh you can see we have this user so we have this list of users but I think the better idea would be not directly send users but also say what it is so we can use this JSON format and we can say users are users ok and this time if I go and refresh it you can see users and all that now what is the status code and all these information if you want to check then you can install something called postman or if you are using vs code then there is something called rest client so if you'll go to extension you can search rest client and here you can see this rest client you can install this package and then inside here you can probably or somewhere else wherever you want you can create a file let's say rest dot HTTP the extension should be HTTP but not name of the file can be anything and here I can say I want to do a get request and that should be on localhost 3000 slash users ok and now if I press on this button get requests you can see we got HTTP status as 200 ok and this is the data which we got users power and super graph I max all these things so this works really well now let's say if I want to do a post request then also I think it should be pretty easy I can say app dot post and let's say if I want to say create let's say we can do that create user and for the simplicity also let's install postman as well so that you can get the idea how postman works so download the app and though we can say download by the time it will download itself it should be very fast though by the time it downloads we will go back and we will write our API create user let's say um and then it will also give us the same options so request comma response okay so this is what we got right and then we can just console.log whatever request we are getting okay so that we can see what we are actually getting in this case and what actually happened with this let me check if my internet is working yes it is working okay now it is downloading so once postman is with us or maybe you can use this off rest tortoise TT p.m. you can write your you know normally post requests but that will be some time taking thing and this is just a shortcut but I just also want you to learn how to use postman so that's why I'm installing it and by the time I let me just give you an idea what is happening here so similarly to app dot paul post you can have app dot delete app thought patch so all these options are here and then in case you want to use some middle layer so you can say let's say app dot use okay inside that maybe you can have some middleware so let's say I have function here custom milk we're something like that and sigh here I can say console dot log and let's try with something like welcome to my middle where something like this and this time I will say here customer aware okay this is looking good now if I click here send request then you will see welcome to my middleware as well right so you get the users and okay you have to cancel this as well right because it actually gives you many things as well if you will go inside use I don't think we can directly go here okay so I'll explain this how that works okay this should be function actually function okay oh that was I think yes this is fine and guys this is once it is done what you can do basically now our postman is installed as well it is asking me to create an account I think I already have an account let's sign in with Google and I can just say I am tech world at gmail.com okay good so now we are here and now we will also try to do create request so every time we will do that we will see something like okay let's try our first API this time it will be post and here so first of all let's try to use get here so get and we can say localhost 3000 slash users something like that let's send it and yeah welcome to my middle wire but yeah the problem is that once you use this kind of middleware you have to also say when to go next so it will give you something like this okay and then after this you could say next let's try that I'm not sure if I am writing it in a correct way but let's try this time send and you can see users so basically middleware is something which you can like before making the request so once you send this it will first go to middleware and it will do some operation and then it will go to your API so this can be really useful once you try to like validate your user or maybe you want to check something you want so there are a lot of middle wires you want to convert your request to something else so yeah those are the things which it can be used so as of now I'm not going to use this middleware I just wanted to give you some idea now let's try to create our user so rather than get we will say post and this time I will say create user okay and yeah this is fine and it should be like you know you should use underscore wherever possible it's a good way of doing thing like create the - user will also work but then this is a better way and in body I can provide some body it's let's say JSON and the JSON would be something like let's say name is power one so and let's say age so this is the user which we want to create let's say 26 okay so now if I do a send request then you will see it is sending some requests are we all ready using okay something happen so you can see in the console dot log nothing is because we are not sending any response back but it actually printed a lot of details for us right and it's if you just see here that what it is sending us so it has a lot of details so it starts with okay I think it's not readable for you so I'm not gonna go into detail but what I do rather than this I can just say request dot parents so it will give me all the parameters and then I can send them response that created your user and rather than these presses I can use this take and I can say what is the user so let's say if it is successful then we can this say request dot name param SOI parents dot name so I'm assuming that they are sending name so now if you go back here what server is still running that is fine now if I try to do the same thing here send then you can see created your user undefined so why undefined and okay request dot something something it is saying okay so something is not working for sure and it is not able to read any data as well so what it is saying that inside this particular when when you are hitting this URL they're actually not sending any kind of data and that is interesting because we are actually sending some data in the body and what where we are finding it in the parents so that's the wrong place to find we should find in the body and that's where it should be parents we can use forget requests now if I try it so you can see we got some kind of error okay and if we see here cannot read property name of undefined okay so when we say that let's remove this and now let's try this created your user that is fine and this is saying undefined yet because it's still not able to get the body out of it so body is in term it is in jacent form and it's just not working for some reason so what we could do here in this case this body is coming as blank so let me just clear this console once no okay okay let's start our server again because we are doing something wrong here okay this is now started again um it's showing that you know your request does not have body any kind of body and it should have like name should be there at least because name is with us if I send it I get error that you know we cannot read name from here and maybe the reason is that would be like this it's actually yeah the reason is that it cannot understand this Jason okay so this is this is something which I forgot to tell you so once you want to pass this Jason so basically what you have to do you have to use something called let's say app dot use so that's why I actually explain your middleware so we will use something called Express dot Jason so what it will do it will actually convert this request to Jason and now if I will just try to send this you can see created your user and one is also printed here so now I can say here basically request dot body dot name and now if I make the same request again this time I I see created your user per one and yeah that's what we wanted right we are still getting two hundred okay and everything looks good so I just want to see if I can change the theme of this okay that that's not a very important thing now we will see how we can convert it you know how we can use the real database for database guys all it's just and this here and we will start our server again so for database we need another dependency another package and that would be we will use MongoDB so we want to install something called Mongoose so Mongoose is another package that means increase the font as well I think font is not good enough for control okay so console font size should be let's say before okay this is better okay if this was improved now we have installed or if you'll go to package dot this and you will see boom goes here although we can fake it move this node moon to dev dependents but that is fine okay mongoose is with us now what I want you to go is like how you can go to this MongoDB Atlas so this is a service which you can use so that you don't have to install MongoDB locally on your machine and here first of all you have to just sign up and then you have to create one user and that will be very easy just up just follow the like you know their guide and then you can just create a new cluster and it will ask you like you know what is the reason which you want to use or what is the provider which you want to use let's say I would say AWS although for free tire it will give me a free type cluster and free to your cluster means that there will be some limitations and all that and now we are just you know learning for our like for our like development purpose or for our just learning purpose we are using it and now because I have already created one cluster so that's why it is showing me that you know this is the charge which you have to give so I'm just gonna say cancel but you can just create like use your nearest or let's say if I say Mumbai then now free tier is available so I can choose the nearest server and once I'm done with that I can go back to my clusters so if I go here to my clusters I should be able to go back that is the question right now I don't know why I cannot go back okay now let's open it again so this is my cluster guys and once you create it then you will see there is an option called connect once you click here there are three options connected with the MU she'll connect your application and connect using MongoDB compass you can use either of them but right now the best way for us to connect is connect your application and though I will choose node GS driver version 3.0 later and this is my connection string so once you create your user actually it will give you some username and password so let me just copy it first and what I can do here in the app tortillas before listening to this I can I could say something like once Mongoose and require Mongoose so once Mongoose is with me I can actually use Mongoose here to say Mongoose dot you know dB okay Mongoose dot connect okay and now we have to give the URL here so something like this and then we can say request comma response inside that we can say console dot log and I can print something let's say I can say connected to the database okay so this is what we can do here and we have to replace our password here guys because okay yes so this replace this password with the password of the user admin is actually my username so that's why it is showing admin now if I run my server then you will see that it is connected to the database although this is a wrong password so let me just say like admin 1 2 3 4 that should be the right password ok it's still connected to the database which means so you know if this is working fine it is also giving me some deprecation warning let me just fix that so after that I can just use use unified topology and now it looks good another there is another warning use new URL parser so I can just add that as well ok now everything is fine connected to the database but you know someone can access your code and can easily access this so user name password and this entire string so for that what we can do we can install one more package here which we will call NPM install dot E and V so dot e in view is basically dot environment which can help us for these kind of things what it will be helpful apart from that is that here we can create a folder file and we can say dot E and V and inside here we can have our connection string so we can set dB Nexen string and we will remove it from you know even if you are putting it to github then also we can remove it so we can copy this from here and paste it here and guys once you are done we can also specify our port here let's say for example so now we have DB connection string and now I can just directly use here DB connection string but it does not know that right what it is so for that or you have to first of all import so we can directly say require and we can say dot environment slash config so once we do that now it should be able to understand this thing which is a DB connection string and you can see we are getting app crashed app crashed so let's sort turbinate it first and then we after restart our server and now okay this is saying app crashed DB connection string it cannot find okay reason can be what can be the reason because this is not defined okay so I think if you go to dot and wire man package so let's say dot and one and NPM and you can open these packages and you can see how to use them okay oh so we didn't specify that so we need to access process first and then we can say environment and then we can give the connection string now if we go here okay connect it to the database so everything looks fine nine know now when I'm saying that you know I have to create the user and I want to like create my user in the database so what we can do for that guys we can create another folder here let's say we call it model inside model we can have let's say user dot Jas and inside user dot J's we can write our schema for you know for creating users so we can say something like what what we can say so let's say Const bomb goes okay and we can say require Mongoose so once we have Mongoose we will also we can also go here and we can search for Mongoose and from there we can get the documentation how to work with MooMoo so you can see this is how using es6 you can do this and normally this is what we write and then this is what we write so this is how we can actually define some model as well guys so I'm gonna like have some model and then we can go ahead with this model so here what we can say so let's see what what they are saying so let's say we have user so we can say cons to user okay and then we can say something like new mom goes dot schema and inside this is schema we can have let's say name of type string and let's say age of type number so I think this is fine and then we can use sorry module dot exports and we can export this user and once we have this schema yeah this is this is the schema as you can see here as well I'll talk about other things like you know how to actually use this schema and all that stuff so you can see we have lot of options here we will definitely go in the details of how to use all of these things but right now it's not very important we can just try it few things and then we will see that you know how things are exactly and now you can see this is how we specify model so rather than this user we can say Mongoose dot model and then we have to give the name of the model so name can be user user user and then we need the schema so schema can be this user schema so this is how we actually specify our model so now we have the user model which we are now giving there now inside the app dot J's what we can do we can get the user user from require and I can say dot slash model slash user from this class I can get the user and then when we are creating this so make it a synchronous first so we will say a sync in JavaScript we actually write it here not in the end in dot it is different so I'm always confused with that okay and then we can just say try-catch so that we can catch the error if there is any catch error okay there is lot of stuff going on here but I am sure this will be good so send and inside that we can say message ever so yeah error case we have hindered now let's say we have to save our user as well so we can say oh wait user dot save right so something like that so let me just see how to work with that so we don't we know that we are getting a request dot body and request dot body should be something which we actually want to you is our user and we know something like user dot safe should be there but how to save it to this user class right so we can directly pass here I think so let's see I'm not save and new my model as it is saying so we could say something like ok so let's say Const my user okay and though we can say new user and here we can say request dot body so this is how we can get the body and rather than user we can say my user dot save ok I think it should work guys and let's let's remove this from here and we can just say my user ok so this is the response we can send now we will go back to postman this is Pawan and age 26 and this is the same API which we are which we were using earlier but this time it will go to the database so now I'll click on send let's see what happens and you can see it has created this ID by itself name / 1 age 26 and yes so we created our first you know this collection now if I go back to my MongoDB get cluster let's see let's click on collections and it will retrieve list of databases and collection you can see in the test database which was the name Mike earlier gave it so you can create your database as well we have users right so users inside users we have power 126 so what I can do now let's change it to let's say Rohit and age to let's say 20 and let's try this and you can see we have row it with 20 and now if we refresh it again let's refresh then you can see we have in the users we have two users no one is per one and second one is Rohit and this is looking perfectly fine right and visualize your data okay I don't know how to activate charts not sure about that okay this is this is working pretty good right so we have our Pawan & Rohit here and now what I can do let's try to have a failure case let's select remove these two and that's all send a blank edition so now if I click send you can see we created it but I think because it has data it created it something it created let's refresh it so we have to handle this case as well again ok it created some blank entry because it does not have any data so let's delete it and now if I go back let's remove these curly braces as well and now let's try to hit it oh it's still creating so maybe what we are doing wrong here is that I think we are still saving it whatever body which we are getting so we haven't applied any kind of check right that you know if if there should be some length or if that should exist or it is required or not right so for that what you can do you can go back here and go to your muumuus and if you will go here you will see something like this that if we provide this kind of schema then maybe you know it can work so what I want to say here is that if I go to world or user dot J's rather than saying just a string we can make another clicks this kind of Jason where we can say type is string required is 12 to crew and then we can say if if some default values there then you can say default similarly for a hair I think let's say it's number and our required true and minimum let's say it has a minimum number length so let's say 2 is the minimum length let's say we are expecting only numbers from 10 to up to someone like 99 or whatever so this is this is the change which we have done now if I go back to here if I try to hit this then you will see that we got some error that a path age is required but it is required and all these kind of things so we have some errors guys and now our database is like if i refresh it here then you will see nothing actually appeared here apart from our two like these are the objects which I created earlier so let me just delete them so yes I think it should be good now okay so we are getting some background noises that yeah so now if I try to do it again there is another error and now if i refresh it then you will see we have only two entries no new entry so yeah we are good now let's try to add one more entry and let's say named as Batman and H as let's say 1000 something like that if I send it then you will see Batman is created with this age if i refresh it then i think it's good to go yeah it's it's working fine so that's it guys I hope that you enjoyed this video there is lot of things which we covered and in the next video I'll talk about more of the things if you are interested in let me know subscribe to the channel if you are not a subscriber already also let me know what term or arm things you want these kind of tutorials so thanks for watching and see in the next video bye-bye take care and keep learning
Info
Channel: Codepur
Views: 22,171
Rating: undefined out of 5
Keywords: node js tutorial, node js tutorial 2020, node js tutorial for beginners, express tutorial, express tutorial node js, express tutorial for beginners, node js mongodb, nodejs mongodb project, mongodb atlas tutorial, mongodb atlas nodejs, backend node js tutorial, rest api tutorial, node js rest api, node rest, node rest api, express rest api, node backend tutorial, node backend
Id: uk-iRSj1Ldg
Channel Id: undefined
Length: 40min 40sec (2440 seconds)
Published: Mon Apr 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.