NODEJS API for Flutter Developers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back to snippet coder and today we are going to learn about api development using node.js so first of all we will understand what is api so api means application programming interface which is a software intermediary that allows you application to talk to each other apis are very common things nowadays they are used literally everywhere on the website api serve as the connection between the database which store all the data and the front end with which the user interact to get access to the data so now what is rest rest which stands for representational state transfer is an architectural style for providing standard between the computer system on the web making it easier for system to communicate with each other rest apis are mostly used json as the preferred choice for transferring data as they are easy to understand and is readable so this is the diagram of rest api so here client is doing the cat or post or put or delete request that will be caused by api in the form of http request and server will return us that http response via api and return to the client as a json or xml or as a html so we have multiple rest operations so first one is a get get means to read the data the function of this operation is to retrieve the data from the database and present it to the user then we have the post post as the name suggests is used to post add new data to the database it's allow user to add new data to the database then we have put means to update a data already present in the database then we have the delete it is you to delete any existing data from that database so here the diagram like here the client so client is calling here get request and take example we are returning as a job post here and the job post is as example here and then we have that post request and that will add the new data of that job post to the api then we have the put request that will update a job post then we have the delete here and delete will be used to delete the job post so now come the main topic that is what is node.js so node.js is an open source and cross-platform javascript runtime to run javascript outside the web browser node.js is used to build scalable web window console application and web aps and hybrid application it is built on a chrome v8 engine javascript was meant for web browser only but using node.js we can use a javascript outside the web browser to build scalable application so now let's move to the code part to understand how that node.js is work and we will create a sample of the api to understand how we can create the api development using the node.js [Music] so first of all we have to download few applications so first one is a node.js we have to go to the nodejs.org and from there we have to download the application this one we have to download recommended for most user 14.18.0 per lts after download we have to install the application as i have already installed in my system so i am not going to install it again so second application we have to install is the mongodb and that we have to install for that community server and for that we have to go to the mongodb.com try slash download slash community and from there we have to go to the mongodb community server and here the version already selected as a current version 5.0.3 then we have the platform here if we are using the window you have to select a window if you are using your mac os we have to select your mac os and then we have to click the download and then accordingly we have to install the application so i have already installed the application so i am not going to install again in the system so now we have to go to our visual studio code to create our first application of node.js so now we are in our visual studio code and from here we will open that terminal so we have created one folder where we are going to create our api and all and the name of the folder is node.js hyphens class one and there we have to put one command here npm i need so what it will do it will initialize the node.js project in that folder so it will ask for the package name so we are already using that node.js hyphen class one then it is asking for the version the version will be same then it is asking for the description we can put any description here like snippet coder nodejs class one then we have that entry point and the entry point will by default will be index.js and we will click here enter then we have the test command so nothing is required here we will click here enter then also we can put the git repository link if there will be any then we have the keywords author then we have the license here then it is asking us is this okay then we have to press here yes so that project has been created here so now you can see here in the left hand side we are having that package.json so now we have to install the express and the express will be used for our server and all and we have to put the command here npm install express save so this will download and save the express in our system and also we'll add that express as a dependency in our package.json file so now we will click the enter here you can see here that node modules has been created here and here in our dependencies it's added that express so now we have to add one more package that will be node moon so what it will do it will allow us to do our development faster it will be just like in the flutter we have hot reload so it will be work in the same way so what it will do it will automatically refresh if there will be any changes in our code so in that case we don't need to stop and restart the server again and again it will automatically check for the changes and it will update that our api and all so here we have to put the command mpm install save dev because we have to use it only for the dependency of the developer dependency and we don't need in the main dependency so for that purpose we have to put here hyphen iphone save hyphen dev note mon and now we will click the enter now if you go to our package.json file you can see here the dev dependency has been added for that so now after installing our mongodb we have to add the package in our package.json also and for that we have to put here npm install mongoose hyphen iphone save so it will save in our package.json so here in the package.json you can see here in the dependency we are having two dependencies first one is express second is the mongoose so now we will create one file here and here we have new file index.js that will be for the javascript file then here we will write a couple of line of code constant express is equal to to require express so required will be work in the same way we in we do in our filter as import same way we have to put here required so we required that particular dependency in our page then here we have constant app is equal to express then we will add one line of code to see our api is working fine or not and here we will put app dot get request and it will be just like a dummy get request slash api request response and here we are sending the response res dot send api working so now we have to listen for that port and here we have app.listen here we have process dot environment dot port or 4000 function and here we have to put here console lock now listening for request so what it will do it will start our server in the local as for the port number 4000 so now our index.js file is ready so now here we have to put the command to run our api and here we have node on index so it will start our server now we have to go to the chrome browser to see is it working or not and here we will put localhost 4000 and if we click enter here we will get here cannot get that mess this api is working only with that endpoint of ap which we have mentioned in our code if i put here slash api you can see here ap is working so now if i go back to the code so here you can see here i have added that app.getrequest with slash api that is endpoint if a user put that endpoint then only it will start working so now our sample ap is working fine so now let's move further to have the implementation of that api development of grid operation of get request posts request put requests and a delete request and we are also going to use with our mongodb [Music] so now we will create one folder here and the name of the folder is routes inside that we will create one new file that is api.js so now we will modify our index.js file and there we have to add the mongoose constant mongos is equal to require mongoose and here we have to add our connection to the mongoose and here we have mongoose dot connect db colon slash slash localhost slash and here we have to put our database name and here we are putting our our data our data is the name of the model we will be creating our mongodb later in this video so now also we have to put here mongoose dot promise is equal to global promise and here we will add app.use express dot json so this will be added for the express parser to pass the data sent or received from the database for that purpose we have to put here app.use express json then here we have to remove this code because we have already done the testing part to test that api is working fine or not for now we are going to use our api from our api.js file this file so for that purpose we have to put here app.use slash api require here we will put dot slash routes slash api so this api file will be holding all over for operation that is get post put and delete and also we can do our error handling also with that our middleware and all and here we can also put app.use function error request response next response thought status 4 to 2 dot send error error dot message so in case that status is 4 2 2 then we are just sending the error message as the error dot message so now we will modify our api.js file to have our four operation of grid and here we are constant express is equal to require express then we have a constant a route is equal to express express dot router and this will be used for creating the router using our express router so now we will create here get request a router dot get slash employee and this will be our endpoint function request response response dot send and here we are just sending our empty empty data of type get because once we are getting over all that credit operation working fine in the apa then after that we will implement the mongoose db and all same way we are going to do for our post request also a router dot post slash employee function request a response and say we are just sending requests then type post and then we have name and this will be we are getting from our request dot body dot name whatever name we are going to provide in our post request so now let's test the api is working fine or not for testing the api i'm using the plugin for our in the video studio code that is thunder client and here we are i'm going to create a new request and here we have localhost slash and here i have to put the port number 4000 slash apa slash employee and here i am going to do the get request i'm i'm clicking here send here i'm getting the error here seem like there is an error in our index file now let's go to the index.js file so here we have forgot one thing we have to export the module also and here we have module dot exports is equal to router so here another issue i have seen here i have to put the brackets here now i will click the save here so there is no error anymore so now i will go back to the request here and if i again click on the send you can see here we are getting the get request result that is type cat because we are passing there as a dummy data as a request dot send type we are passing this send and also we can change the type send request so now if i again click on the send you can see here we are getting get request in the same way if i click here post and from the body if i pass some data here and from here i will pass name raman if i click on the send request you can see here we are getting here type post name as a raman because in the api also we are just passing that response as a type and then we are passing at the name and the name we are getting from that body which we are passing from the api request so our thread operation working fine for the get to pause record in the same way we can do for our put and delete also and here we have put employee slash column id and that will be the id of that particular employee and here in the response send we are passing here put and then we have that delete request in the same way we have to pass here slash column id and here we have delete now let's test that put and delete request also in the tender client and here we have put request and if i clicking on the send here we are getting the error here because we have not passed any employee id and all i have to put a slash one and now i if i click on the send you can see here we are getting the status has a 200 okay and the type we are getting here put request same way we can do for the delete also and here if i click on assign i'm getting here type delete so now let's move further to have the implementation of the mongodb with this all the cred operation [Music] so now we are going to build that our database schema and model for that we have to create one folder here the name of the folder is models inside that we will create one file that is employee.js file so here we required our mongos and here we will put constraint mongos is equal to required mongoose then we have constant schema is equal to mongos dot schema then we will create our employee schema constant employee schema is equal to new of schema and here we required name type will be string then we required employee number type will be number then here we have constant employee is equal to mongos dot model and here we will set employee and here we will set our employee schema then from here we will do module export is equal to employee so what we did we have created one model folder inside that we have created one employee.js file and there we have constant for mongoose and that we required the mongoose library then we have created the schema with our mongoose.schema then we have created that employee schema and here we have created a schema with the name and the employee number then we have the constant for the employee by calling here mongoose.model and there we have just passing our employee schema and here the name of that model that is employee then here we are just exporting our employee so now we will go to our api.js file and here we have to call constant employee is equal to required models slash employee so here we will update our get request and here we have calling here employee dot find and here we are not putting any filter here instead of putting any filter we are just calling a fine and it will fetch all the recorder of the employees in our database but in case we required any filtration and all that we can put here but that part we are not covering in this video we will cover those part in our upcoming series and all then here we have then function employees and from here we will send the response send employees and here also we are calling a catch next so what it will do by using our cache next function it will help to catch any error and pass it to our next middleware for the error handling and middleware in our case which we have defined in our index.js file here then we have our post request and here we will call employees employee dot create and you will pass a request dot body then here we have then function employees response dot send employee then here we will call catch next so by calling a response dot send whatever that data created in our server that will be written as a response so now we will have our put request for that using we have to call here find one and update function to find the corresponding entry in the database using the id we pass in the ur endpoint this one we then find the same employee with the help of its id and we will return the updated employee as a response here we have employee dot find one and update and here we will pass id request dot parameters dot id and here we will pass request dot body for all the parameters which we want to update for that particular employee then function employee and here we will find employee dot find one here we will pass our id request dot params dot id then employee response dot send employee then we have our last operation that is delete operation and for that we will call here employee dot find one and delete here we will pass underscore id request dot params dot id then function employee response dot send employee so now let's test application after that i will explain step by step what we did here so first of all we will call our get request to see what data we are getting so here we are getting some error here let's see what the issue is so the issue is in the epa.js js file so here we forget to put here next and here also here also and this one also so now let's see so here the error is not coming so again click on that request so here we are getting the status code 200 okay and the response we are getting empty because we don't have any data so now let's do our post request to add some data here so now we will go to our body and from here we will pass the data so let's see in the employee.js what data we have to pass the first one is a name the name we are passing here roman then we have our employee number we are passing here one so now if i click on the send request so here we are getting status 200 also we are getting the entity which is creating in the database that is name then we have the employee number and this is the id which is generated from our database and this is the unique id now if i do any get request from here you can see here we are getting the array of the data so again if i go to the post request and i will post another record and here i will put pre if i click on the send request now you can see here new database has been added so now if i click on the get request and if i click on the send you can see here we are getting two records this one is a roman then we have our read record so now we will do our put request also to modify some data if i put a put so now you can notice here we are having employee number as a one for both so in the put request i will change the employee number i will put here five so now if i click on the send request but it will not work because i have to pass the id also and for passing the id i have to copy this id and i have to put here after slash this is the id so now if i click on a send request so now you can see here data has been modified and here i'm getting the employee number as a five so now if i do any get request here if i click on the send i'm getting in not found because i have to remove this id and if i click on a send request you can see here we are getting two data first one is for the raman and the employee number is one here and second one is the breathe and the employee number is 5 because we have modified our employee number by using our put request so now let's do the last api that is delete fund and from here we will remove that data for the raman and i will copy this id put your slash and id here and from here i if i change it to delete now if i click on a signed request i'm getting here some error so now let's see what the issue is i have to go to the ipa.js file so here we forgot to put the bracket here so now if i save here and i will go again to the request and we'll click again on the send button now it is working fine i'm getting it 200 okay status that means ap is working fine for the delete also so now if i do any get request to see here how many results are coming from the database and from here i have to remove the id so now you can see here we are getting only one record because first record has been deleted by using the delete api and all so let's back to the api.js files so let me explain what we did here so here we have our get request for the employee api and here what we are doing we are just calling our employee model and that is the mongoose model and from here i am calling here employee dot find and it will find all the record i am not putting any filter here but we can put a filter like if we want to search any particular employee we can put on the base of the id also that we can do so after that after getting the data we are just sending the response to our response dot sign and here i am passing all the employees whatever we are getting from our tv then here we are doing our try catchy and all to handle our error unknown then we have our router dot post request and here we have employee dot create and here we are passing request dot body and that request dot body will be our this body and from here we will pass name and employee number then after that whatever the data we are getting for that particular employee which is created in our database that we are response back to our user in the api then we have a put request here we are just calling here employee then we have the slash employee id and that employee id we are just fetching here in our parameters and we are calling here find one and update and on the basis of that employee that id of that our mongodb model we are passing here and also we are just passing a dot body on the basis of this id it will update that particular record of that data we are passing here so then we are just calling a find one on the basis of the id we are passing here so whatever the data is in our database that will be response back to the user then we have our last api that is delete api here also we are passing the id here then we are calling it find one and delete and here we are passing id from our parameters id then whatever the response we are getting in our employee model that we are passing back to the user when our response are sent then we have our module dot export and we are just exporting all these routers so that's all in this video i hope you liked the video don't forget to subscribe to the channel like comment share so very soon we are going to start our e-commerce series with our node.js and flutter application so for that purpose i am just creating this crash course for the node.js for those people who don't know anything about the nodejs so they can easily understand the series and they can start learning from our node just series and a flutter series so i hope you liked the video don't forget to subscribe to the channel like comment share i will come back soon with another awesome videos thank you all [Music]
Info
Channel: Snippet Coder
Views: 2,625
Rating: undefined out of 5
Keywords: how to create api in node.js, how to create api for flutter, how to write api in node.js, how to create rest api in nodejs, how to create rest api in node js with mongodb, nodejs, node tutorial, node js tutorial, node.js tutorial, nodejs tutorial, learn node.js, learn nodejs, node js, node.js, nodejs beginners, node, what is node.js, what is nodejs, node js tutorial for beginners, server side, mean stack, web development, javascript, beginner, backend, node js crash course
Id: _hv-4knConI
Channel Id: undefined
Length: 24min 56sec (1496 seconds)
Published: Mon Oct 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.