#1 Explanation about how api works || What is an api || How to create api in Node JS | Learn node JS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to the debug arena in this video i am going to show you how can we create an api in node.js if you are a complete beginner and don't know anything about node.js then also you can watch this video and you will completely understand everything about api what is an api how to create an api and also we are going to handle all the type of requests and errors that our api might face so without any delay let's get started so the first thing let's see what is an api an api is the application programming interface that is it is a type of middleware that allows two application to talk to each other let me simplify it with the help of an example suppose this is the login form in our website and this is the database which store our users data now we want whenever user entered whenever user enter his email and password and click on submit there should be a something which verify that whether user existing exist or not in our database so for that we need an api so suppose this is an api now when user will enter email and password and click on submit this website will send a request to an api please go and check whether this user exists in database or not then api will send requests to the database and to that database will send response back to an api that yes user exists and that response will be send and that response will be received by our website so in this way all this login authentication will happen behind the scene with the help of an api now let me show you how can you make an api so this is the folder where i am going to write code i hope you already installed node.js now first to run node.js the first thing is to generate a package.json file we can generate it by npm init we just give everything details and click on enter yes now my package.json is generated you can see here also to make an api we will require to install some packages i am using express.js like you have to know that no reason environment to run all this and express is a framework of a node that will that we will use to make an api and we are also going to install node 1 so that we can run our server perfectly okay everything is installed now the next step is create a new folder named app.js beside that first we will import our express require we will import our express let me give it name express okay [Music] and now let's initialize it to the app express [Music] yeah i have just uh initialized express [Music] now let me just use lesson to see whether my server has started or not here i am using callback function here i am consoling server started so what i am doing here i am for i had first imported express and then initialize it to app then called a listen which is a function of express to know that whether your node server has started or not so now we will start our server and it will run on this port 5000 like this is the port number and if our server will be successfully started then it will control server started before that we have to make changes here like inside test we have to like start here here you have to write node and the file which you have to start and just save it now open your file in cmd [Music] and then nodemon app you can see here we are getting that our server has started now our first step is done we had successfully installed express and nodemon and also started our server now let's make an api for making an api there are many methods like get post patch delete we will not see all of this method in this video we are just going to make an basic api through which we can send request and risk and receiver response so that we can actually figure out how api works in this video i am going to use the post method to make an api so let me tell you a little difference between get and post in that you can only receive a response from a server but in post you can send a resp request and then receive a response so let me make an api so this is my api app.post inside it first i will give the name of my api that is post and after that make an async call which will be my function so this is the basic structure of an api now let's understand this thing that when user will enter his email and password and clicks on submit then that data should be received by an api and then how can we receive that i had told you that when user click on submit that website sends a request to my api so we can get it to the request so let me console request dot body we receive all that data entered by user in request.body let me say we this let me save this and i am going to use postman to make the api calls so this is my postman first you have to select the post method because your method is a post then you have to enter the url currently my server is running on localhost 5000 so i will write this and then post so this is my uh address like this is the name of the api now you have to set the headers content type we are going to uh to receive in json format that's why i am going to write this and now just in body select raw just send something like i am sending here data others so this is my body uh that is you can consider it like as email and password now when i send it you can see that cannot post i made some mistake let me say it again okay now you can see that now you can see that in console i am getting undefined and i think this undefined is for request dot body okay okay i'm saying actually i'm sending our data in the json format so we have to use app dot use express dot json like this now when i hit save and stop this and send this again okay once again send again and now you can see that i am receiving what i am sending through my body so in this way you can receive the data sended by the user to your api now let's work on this now you can see that i am receiving whatever data is sended to the body now let's work on this so first this structure that like i am sending the data this is known as restructuring now the response will be stored in this data now what i am doing here i am just writing some condition if data is equal equals to others then now our server will send us the response till then we are just consoling it but now our server will send response you can see here also when i click on send now this request never ends it's it keeps on loading but we get the control value because we had never uh because we have never received a response so now let's receive a response like if data is equal equals to others this will say it will send that response dot send this is a function and it will just send status is equals to ok now when i hit save go here cancel this request and then send it again okay let me start the server again go here send it again and now you can see i'm getting status as okay now we are also getting response this is the response from our server so in this way you can work with request and response now let's do other things i hope you are getting the things very correctly like we had made an api we had send a data or a request to an api and also now we are receiving the api and now our api is finished but let me show you the best practice to write an api so that you can handle any type of error so what does it mean now here i have given only that if data is others then so then my api will send me a response what if this uh what if this data is something else then also he should respond to us not only this sometime there might be a condition where that there is an issue in a connectivity then also like you your api has been called but your api has never reached to a function because of some variable mistake or some network issue then also then also your api should be able to return something so that user can know what is the problem behind it now let's handle all this the best practice to handle any error is to use try and catch block inside catch block just write just return the response and set status to error and run this code in try block create an else and inside here like response dot send and i can see here that status user not found now let's save this and go to your api send a request and you can see when i'm sending others it is saying okay and when i am just changing the name and sending this you can see here that we are receiving status is equal to user not found and if suppose this data like i had changed the variable name now when now when the function will reach to this line it will get it will give error and which error we don't know so it will directly go to the cache block and user can know that there isn't some error so now when i click on send you can see your error and instead of error you can just write something went wrong try again [Music] so in this way you can handle all the types of error so this is the best practice to make an api so that it can handle any type of error now you can see that i had used try and get so if it receives any type of error it will just response this now let's visualize this api in our scenario like we have a react application we have a login form user enters email and password user click on button there we will call the fetch api that we will call the fetch function you can see here this is a basic function in the url we will write our url localhost 5000 post our method is post our headers are content type application application json the same header we have given in our postman inside body we will pass our data inside body we will pass our this so now this data will be received by our api you can see here it will process on this and it will send a response so whatever response like here currently it is giving response as status something went wrong so you we will receive that response in here like in data we will get that response so how can we access that data dot status is equal equals to okay so if data status is equal equals to okay so alert we can show a lot like you had successfully login so in this way we can properly make our api use our api to connect between our react and the database and also we can make any number of api for anything and use the same api again and again in the next video i will explain you how can we make an api call using a fetch or xcos or how can we integrate a mongodb in our node.js application all these things we will cover in our upcoming videos that's it for this video we will learn more about mongodb node.js and react in our upcoming videos and please let me know in comments whether you had learned to make an api or not thank you for watching the video and if you found this video helpful then please like share and subscribe thank you [Music] you
Info
Channel: The Debug Arena
Views: 29,401
Rating: undefined out of 5
Keywords: How to create an api in node JS, How to use Postman, Learn Node JS, How api works, api, everything about api, learn backend, use api, how api works, create basic api in node js, react native, web development, react js, use api in react js, react, learn react, behind the scenes of an api, know about api, how backend works
Id: ysF8iy9Kmqw
Channel Id: undefined
Length: 15min 28sec (928 seconds)
Published: Mon Jun 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.