 Hello and welcome to this smart India hackathon 2019 designing digital training program. My name is Archana Gattigar and I work with persistent systems as a technical trainer. We've been discussing about the topic of Node.js and in the last session I talked to you about the concepts of server-side JavaScript and what is really behind Node.js, what is single-threaded architecture and so on. In this session I'll be mainly demonstrating on the different node APIs, the different ways of working with them like what is asynchronous execution versus synchronous execution. We'll also see a complete example on how you can build a REST API using the Express framework. So let's begin. The agenda for this session is the file system API which includes asynchronous calls and synchronous calls. We'll see what are the differences between the two types. We'll see how to write a web server in Node, specifically how to work with the Express server and lastly we'll see how we can connect to MongoDB from Node.js and get some data. Let's begin with the FS module. The FS module contains a lot of methods and some of them here are listed like FS.MakeDirectory, FS.Stats, FS.ReadFile, Redirectory and so on. Now how do we make use of this asynchronous method is interesting. We need to add the required statement like we've already looked at in the previous session. We need to add the required statement for FS module followed by the method calls. So let's say I call the readFile method, we can provide a path to the file that you want to read and the second parameter is what is important. The second parameter denotes the callback function. So remember all IO operations and that includes the file IO are asynchronous in Node.js by default. So this will occur in a separate execution. It can be a separate thread or it can be in any other asynchronous manner but it will not be part of the main single thread that is executing. And hence this callback is required that means after file reading is complete this callback will be executed. So we need to define mandatorily this callback which signifies the next step to be done or the next piece of code to be executed after the file IO is complete. Now this callback let's have a look at this in detail has two parameters the error and the data. So they are also referred to as error first callbacks. The first parameter always is if is the error object that means if some error has occurred then this object will be set with some properties that define what the error was and if not then the second object will be set that is the data that has been read from the file. So here we can then make use of or we can access the data from the file by referring to this variable here. Let's look at a demo. So let's write this code to read a simple file let's say FS equal to require FS module. So this is an inbuilt module provided by node. So we do not need to do any npm install or write any specific code we can directly write require statement and call the APIs. I will say FS.read file I have a sample file with me called as data.txt. So I define that if you have a different path you can also provide the entire path over here and the second parameter is the callback. So I define an anonymous function here as a callback. The first parameter is always error and second is the data. So we read the file and let me say if there is any error then print the error else we can print the data. Now to understand whether it is really happening in our synchronous way I am just going to add some console statements. Let's add that it's ends here I am going to run this file now node demo. So it's printing it in the binary. So we can give the file encoding here utf8 which is going to take care of this and print out the data that is present in this data.txt. So this goal of this documentation this stuff is getting printed here. But look at the statements of program call stack begins and program call stack ends. So although it was a very small file with having very just a small bit of data still it was executed in a synchronous manner that is outside the main call stack. So we can see here the two statements get printed and then sorry this one. So we can see these two get printed and then you have the file data getting displayed. So that clearly shows that file reading is occurring in the asynchronous manner. So all the methods which are present in the FS module they are by default asynchronous. So if you talk about readdir that is again it takes path it takes certain options and there is a callback. So we don't need to do anything specific in order to make them asynchronous by default all these are asynchronous. Let's look at another example let's see redirectry so I already have certain statements in place and I will say fs.readdir the current directory is what I want to read followed by again a callback because remember this is also asynchronous comma files will be returned back and I will display the list of files. Let's run this. So here again program starts ends and then we have the list of files in the current directory. So it's displaying files as well as directory. So mydir is a folder here that I have and that also is getting listed here as part of the current directory. So anything that takes a callback it simply denotes that this will occur in asynchronous faction. So there is another API that is file stats which takes the file name as the first parameter and it will display certain properties in the format of an object called a stats object. So there is stats.size it has properties to find out whether it's a file or whether it's a directory. So here I am running this stat on two things one is on this file and second is also on the directory to see what are the properties of the directory as well. So if we run this. So this is properties of mydir that is directory is true is it a file it says false size is 0 because it doesn't contain anything and properties of data.txt is 356 bytes and the other two flags. You can clearly check here that the properties of mydir are getting listed first that's because this got executed first and then we had the other one getting displayed. So there is no sequence that is getting maintained here because both are occurring in asynchronous fashion. So this is occurring independently and this is occurring independently. So this is a different style of execution that is asynchronous and we need to get familiar with this going ahead. Now when we look at the documentation then for every API that we see here that is make directory there is a corresponding synchronous version available. If we have makedir there is makedir sync if there is readdir there is readdir sync. Similarly for file reading there is another API read file sync. So what this means is for every asynchronous version there is a synchronous version available as well. So this is especially in case where we have where the requirement is that certain things need to happen in the synchronous way wherein there is a dependency and without that you cannot go ahead with the rest of the execution. So in such cases you might want to put certain blocks of data or certain blocks of code in these synchronous versions. So here is the call to read file sync which takes the first parameter of the file. Notice there is no callback because callbacks are only for asynchronous functions. It will have a return value just like any other function that we have executed. So the data read from the file will be returned back and hence we need to catch it in some variable. So let us look at a demo of this. So I will say fs.read file and use the second version the sync version and provide data.txt. I will give the file format and print the file data as this. So this will occur in sequence now. So we will have line by line execution, it will print this and then followed by the file io that will read it from the file until that the next execution will not continue and only when it returns we will have the remaining statements getting called. So I will say node so check out here same as we expected followed by the file data and then we have program ends. So that is the synchronous execution for you. Now we have seen two main concepts we have seen synchronous execution or synchronous execution and now let us look at another concept in node that is streams. So there are two ways in which we can read data by using the fs module. One we can load all the contents at once which is called as buffering and store it into memory or we have incremental load loading of data or reading of data which is called as streaming. This is a very popular concept of node wherein in the first case that is the APIs that we have seen so far is referring to this first method. It is going to read the file maybe in a synchronous way or in the synchronous way but all the data will be loaded into in memory data into the memory and that can be later accessed or sent to the user as a response or it can be further processed. But what happens here is data is read in chunks and that is why we call them as we call it as streaming. So it is like a stream of data free flow of bytes you can imagine it like a pipeline with you know bytes being flowing. So that is how we have the source and a destination which will have data you know being sent from one source to another destination. So this is the first type which we have already looked at read file which is going to take a callback. This is the buffering type and this is the second one wherein it is using streams. So you have the data source and in chunks data is going to be produced and consumed by some other target here. So the advantages of streams it is that it has a smaller memory footprint because smaller chunks of data are coming in and immediately being processed so not much memory is required. It has faster response time because the data keeps on flowing in you know in smaller chunks and you actually need not wait till the entire file is read and then you start processing. You can pipe the data so which is a very popular concept in Unix we can pipe directly from one stream and redirect it to another stream. So probably you can have a file stream that is reading from a file and immediately pipe it to the HTTP response and send it to the user as user and you can immediately send it back to the user. So these are some of the advantages. So streams are mainly two types we have readable streams and writable streams. So readable are mainly to read data so for that there is an API for readable streams and here is the code for that so let us look at this by having a demo. So again when we are making use of streams over the other methods it will still use the FS module so we need to add the require statement and here I will create a readable stream create read stream for the file data.txt. Now there is an important thing that occurs on this streams, streams emit events. So node is based on events just like how we use JavaScript in the browser and it makes use of different events like click events, keyboard events and mouse over events and accordingly we write handlers for those kind of events. Something similar occurs in node as well. Here also we have events and we actually write handlers for these events. So here when we are working with streams the streams also emit certain events. So when there is data available for reading it emits the data event. When the data is not available that means it has finished all reading from the stream it emits the end event. So we have a data event, we have an end event and so on. So streams mainly work in this fashion that is it is based on events. So here a list of events is provided here there is readable event, data event, end close error. So in case some error occurs while reading data from the stream then it will emit out the error event. So if you want to do error handling then you will need to handle this particular event called as error. There are the other methods which we can invoke on these readable streams. So let me write a handler for which event for the data event because right now we are interested in reading data from this file called as data.txt. So we add a handlers by using a method called as on, prestream.on and say pass the event and write the handler here. The handler will have the data available, we can say the chunk that is available. Remember that this is going to read data in the form of chunks. So not the entire data from the file will be available here. It will come in the form of small parts called as chunks of data. So I can print that out here. This chunk of data is this, I will just convert it into a string. We can set how much data that you want to read in the form of chunks. Like we can specify how many characters, how many bytes do you want to read and we can specify that with the help of a very simple property called as high water mark. We can say high water mark equal to like we have defined here and let me say 100. So let's run this file. So check out here we have next chunk. So it keeps on reading it in smaller parts. So basically it has read the entire file in four parts 1, 2, 3 and this is the last one 4. So this is helpful when we want some processing to be done as in how the file is being read. So we need not wait until the entire file is read into memory and then you start working or processing on it. So that is how we can make use of streams here which is an important feature in Node. Similar to readable streams there is also writable stream but before that we can write one more handler called as dot end that means handle the end event. This is mainly to ensure that we have finished reading the file and if certain things need to be taken care of after file reading is done for example you want to clear some buffers you want to reset some values then that all can be done on this event here that is the end event. What happens we have the data event being emitted every time there is data available so automatically the data event will be emitted internally on this object called as read stream and when there is no more data available it will emit out the end event and that is how we will know that file reading has been finished. So similarly you can handle the error event as well and do the error handling for this program. Similar to read stream we also have a writable stream here so it is writing to some file and in the form of smaller chunks of data we can write data to this file as well. Now to ensure that all the writes have been finished and to actually flush the buffer and do the final write to the file we can handle the finish event. So mainly streams work with events and event handling so the same we have it listed. So writable streams can be HTTP response it can be TCP sockets so you can read data from a file you can write it to a socket as well. So it is just a destination that you want to you know send the data through. So these are the events and these are the methods which are supported in writable streams. So we have already seen an example of this. Now let us move to an important use case of node that is how do we write a web server using Node.js and specifically how the express framework helps us to seamlessly do this. Now how does node work as a web server? So that is how we can picturize this we have a server that we are going to write. So let us do a quick comparison with the traditional web server that we have. So typically what happens is there is a thread pool which takes care of each of these requests. So we have one thread which is serving one request. So what this leads to is a situation where all the threads are busy serving the requests and there can be certain requests in the waiting mode. As soon as the thread gets free then it will start being processed. Now one of the reasons why the threads are taking some time to finish or taking some time to execute is because of the major issue of blocking IO. So maybe data is being read from the database or you are updating the database in this particular request and that can lead to a bottleneck. And this makes the thread wait and hence also making other requests wait because of lack of threads. Now what happens in node? In node all IO calls are asynchronous and along with that all HTTP calls are also asynchronous in Node.js. What do we mean by this? What it means is any request that comes in is also asynchronous. And there is no thread pool which is going to serve each of these requests. All the requests are going to be handled by the single thread that we already looked at earlier. It is also referred to as an event loop because it is like a loop that is executing all the requests one by one or all the events one by one. So it all the requests come to this single thread and if any of the requests are going to involve IO then we already know that it will be delegated internally to some asynchronous mechanisms that can include internal threads. So most of the operations are IO based it can be network IO, you are reading and writing to sockets or it can be database calls or some file related operations. So all these go in the non-blocking way that is they do not block the other operations in the flow. So that is where node is going to shine when there are when the scalability is an issue. So a quick comparison here which is thread based web server it holds a connection open until the complete request is processed and this often leads to a blocking situation and to scale up we often need additional copies that is you need multiple servers whereas in Node.js a single server is going to service all the requests and mainly with the model that it follows where IO is handled asynchronously. So let us build a web server using Node.js having seen the advantages that it has. So there are two ways in which we can build a web server. So one we can use the HTTP module which is an inbuilt module provided by the Node.js but it is not a very convenient module and it does not have very easy to use methods there is quite a bit of code that you would need to write and not a very easy to use module. And what is more popular is the express module which is the third party and available via NPM. People have been using express for quite a bit of years now and it is a very popular framework for building web applications. Internally it does use HTTP module at the core it does use it but it is more like a wrapper for us it is more like a framework for us which has easy to use blocks of code where we can quickly plug in. So let us look at how to use this express framework. Now what is express? Express is a routing and middleware web framework where what we mean by routing is deciding how an application is going to respond to the requests. So if the request is for the default application URL then you need to load index.html but if the request is for slash about or let us say slash contacts then you need to go and load the corresponding file. So deciding what to load based on the URLs is called as routing. An express has some easy to use methods which help us to write the different routes and route handlers for our application. So the steps to get started will be coming back to package.json in a while but before that we need to install the dependencies for express that is we need to install the package for express and get started with this block of code. So let us go ahead and do this. So the first step will be to install express. So npm install express the latest version can be used. So you will have to run this which will take a bit of like a few seconds and install all the required dependencies. It will all be placed inside node modules folder that you already looked at. So it will be placed here express along with some dependent modules which whichever it will have it will be added in the same folder. So I already have installed this so now directly start with the program. Let us start with adding a require statement for express. Adding a web server by using express is just 2 to 3 lines of code directly say new express app and say app.listen to some port number that you want to define. So by default it is going to listen to the local host URL but in case you want to specify a different one then you can always do it as the first parameter here. The second is this port number that we are defining. So it is going to run on local host 4000 port number. Now all HTTP calls are also asynchronous and that is the reason there is a call back here. So as soon as the listen function starts executing successfully we will have this call back getting executed. So we will simply say server started on port 4000 started and listening for connections on this port number. So writing a server in express is as simple as this. So let me run this. So here we have a process that is running on this 4000 and you can see it is waiting for new connections. But if I browse to the URL local host 4000 I will not really get any response. Why? That is because the server is doing nothing. We have not added any logic, we have not added any roots in this particular server script. So let me do that. Let me say app.get the default URL that is just simply slash will have a handler and this particular handler will have two objects. It will have the request object and it will have the response object. So similar to how you would write a servlet code which has access to the reference of HTTP request and reference of HTTP response something similar is what we have here. So I can do a console.log saying a new request initiated which will be printed on the service console and I can send a response say response.send welcome to my app. So this is one root that I have handled that is the default root for the application. Let us rerun the script with the changes. I will browse now to this URL. I will say localhost 4000 and this is the response that I will get welcome to my app. So this response is coming from this server script that I just now wrote and this is the console statement that gets printed on my server site. A new request is initiated. So if I open another browser, browse to the same URL. So it will be a second request that is initiated and the response would be right now the same that goes into the browser. So similarly I can add more roots to my application. I can say app.get if the URL is slash contacts then we can say response.send some contacts info and like this we can have multiple URLs being configured. So this is called as rooting and defining roots in this way. So whenever there is a get request so by.get we mean that this is a get request. If there is an HTTP get request for slash contacts the URL is slash contacts then this is how it will be handled. So here I can write my logic to get the data or the way I want to process it and accordingly then send back the response. So again let me check now when I say slash contacts, I will get contacts info. When I say products, I will get the products info. So these are my roots defined for my application three roots I have. So now you can see as we are writing the code we are every time having to restart the server. That is because the changes will not get reflected. Now there is a utility called as nodemon which is going to help to automatically restart the server. So we have this called as nodemon which is going to monitor the script that is running and in case of any changes it will restart it. But it needs to be installed, it needs to be installed not like the usual way but it needs to be installed globally. So far we have seen installing commands by installing modules by saying npm install express or any other module that you would have done and this means it is going to be installed locally. Locally in the sense you will need to add or require statement and make use of the APIs. And when you want to install some modules as command line utilities it will need to be installed globally. Now what is the difference? The difference is like this when you install something globally then we say npm install nodemon followed by minus g flag. When you say minus g it will be installed globally that means we can use it as a command line utility as a command line utility. So I can run commands like this nodemon followed by the script name. So that is what I have already done here. I have installed nodemon by running this command npm install nodemon minus g and hence I will make use of this command to run this JS code. So here it is, it is watching it and if I make any changes and just save it as soon as I save you can see it has restarted it. So we do not need to bother about this part every time we are making any file changes. So this is how we get started with the express framework and we can send some response. So we can also send files as a response instead of just simple text. So what I can do is instead of doing this I can say send file and I already have an index file with me in the views folder index.html. So when we are working with an express framework then you would you know generally have all the views related files inside a folder called as views that is the general convention. So I call send file and say index.html you will need to just append it with the current directory name followed by the path to the views folder. So now when I browse to the default root it should give me this page index.html which has h2 tag check that out. So here is this you can check the page source it has this html page now. So these are all the get related roots that you can define you can also define how to handle a post by saying app.post give the URL and similarly add the handler here. So all these possible options that you can do here. Now when we are using node as a web server then you might have to go ahead with some template engines which will be rendered by the server side. So one particular template engine is ejs you can even use jade as jade or it is also called as pug. So all those template engines will you will need to work with and you know use a particular syntax to render the UI. And nowadays there is a lot of popular frameworks that are coming into in the market where the UI is written with frameworks like Angular, React and you know so on. So generally the UI part is taken care of by another framework completely and the server mainly acts as the rest server. So node basically the express framework is going to act as a restful server it will only provide the data to the UI and on the UI side you may use any of the frameworks or you may use an app which is going to take this data and display it the way it needs to be done. So this is a popular usage use case you can say of node and express. So we will go ahead and see a demo of how we can write a restful server in node. So we have the basic code here written and few steps to get started to write our own rest server. I will need a router object here from the express let us say dot router and router.root will define let us say slash users and on this I can have different methods like get.post and so on. So what this means is we are going to define URLs in this manner http localhost colon 4000 say slash api slash users. If I say slash users and define the method as get then I would want all the list of users back. Similarly, I can have a post request for the same URL with the data coming in in the request body. So this will make sure that I add a new data into the users database. Similarly, I can have a put request for a particular user and I can provide the name of the user that I want to update and similarly it can also be a delete request. So that is how we can have rest operations defined by defining these different URLs, restful URLs. So let us look at the first two that is how you can make a get and how you can handle a post request in this. So here I have a root defined for slash users and I am handling the get part of it request response and I am going to use the same block for the post as well. So whenever there is a request for slash users I would want to return an array of users back. So I have some sample users with me and I am going to define it here. So whenever there is a get request we will say response.send we can say send the data back but as you know that JSON is a very popular response type in our days wherein data is returned in the format of JSON that is key value pairs. So we say response.json and we pass the list of users. Let us check it out. Now we are using a router object here which is part of the express framework but we need to set it to our app that we have created. That is not part of the program here let me say express and it is this app that is listening to connections it is on port number 3000 here so we need to say app.use. For every URL that comes to slash API I would want to use this router object which contains all these routes to handle my requests. First let me stop this program that is running and start this program. So now here just by fixing this router statement to a method invocation. So we have now a port so we have a server running on this port number 3000. Now let me make a call to 3000 slash users before that there needs to be slash API and I am getting back a JSON which contains the static set of values name and age are the properties. So that is how routing works. So this is the virtual path that I am giving slash API and for every request that comes followed by slash API all the routes will be handled by this router object which contains one route right now that is slash users and it is handling get and it is handling a post request. Now how do we work with the post request when there is new data available to be added into the database then it would go into the post part of it. So that we will not be able to try it from the browser but I have postman from which we can try adding some new data here post I am going to pass the name and age here and click on send but before that we need to add code to handle the post request. So to do that we need to first read the data from request body. So to read data from the request body we need to take help of another module called as the body parser by default it is not available we have to use it from NPM. So I have already installed that body parser I am going to say body parser as the module and set it for the app to use app.use body parser dot URL encoded because the format will be in the URL encoded way need to give some default values and here I can read it I can say request dot body dot the property name which is being passed here name and I can read request dot body dot age. So I will create a new object for this I will say and set the properties of this object and simply add it to my list of users that I have. So this is going to simulate adding of a new user. So I have an in memory set of data which contains three users and I am going to try and add the fourth user into it. So let us check out first when I have this there are three users through postman I am going to add a new user here say send so I have not set any response so that is not going to display anything but let me go back and check my list of users I would have four users now which is with the new entry. We can display our response as well by saying new user added. So let us check out we have three so it will give you some response back so that is how we make use of the request body by using another module here called as body parser. So error handling is important while we are working with any routing so taking care of the 404 is by adding this block of code right at the end we say app dot use and do not provide any root here and this block has to be placed at the end of all the URL handlers. And similarly to handle any internal errors so the 500 errors you will have an error object associated with that. So when you add this error object into your into the route handler it would take care of all internal server errors. So adding that into our code very simple just need to add this at the end so app dot use any of the so if I try to access simply slash users you will get the page not found that is because it is directly going into this block where it cannot match any you know root handlers and that is what you will get as the response. Similarly you will be able to add the 500 related errors by making use of this block which can be added here what is the difference between these two blocks is here you have the error object which takes care of the error related part right. So far we have seen working with in memory data we had the data defined here statically but in the real world applications you would have data coming from the database. So for that in order to demonstrate that I am going to take an example of the Mongo DB so we will fetch data we will insert data into the Mongo DB now. So you need to have an installation of Mongo DB with any of the versions above 3 and you need to start this Mongo DB server on your machine. So install and start this which is going to start on certain port number and we will write code to connect to this particular URL and try to read write from this DBs. That is one thing and second is we need to have a package which provides you the APIs. So like we have used Express in order to work with the routing framework here you need to work with the Mongo's package. The package name is called as Mongo's which gives you the API to read, insert, delete and update records. So Mongo's is an API which is internally using another package called as Mongo DB it is more like a wrapper it has convenient methods it has validation layers so that is why we are going to use this particular package. So make sure that you run this command npm install Mongo's and so that you can directly require it and make use of the different methods. So I have already installed a Mongo's so I am going to straight away add the require statement here. So I am going to add the require statement followed by connection code connection to Mongo DB. So check where this Mongo DB is running. So I have it installed on this particular path and going into the bin directory I have actually started MongoD.exe I have executed this file MongoD.exe which has started the server the MongoDB server and it is running on this port number 27017 and it is listening for connections. So I am going to connect to this now from my code from my node server. So we will say mongos.connect and the Mongo URL I will provide so it is Mongo DB followed by the server URL which is localhost right now on port number 27017 and the database that I am going to work with is the user DB database. This is what I will use alright so because we are fetching data from database let us comment out this static data and whenever there is a request for slash users then we need to run or rather we need to fire a query the DB that is user DB and get the results. So I am going to do that but in order to fetch the query or in order to fire the query while using the Mongo's package it is mandatory to have a model file defined. So here I have a model file already defined with me that is user.js alright. So this model file is used to define the schema that is how is the data or the user details going to be defined. So I have couple of lines here that is require statement and then I use mongos.schema and define the complete structure of the data. So here you can see there are three properties name, address and age. So this is how the data is going to go into the Mongo DB with name which is defined as a string then we have address and there is age which is a number. So need to define a model for this by using mongos.model and because this is in a different external file it needs to be exported by using module.exports. So this is mandatory defining a schema is mandatory while you are working with the mongos package. So I have created this and placed it in a folder called as model. So generally all model files are defined in this way you would have a folder called model and you will have you know the corresponding files added here. So first I will require this user file model file by saying dot slash model slash user. Once we have this object user which is returned now I can fire a query by saying user dot find. Find is the method which is going to you know which is very similar to like a select query and I can specify my conditions that is which all records do I want. There are other APIs where you can find by id you can find by id remove update you can just find the first one that is returned first from the database and so on. So I am using the first one the find and the first parameter is a JSON which will define the condition and the second is my callback. So remember all these APIs DB calls are again asynchronous and whenever there is something which is asynchronous you will always have a callback. So this is a callback now callback always has error as the first parameter and second is the data in my case it is list of users. So when you leave the first the condition empty the JSON is empty if it is in this way it means fetch all records. If I wanted a specific one then I could have defined here saying find all the records with name as john this is how it would fetch the records but right now my query is for all users. So let us leave it blank and check for errors if there is any error then the response will send back that error or you can handle it if that is possible from the program and return that as the response. Response.json will be the set of users now. Now it was not a static data the data which is going to come back it is going to come back from MongoDB. So a quick check in the Mongo's shell to find out what all we have so there is one record present already so that is what we should be able to get in our program as well. So it has restarted with all the changes the user DB now so it is not static data anymore. So you can see that what we got the result here on the Mongo shell we are able to see the same in the browser alright. So that is the function called as find which is used to fire a query to the DB. Now similarly we can save a record into the database so instead of users.push which was adding it to the static array I will define now a new user not a new object but a new user and this user I am referring to the one that is from the model file alright. So a new user I am going to create and set the properties name and age alright and in case we are also going to provide as the schema see is the address then we can even add that request.body.address and to save the data we need to say user object.save and of course we will need to give a call back so there is not going to be any return value but we can always check for the error object. If the error object is set that means there is some error else we can directly say response.send new user added alright. So let us check this the post functionality and I am going to pass slash users let me pass a new value now and I am going to include address and send we are getting the response new user added let me make a corresponding gate request so we forgot to comment out the response here. So both the records have been added here so earlier there was one record now we have the new Anu as the new record added using the save method right. So similarly you can go ahead and implement the rest of the functions like put, delete and so on. So similar roots can be created we can even write a different root handler in this manner we can say slash user and we can take a parameter. We can take a parameter like this which is name and on that we can have get requests and we can have dot post sorry dot put requests and we can have dot delete right. So we can pass a parameter and based on that we can you know have our updates to a record we can delete records and even we can simply fetch a single record as well right. So you can try out such similar set of roots. So I hope this gave you an idea on how we can build a restful server in node using express and at the same time using the Mongols API and connect to the database to read the records okay. So of course on the client side you can have any of the frameworks which will connect and you know process this data alright. So the last part of the session is mainly how you can create your own node project and you know how you can define dependencies. So we have seen that our project has been growing we have you know kept on installing a lot of packages and you know installing them you know with you know different commands. But all that is not required when you work with a big project obviously you will have a lot of you know huge list of dependencies and all that can be listed in a single package dot JSON. So to create this to you know have the initial structure ready you can run the command npm init okay. So npm init helps you to create a new node project and it automatically gives you a package dot JSON which you can manipulate okay. So here is this package dot JSON and the important part here in this file is the dependencies. So we can give our list of dependencies here which are you know in the JSON format. So comma separated we can specify all these dependencies and all that you need to do is simply run the command npm install on your command prompt okay. We do not need to install each of these modules one by one you simply run npm install and it is going to pick up all the files all the modules which are which are specified in this package dot JSON and it will install them one by one okay. So this is how the project structure is generally provided with a single JSON file having all the list of dependencies. We have come to an end of this particular session now and to summarize all the points that we have covered we started with looking at what are error first callbacks how do we work with them in node. We also looked at the file API and looked at the two different ways that is asynchronous execution versus the synchronous version and then we looked at some an important concept called as streams in node.js and how do we work with them by using events. We also looked at how we can build a web server using express framework and lastly we looked at how we can connect to the database and namely the MongoDB from node.js. I hope you found this session interesting and useful thank you everyone.