The Complete Node.js Bootcamp: From Beginner to Expert | Unlocking the Power of Server-Side JS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
YouTube channel so in this video we are going to be learning about nordges from scratch so before we get into this course there are just a few things which I want to mention this course is the part of my merge stack course if you want to check it out feel free to do that which is completely free you can find it on my YouTube channel and the next thing is the coding so you can found all of the coding in my GitHub repository so now let's talk about what is a node.js and why you should care about learning node.js so what in the word is an object so node.js is a free open source cross platform JavaScript runtime environment that lets develop a ride command line tools and server side script outside of the browser so get it I bet you don't because there are a lot of buzzwords right here like open source cross platform runtime environment so now let me just break down all of them for you so what is that open source means so open source just simply means like node.js have a huge community of collaboration and they are consistently providing new feature to node.js and making it up to date and cross-platform means like if you write node.js code so your code can be run on any operating system out there like if you are using a Windows or Mac OS or lining so your code will totally work on any operating system and runtime environment simply means like it is using something called a Google Chrome V8 engine something which we are going to be discussing later on in this course but you don't have to worry about it for now and which allows JavaScript to run outside of the browser so in this course we are not going to be writing any document.queryselector document.getelement by ID or anything related to document in node.js we have something called Global which we are going to be discussing later on in this course but we don't have any concept of document in node.js so we are going to be still writing a JavaScript code but we are not going to be writing any document whatsoever okay so now this is the definition of node.js now you get like what is all of their stuff means but one you should care about learning nor JS so why you should use node.js so I can give you a lot of example of this but I just wanted to mention these few so the first one I want to mention is Javascript so node.js is built on top of JavaScript programming language which means that if you already know JavaScript so you can easily learn node.js and this makes it an interactive option for web developers who wants to use a familiar language like JavaScript on both on the front end and on the backend as well accessibility node.js uses something called Avengers non-blocking input output all of these words which we are going to be discussing later on in this course like what is event driven non-blocking and input output model means we are going to be discussing all of that in a very great detail but I just kind of want to convince you like why you should learn node.js so this makes it highly scalable and it can handle large volume of fabric with ease so making it ideal for application that needs to handle high volume of requests so the next thing is performance node.js is known as for its Speed and Performance because it runs something called a weird JavaScript engine and it can execute code very quickly making it a great choice for real-time applications okay so launch ecosystem like as I said if you have any kind of problem in node.js so you can either copy that problem and you can just paste it on Google form and you can form the solution in seconds because node.js has a large ecosystem and we are going to be also learning about something called a Nord package manager but you don't have to worry about that for now cross platform as I said like if you write node.js code so you can run it in any operating system out there like Windows Mac OS Linux and all of that so I hope I convince you enough so if you really want to learn node.js so you are totally in the right place alright so that's in about one you should learn node.js so now let's just talk about the stacks so if you learn node.js so you can use it in different kind of stacks so in this course we are going to be using a merge tag so which means like a mongodb Express reactors and node.js so we are currently learning node.js so we are currently learning node.js so once we are done with node.js then we are going to be jumping into express.js then mongodb with Mongoose and finally we are going to be jumping into react.js Okay so yeah so we are currently learning about a merge stack you can use it with means tag if you want to so here you can also use like angular instead of react and you can also use it with something called moving stickers sorry I can't pronounce the name of this stack but uh here you can see we have a view GS right here so if you learn node.js so you can use it with different kind of stacks and also with electronics and all of that kind of good stuff so I hope I convince you enough to learn about node.js so if I convince you enough and you've decided to learn about node.js so congratulations you've take a great step so now let's talk about a setup for this code so in this course I'm going to be using a visual studio as my coder because throughout this course we are going to be using this Visual Studio quarter and I have complete course on a visual story recorder and I can totally guarantee I can even promise you that if you watch that course from the beginning through the end you are going to be calling yourself as a Sonic coder you are going to be writing core like a some some sort of a laser or something okay so I'm going to be using visual story recorder as my quarter and if you want to download it so here is the link you just have to write like code.visualstudio.com and I'm gonna also give you that link in the description below so if you come to this side it will by default detect your operating system like I happen to be using Windows so here you can see it detect my operating system right here but if you are using something else so you can download it for like Mac OS for Windows or for Linux if you want to okay so you can learn more about that or you can watch my course which is totally for free on my YouTube channel so now let's talk about the node.js so in this course obviously we have to install a node.js so if you want to install node.js first of all you have to come to this link which is node.js.org forward slash en you can even remove this en if you want to and you can just write node.js.org and it will also bring you to this page right here okay so uh here you can either click on this LTS version or this um current version right here so it doesn't matter which one you want to download you can download any of them but for me I've already downloaded this one and chances are your version will be totally different than mine like not chances this is a fact if you're watching this video your version will be totally different than mine because node.js as I mentioned like they change like they have a huge community and they change these node.js a lot they are providing new feature to this node.js a lot so if you want to follow along with this course you should have node.js version great then add and it doesn't matter how much grade it is so in my case my version will be 18 point something but in your case it might be something like 20 or maybe 24 or 25 or something version in that area okay so you can just click on this button and it will just start downloading and then hit next next next next next and it will just install it in your machine okay so this is the first website where you can get node.js from the second website is something called a node.js.dev which is for developers so if you want to learn node.js and here you can see that definition which I just copied and which you just saw on slides like node.js is a free open source cross platform JavaScript runtime and blah blah blah so the difference between these two website is like this website will even allow you to learn node.js interactively so you can just click on this button if you want to download it so here you can see these buttons so you can download either this one or this one I'm gonna click on learn more if you click on there so here you can learn about like what is a node.js how to install it what is the difference between node.js in the browser and weird engine something which we have learned throughout this course but not right now okay so here you can also write your code right here and you can also copy the code from here and you can also found all of this coding inside this website as well so you can just click on this docs right here then click on the current version which you are going to be using so I'm going to just click on 18 this LTS version and it will just brings us their documentation right here and there are a lot of modules and we are going to be discussing a lot of them but not all of them because that will be incentive if I show you every single uh module so let's suppose if you are learning about a path module so I'm going to just search for a path right here I'm going to close them I'm going to click on this path and you can learn more about this path module right here so what is a path module how to Import and Export it and obviously we are going to be learning all of that stuff throughout this course anyways so there's the websites if you want to check it out and now let's make a setup so in my case I'm going to create my setup in my desktop so I'm going to create a folder I'm going to give a name of like node.js forward slash workspace would be fine and I'm gonna right click on it and then I would choose the small option and I'm going to open it with a visual story recorder right here okay so it will now open this folder in my visual studio code so chances are your coordinator will not look the same like mine because I change a lot of stuff inside this real story recorder okay so now the next thing which you have to do is that you have to go to this extension area this area I'm talking about so you just have to click on that but in my case I hide there so I'm gonna have to hold Ctrl and hit Q and then I'm going to click on this extension and it will now brings us to this extension area right here okay so I'm going to just zoom in a bit so you guys can see everything a bit better if you want to install all of them you know what you don't have to install all of them but you just have to install this code Runner right here and this prettier so this pretty area allows us to format our code is something which I don't want to discuss right now and then you have to install this code Runner because throughout the course I'm not going to be using a command palette maybe in some situation I would but not that often so I'm going to be using this code Runner extension right here which is you can use it for C C plus plus JavaScript and all of this kind of and all of these kind of programming languages right here so now let me just create index.html file right here so I'm going to just Square index.ht not HTML but JS file because we are going to be running all of our code inside this file so now let's suppose if I want to write like console.log of hello node.js or something like that so now if I want to run this code so how can I go about doing that well the first way is that we have to create this uh index.html file then we can write a doctype HTML and then we can just script and uh you know I'm going to remove that I'm going to write this script SRC and I'm going to link it right here and then we can open this file and then we can use that but as I said we are not going to be using this HTML and all of that stuff throughout this course because we have to use something called a document for there like a document and this is a JavaScript used for front-end development and this is a JavaScript used for backend development So currently we are learning about node.js so we don't have to create an index.html file so how can we run this code so we can run this code by using our terminals so you can either open this terminal right here or you can open your integrated terminal which you will have inside your visual story recorder so you just have to hold Ctrl and hit till day until will be underneath your Escape key and above your tab key so here you can see we have this integrated terminal right here and this is the path okay so what I'll do is that I am going to just first of all write a note and then the file name so in this case my file name is index.js right here so I'll just write index so I'll just write i n d and hit Tab and here you can see it will just auto complete it for me I'm going to hit enter and here you can see it will now gives us hello node.js right here but that will take a lot of our time so let me just open that right here and let me just clear the terminal and let me just run that one more time first of all we are going to be writing a node and then the file name in this case I'm using index.js so I'm going to hit enter and here you can see it will gives us that message right here like Hello uh hello node.js right here inside of console but this will take a little bit of time so the next thing which we can do is that we already installed this code Runner extension right here so now we can use there so if you want to run this code by using a code Runner so you don't have to specify this path and then use node and then the file name you just have to come to the file and you just have to click on there and here you can see it from now gives us the result of that file right here so that's completely awesome but you know what will be more awesome than there is to create our own shortcut for there like I'm gonna just hold I'm gonna just use my shortcut like that and here you can see it will also run my code I don't have to move my mouse to this button and then click on it it will take me a little bit of time so I'm gonna just use my shortcut like boom this just like that if you want to create a shortcut for yourself so you just have to first of all hit F1 button and here you just have to write like um open keyboard keyboard shortcut preferences or something like that I'm gonna click on him and here you just have right code Runner okay so if you just write code Runner then I want you to just click on the second option right here which is this code Runner um dot run this extension right here double click on it and here you can specify your shortcut so here you can see I've already provided my shortcut which will be shift enter but you can provide any kind of shortcut if you want to but in my case I am going to just write I'm going to just hold shift and hit enter okay so it will just create this shortcut for me so anytime I want to execute my code so I can just use that shortcut by holding shift and hit enter you don't have to do that you can just click on this button and it will also run your code but if you want to create a shortcut for yourself so that's how you're going to be creating shortcut for yourself I just forgot to mention how you are going to be checking the version of your node.js so you just have to open your terminal and then just write node and then Dash V and hit enter and it will now give you the version right here but chances are your version will be greater than mine because as I said that node.js changes a lot I resumed it so chances are your uh what do you call it your node.js version will be totally different than mine so you can also check it by using node dash dash version if you want to and here you can see it will give you the same exact result right here you can use no dash V and it will also give their result and by the way one more thing if you install node.js in your machine so you will also get npm for free as well so you just have right npm-v and it will also give you the version of npm right here so I'm using 9.5.0 and chances are your version will be greater than mine so you can also check that by using npm-version and hit enter and it will also show you that exact version which I'm using right now so I'm gonna just write accent and you can also use that version by using your integrated terminal on visual story coder if you want to so I'll just write like uh now dash V and also node dash dash version and npm dash V and also npm dash dash version npm dash dash origin right here so it will also gives us that um same result right here as well say bye to this one so now let's talk about something called Rebel in node.js so which is just a area where you can run your node.js code so you just have to open your terminal and by the way Ripple just turn from read email print Loop you just have to open your terminal I'm going to make it a bit bigger so you guys can see everything a bit better and I'm gonna just write node and hit enter so if I do them it will ask us like if you need some help so you can just use this command which is dot help I'm going to use dot help right here so here you can see it will not gives us different kind of commands like Brink clear editor exact and also here oil to loading self so you know what I'm going to just declare my variable like const name and it will be now equals to my own name like Hussein web dev okay so I'm going to hit enter right now and now if I want to check it so here you can see if I write name and here you can see we are now getting this Hussein web dev right here I can also write like function and I'm going to just give a name of like Grid or something like that username it will tag a username and I just hold shift and hit enter and now I'm going to just write console.log of username okay so then finally I have to close there and hit enter so now if I want to provide some sort of a value turn so I'm going to have to write a grid or for Zen like so and close it and hit enter and here you can see it unlock uses who's in right here I should have reported welcome welcome or some sort of a message but I forgot to do that if you want to get out from this Ripple so you can just hold Ctrl and hit C and use their shortcut one more time like hold Ctrl and hit c one more time and you will just get out from there if you don't install node.js so you will not see this so you if you just write node and hit enter so you'll see some sort of error message if you didn't install node.js in your machine okay so I'm going to hit Ctrl and hit C command C or control C hit Etc right here so that was it about Ripple so now let me just show you how we can run our node.js code so what I'll do is that I am going to just write some sort of a code like I don't know let money will be equal to true and if you have money then you can just like I don't know Buy stuff and if you don't have money so what you can do is that you have to bring money let's see if I want to run this code so I already have a shortcut I can either click on there or I can use my shortcut which is shift holding shift and hit enter and here you can see it will not use us bring not bring stuff but buy stuff but if you change that to false save my file clear the terminal and if I want to use it one more time and here you can sit from now ask us to bring money okay so I'm going to close there and that was it about how we can run our code what I will do is that I am going to uh delete that how can I delete it I'm going to select it and hit delete button so it will just delete it for us alright so welcome to the first chapter of this course so in this chapter we are going to be learning about a module wrapper and index.js right here so here you can see I just create a folder given name of like module wrapper and inside this folder I have index.js file right here okay so what in the word is a module wrapper in node.js so anytime you want to execute our code so our code will be wrapped with this function so you know first of all let me just write some code so I'm gonna just write console.log of hello node.js or something like that and now if I want to run this code and here you can see it will now gives us that hello node.js right here in the console you and I both know that so anytime we execute our code our code will be Ram with something called Ife function or module wrapper function so it will look like this let me just write a code for it so I'm going to just write a function given name of flag some sort of you know I'm not going to give him any name and it will take these parameters to exports then we have require then we have module and finally not finally but then we have a file name and finally we have other name and then it will wrap all of our code right here so this is how our code is get executed so anytime we execute our code it doesn't matter if you write console.log or Loops or function or anything like that or code will be wrapped with this iffy function and it has these parameters like exports require module file name and directory name as well so you know I'm going to write my code right here like console.log uh hello from iffy you don't have to worry about what if your function is this is just a pattern it was used back then but not that much nowadays but hey if you want to run this code so you will have to run it right here so like this is why we call this function is a if you function immediately in work function expression so that's what if it means okay so you know I'm going to comment this line out and I'm going to sell my file right here so now I'm going to run it and here you can see it will now gives us this result right here like hello from iffy function so you now you might be thinking like uh what's the point of all of these stuff right here like all of these parameters so what I will do is then I'm going to remove my console log from here I'm going to comment this line now oh you know I'm going to cover all of this entire function right here and what I will do is that currently I'm gonna just show you this file name and this directory name for now so what I will do is I will just write Consular log and dash dash file name right here okay so if you sell our file and now I'm going to run it and here you can see it will not gives us the entire path right here and also this file name right here as well so index.js file okay so it will gives us that path of that file right here but I'm gonna clear that out if you just duplicate this line of code and I change that to like their name like directory name and it will just gives us the directory name but it will not gives us that file name so you know I'm gonna run this code and here you can see it will give us all of that entire path right here but it will just omit their index.js right here so then it's because of that directory name or the folder name it fuel just gives us all of the entire folder name but it will just remove this index.js right here at the end but the file name well gives us this index.js at the end right here so I'm going to just close there so this is what if we function or module wrapper is so anytime we execute our code our code will be read with this Ife function if you just simply means immediately invoke function expression and it will take these parameters like exports require module file name and their name so I am going to show you there next and then finally I'm gonna also show you this module as well okay so that's a module wrapper in node.js alright so now let's start our next chapter which will be modules modules index.js right here so I just create this folder given name of like modules inside this folder we have this index.js right here so what are modules first of all so modules are a way to pass over code from one file into another file so it will allows us to reuse our code so that we don't have to write our code again and again in different kind of files so let's suppose if you just write some sort of you know what let me just create a new file I'm going to give a name of like greet.js right here here so we are going to be just creating one simple function inside this folder like function and I'm going to just give a name of like grid it will take a username and I'm going to just log this username like hello and also I'm going to just provide a username right here okay so I have this function right here but if I want to use this same function inside this index.h I mean like inside this index.js file and let's suppose if you have like 10 other files so how can I use this exact same function in 10 other files solution is to use something called modules in node.js so we have this concept in react.js which we are going to be also learning very shortly and we also have the concept of modules inside this node.js as well so what I will do is that first of all I have to tell other file uh tell other file you can use this same function okay so first of all I have to tell all of these other files like you can use this function right right here so how can I tell other files like I can use this function well I have to first of all write a Mojo dot exports and then I have to specify the thing which I want to tell other files like you can use there so I'm going to just pass my grid right here so just a grid just a function name right here so do you remember this module and this export from somewhere I know you do because if you go to our module wrapper function right here so we also have this exports right here and we also have this module right here so we just write one module dot exports and it will be now equal to the function name so now it will allows us to tell other file like you can use this function in all of their other files which we have inside our directory okay so now that we tell other file that you can use this function so now it's time to bring that function from this file and use it inside this index.html file so how can I go about doing that well so first of all we will just write const and then we are going to be just writing like a grid right here so I'll just write a grid and it will be now equals to require and then we have to specify the path and this one allows us this will allows us to use their function from their index.js file and we can use it right here okay so we can now use this grid function inside this index.js file right here so what I'll do is that I am going to just write grid and I'm going to call it right here and I'm going to just specify who's in and now I'm going to run this code and here you can see we are now getting hello Hussein right here we are not running this function we are not running this file we are running this file right here okay so if I want to run this file so I'm going to just run it it will not gives us anything because we are not running this function inside this file instead we are running this function this grid function inside this index.js file right here so I'm going to just remove these comments from here and I'm going to also remove this command from here as well so that was the first example how you can use it so let's suppose if I want to remove all you know you know I'm gonna just show you there a little bit later so that's the first example now let me just give you one more example so what I will do is that I'm going to create one more file I'm going to give a name of like pupils.js right here and let's suppose I have three variables I'm going to give a name of like uh I don't know person one will be equals to Hussein let person 2 will be equals to LX and then let person 3 will be equals to John okay so now if you want to use these same variables outside from this file like if you want to use these same variables inside this index.js file or maybe in some sort of other file like maybe in this grid file so how can you go about doing that first of all we have to tell we have to tell other files you can use these these uh very variables there you go first of all we have to tell other files like you can use these variables so how can we tell them first of all we are going to be writing module then we are going to providing exports and it will be now equals to person one okay so if you save this file and now let's suppose if I want to use it inside this Grid or not grid but this index.js file so first of all I'm gonna have to import it so if I want to import it I'm going to use uh const and then person one and it will be now request to require and then the path will be this pupils right here and notice I am not providing dot JS extension right here here we have this extension right here so here when I'm requiring it I'm not providing that dot uh dot JS extension right here if I do that let me just provide.js extension and if I log that to a console like person one and sell their Runner and here you can see it will gives us that same result but if I didn't provide it like is this one I didn't provide there or if I just remove it from this one as well okay so let me just run there one more time and here you can see it will just work the same but we will see a little bit of conscience in other Modern es6 Way of importing you know that's other story okay so that's how we can get this person one so what about this person two and person three if you also need uh this person two and person 3 inside the other file so how can I use that well now we have more than one thing so we have to wrap that in curly braces right here so I'll just wrap them inside a curly brace and I'm gonna just specify person one comma person two comma person three right here so now we export that as object so I'm gonna just sell there and now here what I'll do is there I'm going to also use these curly braces right here so I'm going to just write person one comma person two comma person three okay so if you do that now if you sell a file now if you run it so here you can see it will give us that same result right here so then we are getting I'm going to remove them and what I will do is that I'm going to log their other ones to the console as well so person two and then we have Consular log person three okay so now if you sell there and now if you run it and here you can see we are now getting uh Jose and Alex and John as well so we can also pass it to this function right here so we have this function definition inside this grid function right I mean like inside this grid file right here but now if you want to call it inside this index.js file so we already import there by using this require function so we can also pass it in here so I'm going to just remove that I'm going to just use grid and first of all I'm going to pass this person once so I'm going to just copy and paste there then I'm going to just use this grid one more time and then person two greed and then person three okay so if you sell a file so what do you think will be the result just I want you to just pause this video and I want you to give me the answer like what what do you think will be the result for this course so now it's time to run here so I'm going to just run this code and here you can see it unlock uses hello Hussein Hello Alex and hello John right here okay so this is how we can use the required function to get the data and this is how we can use module.exports to export the data or to tell other files that you can have access for to this function like you can do whatever you want to do with that you can execute or you can like do whatever you want okay so this is how we can um tell other files you can have access to this function or variable or anything like that and getting that function right here okay so this is how you can get it right here all right so that was it about modules in node.js so this way of importing and exporting is totally okay but we have one more way to Import and Export over data from one file into another file and which is called import and some stuff which I'm going to show you just in a second and Export some stuff not exports but just export some stuff so now you might be thinking like we just learned about requires so why in the world we will need to use import statement that's because I have seen a lot of developers including me myself who work with the merge tags so for front end you're going to be using import statement but it can be a little bit of challenging like you are going to be using Import in a countless file like you're going to be creating different kind of components and you are going to be using this Import and Export all the time but when it comes to banking you are going to be using required so it can be a little bit of challenging so that's why okay and that's the only reason node.js developers also allowed to use this import statement inside onenote.js as well okay so if you have like node.js version less than 8 I guess so it this feature will not work but if you have greater than add version this feature will work and throughout the course I'm going to be using this new newer way of importing and exporting so what I will do is that I am going to just collapse there and I'm going to create a new file I'm gonna give a name of like uh I don't know three es6 Imports okay come on not like that let me just do that one more time let me just collapse there are three es6 Imports and inside this folder I'm going to create index.js file right here okay so the syntax will look like this first of all let me just create a function file like um green.js or something like that it doesn't matter I'm going to just zoom in a bit and I'm going to just create that function right here so I'm going to just give a name of like grid and username and I'm going to lock the console like uh hello and also username right here okay so if you want to tell other file tell other files you can use this function so if you want to tell other files like you can have access to this grid function so for that you are going to be writing export default and then the function or whatever name you want to provide like in my case I'm going to just provide grid right here and this is how you're going to be telling other files like you can have access to this grid function okay so that's how we can export this function and now if you want to import it inside this index JS file so I'm going to first of all use import and then the name so here you can see if I just write gr and here you can see we are getting this shortcut so if I click on it it will just import it right here but in this case you have to specify the dot JS extension so you don't forget that like in this case you have to specify this dot JS extension if you didn't provide that it will give you a nasty error and you will take like a lot of time to debug this and it will waste a lot of your time to figure out where in the world is the bug so you have to specify this MJS are not MJS but JS extension right here okay so now let's suppose if I want to use this function so I'm going to just specify my name right here so everything is like totally okay we are exporting we are creating our function we are exporting it right here we are importing it right here we are also providing this JS extension right here and if we run this course so let me just run there and here you can see we are getting this nasty error and it is saying like warning to load an es6 module set the time module in the packet Json or use the Dodd MJS extension well that sucks now we have to change both of these file extension to MJS so this is going to be the first solution for that problem so I'm going to just change that to MJS and I'm gonna I'm gonna say yes to this one search we'll just provide that MGS M right here okay so I'm gonna just oh come on I'm going to save this file and I'm gonna also have to change this extension to MJS instead of JS right here so now if you want to run this code so I'm going to just use shift and hit enter and here you can see it is totally working okay but nobody use this MJS extension uh because we are working with the Javascript file so why in the world we are going to be writing MJS so nobody use this MJS extension this is a solution which node.js is providing but this is not a better solution so I'm gonna have to remove these amps from here and I'm gonna have to sell this file so here you can see it will just remove that M from here as well okay so if you run there so here you can see it when log uses then another way which is called um what do we call it set up es6 set the type to module in the package Json file so if you want to get a packet Json file for there we are going to be using our terminal you don't have to worry about what a package.json file is and what a npm is you don't have to worry about all of that we are going to be learning all of that in a really great detail later on in this course but currently we have to generate that file so we can use this newer way of importing and exporting so what do we have to do I am going to just open my oh you know you know what I'm gonna just open this integrated terminal right here I'm going to just zoom back so but you guys can see everything a bit better here you can see we don't have any uh package.json file right here but if you want to generate red one so for that we are going to be first of all navigate through that file so here you can see currently I am inside this uh see user desktop node.js workspace right here so what I will do is that I'm going to just write CD to change the directory to this third uh folder right here so I'm going to just write it three and hit Tab and now I'm going to hit enter so here you can see now I'm inside this third es6 Import and Export file right here okay so now what do we have to do we are going to be writing something called npm and then init and I'm going to hit enter right here so it will first of all ask us about a package name so I'm going to just hit enter I'm not going to be providing any package name no versioning I'm gonna just hit enter hit enter for description entry point test command git repository keywords author license and it will finally generate that package.json file for us right here which you can see right here okay so I'm gonna just save about this one and now I'm going to just open this package.json file right here and I'm going to just provide type in quotes this is a Json file right here so we have to use these codes and type will be now set to module okay so now I'm going to send my file and by the way don't forget this comma like sometimes people forget this comma and they get an error it's sometimes saving the file and something like that anyways so just write this um one line of code which is Type to set to module and then save about this file then open your index.js file and now if you run over code and here you can see we are totally getting this hello Hussein right here if you change that to something else like I don't know apple or something so if you run it and here you can see we are now getting hello Hussein and hello apple right here okay so our code is still working but there is one more gotcha which you have to keep in mind so let's suppose if I just remove that dot JS file extension and now I run it so here you can see it will gives us a beautiful error like cannot find the module very helpful right so it will give us that error and we have to specify that dot JS extension right here and there is one more gotcha which you have to keep in mind we don't have access to that dash dash file name no more so like we can't use this file name no more I'm gonna sell my file and I'm gonna run it and it gives us this beautiful error like what the hell are you doing Jose and what is their file name and all of their stuff so dash dash file name is not defined in es module scope this file is being created as a es6 module because it has dot JS file extension and it is found in this location so you can learn more about that if you want to and you don't have no longer access to that dash dash their name as well so if you sell that and boom we are getting this file name error and we are also getting that other one which is the you know what I'm going to just not come on I'm going to send my file and run this one and it is now giving us that their name error right here so we don't have access to both of these right now so what we can do is there for there we are going to be using a path module and it allows us to get our path and all of that kind of stuff which we'll learn next but now second we successfully can run our course so let's suppose let me just create another file I'm going to give a name of like pupils P or ples.js I'm going to declare a few variables like Jose alert P2 will be equals to like Alex and land P3 will be equals to Jordan Peterson and now we have to tell other files like uh you can have access to these files so what I'll do is that I am going to just write export and then I'm going to just write on this curly brace right here so I'm going to just provide P1 comma P2 comma P3 so this is how we can uh like export multiple things at one time so now we export that now if you want to get it right here so what do we have to do we are going to be writing import and curly braces P1 P2 P3 and it will be now equals to not equals to but from and then specify the location so if you wanted to go into uh what do you call it this come on pupils.js file right here now let me just log them to the console like P1 comma P2 comma P3 and now let me just run it so here you can see we are now getting Hussein Alex and Jordan right here so this is how we can use the modern way of importing and exporting and this is what we are going to be using throughout this course alright so next thing which you have to do is that we have to learn about something called The Path module okay so I'm going to just create a folder and file inside that folder and now let's go to the documentation and I'm going to just search for a path okay so here you can see we have this path module right here I'm going to click on that and it will now brings us to this this path module right here okay so we have a few method on this path so we have this best name method on this path we have a delimiter and name for directory and EXT name for extension format is absolute S4 just a little bit of checking and normalize function we have a lot of functions right here so if you want to get all of the code you can also take a look at these documentation and learn more about that like each of these function so like what are they doing and why are they doing you can learn more about that right here but now it's time for us to write a little bit of codes so let's talk about the path module okay so I'm going to just write you know what I'm going to first of all generate that package Json right here so I'll just go to the fourth folder and I'm going to just use npm inner Dash Y in this case and it will just answer all of our questions and it will just generate that pegs.json file for us right here okay so what I'll do is that I'm going to just open this package.json file and I'm going to change the time to module okay so just right type to module and now here we can use our Import and Export right here so what I'll do is that I'm going to just write import path from path module okay so now path module allows us to work with a path like what is a path a path will look like this like first of all we have a C drive and inside this C drive we have a I don't know like courses or something and then we have a node.js course and then we have like I don't know the path uh module folder or something like that so we are going to be working with this kind of path by using node.js so I'm going to just remove them and what I will do is that I am going to first of all use path.best name method and here you can also learn more about the like um return the last portion of the path so this method just return the last portion of the path you can learn more about that right here if you have you are most overtured you can learn more about there right here inside a visual story corner or you can just come to this documentation and learn more about that so what I'll do is that I'm going to just provide some sort of arbitrary path right here like some gibberish so what I'll do is that I'm going to write C inside this C I'm going to go into the node.js and like that so I'm going to just pour double slashes right here and then we will have index.js file right here for some reason okay so I'm going to just send my file and now I'm going to run it and here you can see it will just gives us the last portion of this path okay so the last portion is index.js so if we change that to like I don't know F dot not Apple but F dot HTML and now if you sell there and here you can see it will not just gives us app.html right here okay so and this is how we can use pen.band's name which allows us to get the launch portion of our path so now let's talk about uh how we can omit that extension so we are going to be just writing like path our best name the same function and what I'll do is that I'm going to just specify the same path right here so I'm going to just copy that I'm going to paste right here and I'm going to change that to JS extension and now if you want to remove this dot JS extension from here so we have to specify it right here like dot JS extension I'm going to cover this line out and now I'm going to run it and here you can see it in this case we are just getting this app right here not app.js but we are just getting this app dot uh I mean like we are just getting this app if we uncomment this and now let me just save there and let me just run this code so here you can see we are now getting app.html and in this case we are just omitting that extension okay so I'm gonna just close there and save a bar to both of them and now what else do we need we can also provide a directory if you want to so I'm going to just write like uh Consular log path or name which allows us to provide a directory name so you know what let me just go to our documentation path.urn name and I am going to just click on this pattern name right here and here you can see that the panda their name method Returns the directory name of the path okay so let's suppose if you just provide this uh all you know what let me just provide that same one I'm going to copy that from here and I'm gonna just place it right here so I'm going to just sew there and now I'm going to run my code so here you can see it will now it just gives us this node.js right here because this is our directory and this is a file so it will just gives us this directory right here so let's suppose if I just remove them uh and I'm going to just provide like I don't know maybe courses or something and now I'm going to sell there and let me just run that one more time so here you can see it will just gives us the directory name like the folder name so we are getting the C drive the Northeast then courses right here okay so this is how we can work with this uh Journal right here so the next thing which I want to mention is also the extension name so in some situation we would need extension of a file so we are going to be using that path dot EXT name okay so this EXT Naval allows us to get the extension of that file so let's suppose if I want to just get this URL let me just copy that from here and I'm going to just paste it right here okay so now let me just save there and then now let me just run there and here you can see it will just gives us that dot JS extension right here dance because here we are specifying that dot JS extension so it is now giving us that dot JS extension right here so I'm going to just change that to like something else like I don't know maybe plug or something I'm going to a cell there and now let me just run this code and here you can see it just gives us that extension so this is how you can get extension from a file okay so I'm going to just comment this line out and now let's talk about a Giants so if you want to talk about a join we have to provide a little bit of strings right here so I'll just write constant.join and here we have to specify different kind of strings and it will just generate a path based on that string so I'll just write like C drive and then uh I don't know I'm gonna go in I'm gonna go there I'm gonna provide a comma and I'm going to destroy Jose and web there for some reason and then I'm gonna just provide like courses or something like that and I don't know Redux toolkit course or something or yeah I guess that would be fine so I'm gonna send my file and I'm gonna run it and here you can see it will now generate this path based on what we provide right here like we provide this string this string and all of these strings so it will now just generate this path based on these strings right here so this is how we can use this join method okay so I'm going to comment this line out as well and let me say bye bye to this one and now the next thing which you have to do is that we can also go up a level one directory so you know what I'm gonna duplicate this line of code and here I'm going to specify the comma um yeah I'm gonna just specify the comma and I'm going to specify the quotes and dot dot which means like um let me just write a comment for myself go up a level okay so it will just allows us to go up a level so in this case it gives us like two kid but in this case here it will just omit this Redux toolkit it will give us C for Zen web dev and courses but it will just remove this uh Radix toolkit because we are going up a level so I'm going to just run this code and here you can see it well just gives us C drive hosen web dev and then courses but it will just remove this Redux toolkit from here as well so what I will do is that I'm gonna uncomment this line out I'm Gonna Save it and now let me just run this file so if I just run it in this situation it is giving us this Redux toolkit but in this situation it is removing it from here that's because of these two dots if you want to go to this level like a Zen web dev and you also want to remove this courses from here so you can also do that I'm going to just duplicate this line of code and you can also part comma and then dot dot one more time so here you can see we specify these dot dots two times right here okay so I'm going to just save this file and I'm going to clear our terminal and now if you run this code and here you can see the first one will give us this Redux toolkit then it will give us courses and in this case it will omit that courses as well because here we specify two dot dot um I mean like two times that dot dot right here okay so this is how you can omit these um I mean like this how you can go up a level one more time so I'm going to just comment this line out and I'm gonna also comment this line out as well and now let's talk about a normalized so in some situation you would get like a very ugly in the path so I'm gonna just write call Path dot um what do you call it normalize right here and here I'm gonna just specify like a very ugly uh path right here and it will just normalize it for me like courses and I don't know Redux and I'm going to just remove that to like uh I don't know store maybe and features or something like that and I'm going to provide like I don't know I'm gonna just provide this link right here so if you sell a file so it will just normalize your path so I'm going to just provide a little bit of their dishes not dashes but slashes one more time and now if you run or code and here you can see it will just gives us that exact same path right here here we just specify a lot of them so I'm gonna have to remove them and also from here as well and now let me just save there and check him out so this is how we are getting like uh courses and inside this courses we are getting one in the world is going on let me just clear there uh we are specifying three uh four slashes right here so now let me just uh removed there from the at the end so I'm going to save that and here you can see it will just normalize our path right here and once again you can learn all of these description uh I mean like all of this method right here inside this documentation if you want to so I'm going to just close there I'm going to comment this line out and now let's talk about the parts method so I'm going to just search for a parse okay so this parse method I am going to click on it and the path pounds method returns an object whose property represents a significant element of that path so it will give us directory name root base name extension and file name so what I will do is that I'm going to just consider log path dot uh parts and I'm going to use this path right here so I'm going to just copy this path from here not a pug one but this one I'm going to copy there and I'm going to just place it right here so now if you several file and now I'm going to run my code and here you can see we are now getting all of the information about this path like the route will be set to this one like or root file or directory is the C plus not C plus plus vertices C and our directory is now equal to the C node.js and courses then our base is equal to this app.js or extension is equal to or this JS and the file name is set to app and so we can also get different kind of information based on there like Parts uh and I'm gonna also specify the same URL right here like so I'm going to comment this line out and I'm going to just provide like uh I don't know base maybe sell my file and now if we run it so here you can see it will just give us the file name if you want to get like um let me just duplicate this line of code and if you want to get the extension maybe or maybe the name of the file we can also get there or we can also get the extension if you want to so several file and now let's run it so here you can see it will just gives us the best name the file name and also the extension right here okay so there are a lot of things you can do with a path module and this is a huge module like you can learn a lot of stuff right here but the thing I show you is um I I would say like 80 percent of the time you would use these things and that 20 percent you'll you use like some other things which are mentioned in here like I don't know uh delimiter and all of that kind of stuff so I'm gonna just comment this line out and that's a path module in node.js so then set about a path module so now let's learn about a new module called uh FS or file system module so I'm going to just create this folder and inside this folder I'm going to create this file index.js so before we're getting into all of the code first of all we have to search for their modules so which is file system module which you can found right here okay so I'm gonna just click on there so this FS module have three flavors the first flavor it has is a promise API the second is callback API and the third one is the synchronous API so we are going to be learning both of these three I mean like all of these three and we are going to be writing a lot of code but not all of the code because this module is like supremely long if you want to explore it by yourself so feel free to do that like there are a lot of stuff you can learn right here so I'm not gonna go into that much detail but we are going to be learning about like creating a directory like folder reading a content from the directory removing the directory and all of that kind of stuff so now what I'll do is that I'm going to close there okay so for the comma in there and I'm going to create three files inside there so um promise API this is going to be the first one and I'm going to copy this coding from here and I'm going to place it right there I'm going to remove both of this file okay so I'm going to copy that past it past it one more time so we have these three files I mean like three folders and inside of each of these folders We have these two files right here okay so I'm going to change this one to like um call back API or something and I'm gonna change this one to Third and Sync API so I'm going to just show you according for one and then I will show you like what you can do to remove like all of their color stuff so I'm going to just go inside here and let me just make that a bit bigger promise API okay so I'm gonna go inside this index.html file the I mean like inside this index.js file and I'm going to import everything as FS from this FS promises okay so we are going to be importing all of our content which we are going to be using from this fs and promises and we give a name of like fs and we are importing everything okay so now let's talk about creating directory right here so how can we create a directory or create a folder rather by using node.js so for that we are going to be using the try catch block if we get some errors we are going to be logging there to the console otherwise you want to do whatever you want to do right here so we are going to be using event and as I said like this is a promise API so we are going to be using alert right here like I use a red because I don't want to change things like DIN and catch and all of that kind of stuff if you don't know what a weird keyword is like testing event so feel free to watch someone else video or something like that I either read documentation or something you will get to know what a asynchronous JavaScript is okay so then what we have to do is that we have to get this FS from here and we just have to write dot mkdir if you've worked with this terminal and you know how to create a folder so you already know what mkdir command will do for us it will just create a new folder for us okay so here we have to specify the real path right here like not arbitrary but here we have to specify the real path so let's suppose if I want to create my folder inside the C drive and here you can see I don't have any node.js folder right here like no node.js folder whatsoever so if I want to create the node.js folder so what I will do is that I am going to just write a C drive I'm going to go into a C drive and I'm going to just use node.js right here okay so if you want to create that folder like this node.js folder inside this C drive so how can I go about doing that we already write a code for that and now let's just write um some sort of a log for yourself like uh I don't know uh folder create created and dot dot dot so now if you sell that now if I run my code so here you will see a node.js folder okay so I'm gonna just run my code right now and here you can see it is now giving us this message like folder created and here you can also see this node.js folder right here if you double click on it and here you can see we don't have no content nothing inside this folder but now our folder is created right here so the next thing which I'm going to do is that you know I'm going to delete this folder one more time I'm going to delete it there I don't know how to do it just click on and it will now delete it from here and now the next thing which I'm going to do is that I am going to show you an error so here we do specify the C drive right here we do specify this no modules I mean like this node.js folder right here and now I'm gonna go inside there and I'm gonna create a new folder like I don't know courses maybe so if I just run this code I'm going to save this file and if I run this code we are getting this nasty error like no such file or directory mkdir with this location is found so why is there we are getting that error that's because our path is valid from this point to this point right here like we have this C drive but we don't have any node.js folder right here what we are telling node.js is that we want to go inside this C drive and then inside this C drive we already have this node.js folder so inside this folder I want you to create this courses folder for me but currently we just have this C drive and we don't have this node.js right here so that's why we are getting that nasty error okay so let's suppose if I just create this node.js folder right here let me just run this code one more time and it is saying like no such a file or directory is found I'm going to close that and I'm going to create a node.js folder right here and here you can see we have that node.js folder right here so now if we run our code so I'm going to just run my code and here you can see it is saying like folder created and now if you go inside this node node.js folder so what are we seeing we are getting our courses folder right here as well so let me just make that a bit bigger so you guys can see everything a bit better right here so we have to specify a valid path in this case because they don't play no games so yeah but if you still want to create that folder let me just delete this folder from here um you know what I'm gonna just yeah this is how we can delete this folder anyway so now we delete this folder and now if we run our code so here you can see it will just gives us that error but if you still want to create this folder like um if you want to go inside this C drive and you want to create a node.js folder and inside this node.js folder you want to create a courses folder so for that you are going to be providing this flag which is called uh first of all you have to specify these curly braces then you have to specify recursive and then set that to True okay so now if you sell there and now if you run our code so here you can see it is now saying like folder created and now if you check him out let me just remove that from here and now if you check him out here you can see we have this node.js then we have courses and all of that stuff so I am going to delete there one more time and what I will do is that I'm going to create a new folder inside there like uh courses uh Redux Redux and inside this Redux course uh I mean inside this redex folder I want to create one new folder which will be like toolkit toolkit or something like this so I'm going to just sell my file and now I'm going to run my code and here you can see folder is created I'm going to check it and here you can see we have courses Redux inside this Redux we have this redx toolkit right here so anytime you want to create all of these folders for that you are going to be providing this recursive and set that to True okay so what I'll do is that I'm going to remove this calling from here so save a bar to there so this is how we can create a directory and this is how we can provide a recursive lecture so that was it about creating a folder so now let's learn about how we can read um the content of that folder so what I will do is that I'm going to just write const files and it will be now set to await and we want to use FS dot um you know what I'm going to hit undo and it will now give us node.js courses redex and Redux toolkit right here so we have all of these folders right here okay so I'm gonna go inside this node.js and I'm going to remove all of their folders so we just have this node.js right here and here I'm using that fs and I'm going to use different method in this case because we are reading the directory so we are going to be writing like reader okay so it allows us to read our directory so I'm going to just provide C drive and node.js and inside this node.js we just want to read that okay and now let me just remove this console log from here and here what we have to do we have to specify the four off Loop like we have to iterate over to all of the files which we are currently getting so I'll just write four of and here I'm going to just specify the file and here files and now let me just log that to the console like file not file is where file there you go I'm going to just provide a file right here semi-file so now if you run our code and we are not getting anything that's because we didn't specify any files right here so let's suppose if you have some sort of a file so like I don't know I'm going to just create a text file and I'm going to just give a name of like um I don't know hello.js or something you know I'm going to just give a name of like hello I'm gonna hit enter and now I'm going to just specify some sort of a Content inside there why it's not opening open it come on it's taking forever and I'm gonna just write hello Hussein okay so I'm going to send my file and now if you run our code and here you can see we are just getting hello.txt right here if you have like more files in there let me just copy and paste and paste the past let me just copy and paste there all the time and now let me just clear the terminal and now let me just run this code and here you can see it will now gives us all of the files which we currently have available inside this folder so I'm going to just remove all of these files from here okay so let's just clear this out and yep there's totally okay this is how we can read the file I mean like read the content of the file and now let's talk about how we can remove folder slash direct query okay but if you want to remove a directory first of all it should have to be empty so like let's suppose if you want to create a courses folder right here I just create a courses folder inside this node.js right here inside this node.js I have this courses folder there we go but both of these folders are totally empty so if you want to remove that it should have to be empty like if we have any file whatsoever like any content whatsoever it will not remove it so I'm gonna also show you that so I'm going to just write event and then if I start uh what do we call it rmdir so if you are aware of that rmdir from terminal you already know what it will do for us so I'm going to just use like fs and then rmdir and here we have to specify the path so I'm going to just find I want to go into the node.js and inside this node.js you want to go to the courses okay so we just wanted to remove this courses folder from there and why we are getting this nasty error come on we don't want to power any finally whatsoever anyway so here you can see we just provide this path like we want to go into the node.js and we want to remove this courses folder or maybe both of these folders so I'm going to just run this code right here and it will not give us anything but if you go inside this node.js right here so it will just remove that final thing over that final folder which we have provided right here okay so let's suppose if I run this code one more time it will say like no such a file or directory like rmdir like we don't find that courses which you specify right here so we don't have any courses right here inside this node.js so you can delete there anyways so what I will do is that I'm going to create that courses folder one more time I am going to just create that folder right here I'm going to give a name of like courses and inside this courses in this case I am going to just create a new file and I'm going to just give my name of like hello okay so now currently we just have this file like this folder is no longer empty so it will not delete it so if you sell a file and now if you run it and here you can see your directory not empty rmdir it should have to provide directory is not empty so it's saying directory not empty rmdir so we have to first of all remove the directory I mean like first of all we have to empty the directory then we can remove it from our computer so I'm going to just run this code one more time and it will just remove the directory from this node just for a file right here oh my goodness we have to go back by using that and now if you go inside so it will just remove that directory from here so that's that so now it's time to learn about on how you can create why in the world we are getting this error I am going to go inside there one more time and that's gone anyway so now let's learn about how we can um create and um write files okay so like how we can create a file and how we can write to a file so what I will do is that I'm going to remove this code like this line of code right here and I'm going to just write a word one more time fs and in this case I am going to write write file right here this right file method and here we are not going to be specifying any path whatsoever we can specify it but in this case I wanted to get like this folder inside I mean like I want to get this file which you are going to be creating inside this directory so I'm not going to be specifying any location whatsoever so what I will do is that I want to get like readme.md file and inside this file I want to get like Hello uh node.js right here so what we are doing is that we are creating a new file given name of like readme.md and we are providing this content to it like hello let me just get the hello node.js so we are just providing this content inside this readme.md okay so what I will do is that I am going to just run this code and here you will notice that file like we currently don't have any readme.md file but if I run this code and here you can see we have this readme Dot md5 right here okay so I'm going to just double click on it and here you can also see that we are getting this hello node.js why this is coming from this node.js Hello node.js message right here which we are specifying it right here I'm going to close them and this is how we can create a file and this is how we can specify uh new content to it but there is a gacha uh if I just remove that instead of node.js I'm going to change that to hosen pep Dev okay so if it's several file like currently we have this hello node.js right here but if we run this code one more time I mean like run this file one more time if we go inside this readme.md and what we are getting is hello Hussein web dev right here where in the word is or hello nor JS where is that well that's replaced with Jose and web there because Jose and web dev have a lot of power so that's why that's got replaced anyways but if you uh if you wanted to append the content so you can also append the content if you wanted to so by default it will just replace it with a new content but if you want to append more content into this file so you can also provide more content into it I'm going to show you that a bit later like how you append your content into to that file now let's learn about reading a file so how can we read our file and how we can like interact with it so now the next thing which we have to do is that we have to remove this line of code and I'm going to just write const data because we are going to be getting some sort of a data we are reading our file I'm going to just use a word fs.read file right here in this case read file method so which kind of file we wanted to read well we just want to read over readme.md file so I'm going to just write readme.md file right here and I'm going to just log there to the console so like I'll just write like console.log data right here but what do you think will be the result if you just run this course so if I just run it right here it will give us something called a buffer or some people call it a buffer buffer or buffer it will give us their buffer but if you wanted to get the actual data from this file so for that we are going to be providing a flag to it so which kind of flag we want to provide we are going to be wiring the utf-8 so I'm going to just provide a utf-8 right here so if you just provide this utf-8 and if you sell a file and now if we run it and here you can see it will now gives us the content of that file like hello Jose and web dev right here okay so this is how we can read the files so now let's talk about appending the data so what I will do is that I'm going to use you know I'm going to remove both of these line out and I'm going to just write a word FS dot append data or append file in this case and I'm going to just select my readme.md and I'm going to just provide like uh I don't know JS is the best right here so if you want to append this data to this uh readme.md file like hello Hussein web dev and we want to append this node.js is the best data into this file so we are going to be using this append file and now if you save our file and now let me just run this code and it will just append it we can also provide that Vlog for us but anyways I'm gonna just open there and what we are getting is that node.js is the best okay so we are now getting this line of code so if you want to print something else like um Jose is also the best instructor or best teacher so now I'm gonna sell that and I'm gonna run my file and let me just open that and here you can see it is now giving us Hussein is also the best instructor in the entire world yeah so this is how we can append uh new content to our file okay so now let's talk about a copying a files so I'm going to change the label to copy come on copy file okay so we are going to be getting the content from one file and we are going to be creating a new file and we are going to be placing our content into that new file so how can you go about doing that so I'm going to remove there and I'm going to just use a red fs.copy file as you guessed it and we are going to be selecting our readme.md file which is this file right here and we want to tag all of the content which this readme.md has and we want to create a new folder uh so I'm going to just create a new folder like uh I don't know maybe um info.txt would be fine and I'm going to just place all of the content from this readme.md to this info.txt currently we don't have that info.txt file right here but once we run our code we will have that info.txt file so what I will do is that I am going to run my file and here you can see we are now getting this info.txt I'm going to double click on it and what we are getting we are getting all of the content right here like hello Hussein web dev node.js is the best and Hussein is also the best right here okay so this is how we can copy the content from one file and place it into another file right here so now let's talk about getting the information so statistics or something like that whatever you want to call it so get file uh information there we go and I'm going to remove this line of code and I'm going to just write info because we are getting some sort of a data and then I am going to use fs.stat for statistics or stats or whatever you want to call it and I'm going to just get a statistics from that info.txt file which we've created just a second ago this one okay so now I have there what I will do is that I'm going to log that to the console like uh info maybe I'm gonna just log there to a console and I'm going to run my code and here you can see we are getting a lot of data about that file so we have development the mod the end link the uid the grd info uh not fo but I know the size 61 and we are getting a lot of info about that file but we can also get a specific info if you want to so I'm going to duplicate this line I'm going to comment this line out and I'm going to destroy like is directory is it directory or a file like is this a directory like is this a folder or a file so if I just run there it's not a file it's a directory no yeah is it a direct what I just said is directory order files so it's not a directory it is giving us files like it is a file so that's why we are getting fonts right here okay so now I'm going to log one more thing to the console like console.log info dot is file in this case and now I'm gonna just run there and now let me just save there and here it will now gives us true right here because this is a file this is not a directory so it will gives us true right here so that was the first flavor of this um what do we call it FS module okay so you can do you can still run all of their method in this callback API and in this Sync API as well but in this callback API you can also also pass a callback if you want to and I'm going to tell you the difference between passing a callback and not passing a callback later on in this course but currently I just wanted to show you the syntax so let's suppose I'm going to just import everything as FS from justfs not promises in this case we are not getting anything from promises we are just getting everything as a FS like we are giving Alias of their yeah this is just Alias and yeah here what we are doing is like let's suppose if you want to create a file so for that we have a method called mkdir so I'm going to just use we are not going to be also using the try catch Block in this case because this is not a promises we are not working with the promises in this case in this case we are just using a callback so we can just write fs.mkdir and let's suppose you want to create a c you know I'm going to delete this folder from here I'm going to delete this folder from here and I'm going to just go inside this node.js and I just want to create this node just for right here inside the C drive and I'm gonna get my error if you got error and then what we have to do I'm going to just write like if we get some sort of error so then we want to throw that same error okay if you don't have any errors so you just want to lock the console directory created you don't have to do that but I'm just doing it because you want to see some sort of a message I'm going to run my file and directory is created right here so if you go inside there it's empty so in this case like you can read the file you can write to a file you can read the content from one file you can do all of their stuff which I just mentioned inside This Promise API but in this case you don't have to try catch block and in this case you can specify this uh callback function right here so what I'll do is that I'm going to just copy these two line of code and I'm going to remove this uh callback from here I'm going to also delete this node.js folder from here I'm going to just pull out a function you can either provide this function with the name or you can provide a callback function so I'm going to just write error and I'm going to just write if we get some sort of error through that same error right here otherwise we just want to get like directory tree yeah come on created there we go and now if you sell there and now let's just run our code and here you can see the directory will be created right here so you can do all of that stuff I don't want to just like make this course a bit more huge than that so yeah you get the idea you don't have to use the trying catch blocks and you can specify a callback function right here so that was it about promise API the Callback API and this one will allows us to write our code synchronously This Promise will allow us to write our code asynchronously this one will allow us to run our code synchronously okay so if you don't know the difference between synchronous and asynchronous so you can either watch my video on that or someone else's video on YouTube or you can read the documentation about asynchronous programming and all of that kind of stuff so now let's start working so I'm gonna just write import and everything import everything come on import everything is FS from FS module right here okay so let's suppose if you want to create a create a directory so we can just write fs.mkdir you already know there and in this case we can just write our code we don't have to specify the try catch block and we also don't have to specify that callback function right here okay so we can just write like the C drive go to the C drive go to the node uh node.js file do we have yeah we do have inside this node.js folder uh we want to create a new folder like test and inside this test folder we want to create a demo folder and I'm going to also provide that uh what do we call it recursive true because I just want to make this folder I don't want to check them so I'm going to just hit enter uh not enter I'm going to just run my file and node.js recurses true mkdir so why we are getting this nasty error what is it saying the CB argument must be a type of function oh that's because we forgot to include a sink right here as I said this is a sync so I'm going to have to party sync and now if you run it and here you can see it will just create that folder right here so we have test inside this test we will get to see our demo and here you can see we have that demo folder right here so you can do all of that stuff like you can read the content you can copy the file you can append to a file you can remove or delete the directory and all of that kind of stuff but anything you do you have to specify this sync at the end right here you just have to provide this sync at the end so like let's suppose if you want to remove that directory so remove uh remove directory so I'm gonna just use fs.rmdir sync in this case like you don't you can't forget a sync at the end if you are working with a synchronous version of it so you don't have to specify try catch and also the Callback function but you can't forget the sync at the end so let's suppose if you want to just remove like uh I don't know maybe C drive I want to remove node.js uh come on you know what I'm going to just copy this line out and I'm going to just remove this last demo folder which we have right here so inside this test folder we have this demo we want to remove that and I'm going to save this file I'm going to just run it and it will just remove it from here uh yep that's gone from this test right here so I'm going to just comment this line out and yep that's gone so you can do all of their stuff which I mentioned right here but um in this case you have to specify this sync and I already show for you the differences between all of them so I'm going to just remove these two folders I mean like files from here as well so there's a FS module in node.js alright so now let's talk about a operating system module so OS module index.js okay so now let's go into the documentation and I'm going to just search for Os or operating system come on Oprah NG OS in the uppercase yep there we go so we found their operating system module right here this OS module will give you information based on your current operating system like uh the architecture the CPU and the free memory like home directory the host name and all of that so now let's just use them so what I'll do is that I'm going to go and now let's just type import Os from OS okay so let's suppose if you want to get information about what platform specific so I'm going to just use OS for operating system and then platform and I'm going to just execute this function okay so I'm going to just run this file and here you can see it will now gives us the win32 I'm using the windows 64 version uh I mean like 64-bit but it is giving us the platform specific version right here so which is 32 and you can learn more about that right here so like we have um what do we call it platform here is the platform pllt there we go so if I just click on it and it will now give us this definition like Returns the string identifying the operating system platform for which the node.js binary was compiled So like um Linux use IX and it will give us their best for a different kind of operating systems right here but on Windows you will get this 32 um platform specific version right here okay so that was it about the OS platform so now let me just log to the console OS Arc the architecture which my operating system is currently using so I'm going to just run my file and it will now gives us this 64 right here so if you're using like Windows 32 bit so it will just gives us 32. in my case I happen to be using 64 so it is now giving us the 64 architecture right here okay so I'm gonna just copy there and not copy but let me just write them manually and now let's just use CPUs so now if you run there it will now gives us information based on their CPUs like the model of the CPU the speed and the times and you can learn more about that and your information will be totally different than mine okay so here you can get a lot of information you can even restructure that if you want to but in my case that was it so now let me just lock the console OS dot uh I don't know hostname would be fine so what will be the host name so I'm going to sell there and the hostname is equal to this desktop and this name right here okay so that's easy and awesome as well so you can get like a lot of information about your current operating system so I'm going to just use like home um not home but um yeah I'm gonna just use my home there like home directory so I'm going to sell there and it will now give us users and who's in right here okay my name I'm gonna come in there and I'm going to use OS dot um network interface I'm not going to run this file right now because it will show you information about my Netflix so I'm gonna just leave that for you if you want to run it so feel free to run there and then let me just write like OS dot um free mem for the free memory okay like how much free memory I have so I have this amount of free memory and it will give us in bits so that's fine it's a lot and now let me just look to the console like how many memory I have like total memory okay so what will be my total memory I'm gonna sell this file and I'm gonna run it and this is the amount of total memory in my hard drive okay so that was it about operating system module in node.js so there's operating system module so now let me just create another one the seven uh URL uniform resource locator module and index.js right here so now let me just search for their URL okay so if you can get it and here you can see we have this URL module right here I'm going to click on there and you can learn about like the hash uh the host name and also the href the origin the password the path name the port and you can learn a lot of stuff but I'm gonna just show you a few of them so the first thing what I'll do is that I'm gonna have to open my terminal and that's cool and I'm going to just import the URL just a URL it will give us a Constructor so that's why we are just getting the URL and I am going to use const my URL um or you can give any name you want but in my case I'm going to just give a name of like my URL and it will be equals to new URL and here I am going to specify like some sort of a URL right here you can specify any kind of URL right here but I am going to just specify just an arbitrary URL like www.example example.com and I'm going to also provide the port name which will be like uh 8080 one more time and then I'm going to just provide some sort of a path like p a t h go Bank and then I will also provide a query right here so I'll just write a query parameter and don't worry about all of this stuff which I'm writing right now if you get into the express yes with me you will Master every single thing which I'm writing right now and then finally I'm gonna just pour the hash right here okay so now if you sell that and if you want to get information about this current path which we just specified right here inside this new URL Constructor or I shouldn't have to say path because this is a URL and URL just simply send from a uniform resource locator okay so I'm going to log to the console like my URL dot hash let's suppose if you want to just get this hash version right here so I'm going to just sell there and I'm going to just run it and points taking so much time okay so now it will give us that hash right here we can also get different kind of informations so I'm gonna just duplicate there and change this hash to host so like let's suppose if you want to check the host I'm going to comment this line out and now let's just run it so I have to server come on so it will gives us this host right here like uh who's then web dev or if you have like your own website so you can also get this hostname right here anyways if you want to get the port so you can also get your Port like my URL and you can get like uh I don't know the host name maybe so if you save there and here you can see it just gives us the host name without a port which is this port right here or you can just like or if you just want to get the port name so you can just specify that Port right here okay so if you just specify that Port sale there and just run it and here you can see it will give us the port of 88d right here okay and you can also get the edge riff if you want to so it will now use this the all of the path right here like all of the href in HTML we use like anchor tag and report href of some value turn so it will give us that edge referred here okay so I'm going to duplicate them and comment this line out and now let me just write uh I don't know the protocol maybe so let's suppose if you just want to get a protocol so now I'm going to just save there and run it and it will give us the https protocol right here so if you were using like just http B and just run this code so it will just gives us that HTTP protocol right here so we happen to be using https so that's why we are getting the https protocols so let's suppose if you want to get a search so let's just write a search so here you can see it will gives us that query and whatever you are searching for so in this case we are just writing string so it will just give the string so later if you join me on my Express course then you'll get to know how to create different products and all of that kind of stuff so my URL dot search and also get a params so let's suppose we just want to get the search param so now let me just run there and it will be now set to the query and it will be now set to the string okay so I'm going to cover this line out as well and these stuff are like important but in some situation not that much often I am going to write a code in both uh works the same okay both of this code works the same so like if I just write my URL dot 2 spring and now let me just call there and duplicate there and we can also just write to Json and now let me just save my file and I'm going to clear this screen out and if I run it so here you can see both of them will give us same result already here anyways so that's it about the URL module so you can learn more about that like this URL module and you can either require it in by using the es6 version or the required method right here so yeah this is how you can you can learn crazy amount of stuff about this URL uh which I'm not gonna go into that much in detail you can even set there to something else like if you get it by using this method you can just provide the equal to sign and you can change it to whatever you want to change okay so this stuff is totally easy so that's why I didn't want to waste a lot of your time on doing that okay so that was it about a URL module alrighty so then was it about a URL module so now let's talk about one of my favorite module on the entire node.js which is called HTTP module okay so I'm going to just create this folder inside this folder I create this index.js file and I am going to go into that folder so now I am going to just search for HTTP and luckily for us I already did that I'm going to just click on HTTP and here you can learn crazy amount of stuff right here but the thing we are currently interested in is something called a server like how we are going to be creating our own server uh and all of that kind of stuff so I am going to just write type and it will be now set to module and provide a comma in there so if you are coming from some sort of a PHP background and you've worked with a patch or something like that so in that places you don't have to create a server by yourself so if you are working with the node.js so you have to create a server by yourself and I know it sounds horrible like creating a server it's the most easiest thing you can do and you do that like for every kind of project so you will get a lot of practice and in real world project you are not going to be creating a server by using node.js but instead you are going to be using some sort of a library or framework like express.js so later if you decide to learn express.js with me so I'm going to show you what to do and what not to do so the code which I'm about to write it does work but I have to say like nowadays nobody write code like this in node.js they use express.js Library I mean like framework and they create a server in Express yes so I'm showing you uh the node version the vanilla version of creating a server so what I will do is that I'm going to just write import HTTP from HTTP now I import this HTTP module right here so I'm gonna have to uh get this HTTP right here and I have to use this create server and then yes here you have to specify a function and it will not take any name and here you can specify the request and the response and if you want to make it like Arrow function so feel free to do that I'm going to do it as well so now we successfully create or server congratulations we successfully created our server but now we have to store that in some sort of a variable so I'm going to just give my name of like server you can give a name of Apple if you want to let's see rvr there we go so now we do that and it will take like a requests like which kind of request is the user making and we can also provide some sort of a response to it like some sort of a response to a user so what I will do is that I'm going to just write a server.listen outside from this function I mean like this create server right here okay I'm gonna have to provide a port right here so I'm going to just provide a port of 8 000 and then I'll just lock the console like um some sort of a message for myself like uh server up baby server up so now we successfully create our server and now our server is listening on Port 8000 right here so like anytime you go to our browser and we just write like localhost and 8 000 and if we hit enter but over server I mean like our file should have to run so it will give us some sort of information based on that so what I will do is that here I'm gonna just log to the console like request like which kind of request are we sending so it will give us a lot of information about this request so I'm going to go back and now I have to run my file so I'm going to run it and server is up right here but now if I hit enter like first of all I have to write this local host of 8 000 and I'm gonna hit enter and now here you can see our server is spinning right here but if we check out order terminal here you can see we are getting countless of information about this server like the host the parent and all of their method and properties like this stuff is totally insane so what you have to do is that you have to hold Ctrl and hit F and here you just have to type URL and we are not interested in this URL I'm gonna just click on this one and here you can see we are just interested in this URL and this forward slash means a home like if we just provide this forward slash right here and if you hit enter it means like we are going to our home page or if we didn't provide any um anything what servers it will just by default redirect us to our home page and we can also provide forward slash about and all of that kind of stuff which I'm about to show you okay so we are just interested in this URL uh property on this request so I'm going to close this one and I'm going to also clear it out and stop my server right here so if you stop it everything is working the way we expect them to work so I'm going to just comment this line out like so so the next thing which I want to show is that we have to send some sort of a result to our users their user can see some stuff so let me just write it like this dot right right here so if you just write res dot right and this right method will allows us to write some sort of a code so all I'll do is that I'm gonna just write H1 off like um uh I don't know hello from node.js server okay and I'm gonna just close my H1 right here okay so now if it's similar file we have to run our code one more time but I'm I'm gonna have to close this file right here and I'm going to run my code one more time server is up right here and now I'm going to open my new tab and I'm going to just search for localhost 8000 okay and I'm gonna hit enter and what are we getting we are getting this hello from one node.js server that's cool so now we're currently we are doing I'm gonna have to stop this one what currently we are doing is that we are creating our server and we are getting some sort of a request like when the user hit this port like 8 000 so what do you want to do if you wanted to show the user this H1 of hello from node.js server right here okay so that's completely awesome so if you want to end the server um I mean like in the response so you can also just write like a res Dot N and you can provide some sort of a message to a user by doing that which I'm going to also show you just in a second but uh let me show you one more thing you know I'm gonna uncomment this line out and I'm gonna run my server one more time so our server is up and now I'm gonna have to go in here and refresh it and it will still gives us that uh hello from node.js I'm going to right click on it and click on this inspect area right here like so and then I want you to just click on this network panel right here and refresh your browser one more time so once you refresh your browser so what you will see you will see this localhost right here I just did that by mistake I don't know how to undo that anyways so I'm gonna just go back so you guys can see everything a bit better and I guess something is not all right so I'm going to click on him alright so now it's working anyway so here you can see our request URL is set to this HTTP localhost 8000 and or request method is set to get or status code is to uh 200 right here and okay and or or remote address is this port which is this port right here and we don't have access to something called a content type like which kind of data are we sending uh to our server like which kind of data are we showing to the user are we sending some sort of a Json format or HTML or just a plain text even so now if you want to show to a user like which kind of data are we sending so for that we are going to be writing um what do we call it rest dot set header and here what I will do is that I am going to just write content Dash type and I'm going to just have to set there to whatever you call it text forward slash and plain or you know what I'm going to set there to HTML because in this case we are sending html text right here so now if you sell there and I'm gonna have to stop it and then run it one more time so our server is up one more time and now I'm gonna have to refresh click on localhost one more time and what you will see is the content type is now set to text and HTML right here okay so this is how we can send four headers right here so what I'll do is that I'm going to comment this line out and the next thing which I want to show you is the status code like uh here you can see over status code is now currently set to 200 right here but in some situation if we get like some sort of like uh other route like if you just go to this route or status code will be still equal to like 200 100 so if you want to change that to like 404 or something like that so we can also do there so I'll just write res dot status code and it will be now equals to like 404 404 like so and we can also even send a status message if you want to so I'll just write restart status uh not code status message and it will be now equals to like bad or not found or something like that so I'm going to just send my file let me just stop it and let me just open my run my server one more time and now let me just refresh them and here you can see we are now getting that status call right here so if I click on them so it will now give us four or four bad request right here so I'm gonna have to remove their stuff from there as well if we click on them one more time so here you can see it unlock users 404 and bad requests so this is how you can play around with the server and all of that kind of stuff and we can also use a shortcut for this so I'm going to just sell them and I'm going to just write um I don't know short hand um for status code for status code message for status message and also for header so we can also write um shorthand for this so I'm going to just write raise that right hand writn and head like so and it will be equals to 202 first of all we are going to be providing the code and then we are going to be providing the status message like good or something like that and then comma we can also provide or content and all of that kind of stuff so this was the thing I was talking about uh you know I'm gonna have to provide first of all these curly braces and then instead of that comma I'm gonna have to provide this value as object right here so I'm going to send my file uh let's just stop our server one more time and re-run it again and now let's just refresh our browser I'm gonna click on him and here you can see we are now getting two or two good requests and our content tab is now set to text forward slash HTML right here so I'm going to stop my server right here say bye to them and there was just a little bit introduction about HTTP so now let me just talk to you about routing in HTTP module so how can we specify different routes for our users so once again I'm gonna show you the code but this code is not that much usable I mean like nowadays A lot of people use like Express shares they don't even use node.js for this kind of stuff but I'm just showing you anyways just in case if you go to some sort of interview and they ask you to provide routing by just using vanilla node.js not express.js so you would be able to provide them the solution because I'm showing you that right now so now let's just talk about uh routing in HTTP module or something like that okay so inside this folder I'm going to create index.js file and that's cool semi-final let's have a bite with this one and now let me just import first of all my HTTP module from HTTP let me just zoom in a bit first of all what we have to do we have to create our servers so I'm going to just use like http.creates server it will take a request and also the response and now inside This Server we are going to be doing a lot of stuff so let's just listen on Port 8000 you can provide 2000 or 808 or something like that if you wanted to but in my case I'm going to just listen on Port 8000 and server up baby okay so here what I'll do come on all right so now let's talk about the real or routing so if somebody like let me just show you this this forward slash means um this forward slash means the home page and this forward slash about you can specify the amount you can specify like I don't know contact page or something like that and we're not going to be going into that much detail of like a specific ID of some sort and we are not going to be also learning about like uh I don't know product and search query or something like that we're not going to be talking about there but we are going to be just talking about the basic simple routing in node.js so I'm going to just delete that and here we already know that we have uh access to that request or URL property so like a red dot URL which I show you just a second ago or let me just log that to a console so you guys can see everything a bit better dot uh URL I'm gonna send my file run my code and server is up now I'm gonna go into my local host and now if you sell there it will now gives us this forward slash right here which means like we are just going to the home page and let's suppose if I just stop this server right here I'm going to stop it and I'm gonna just clear it out and let's suppose if I run this server one more time let me just close that let me just run this server one more time and let's suppose if I just write like localhost and forward slash uh I don't know Jose and web devs or something like that and if I hit enter okay so here you can see we are now getting this Hosanna webdevroid here rather than this home page so that's why we are going to be doing a routing and here our server is hung up and we are not able to see anything right here go back I don't know what happened I'm gonna have to close my server foreign URL so now let's just provide our conditional rendering right here so if a request.url is equal to in quotes this forward slash so which means like home so I'm going to just pour it like res dot end and we can just provide like um H1 of uh I don't know I'm going to just write home and here I'm going to also put Emoji of home you don't have to do that but I'm doing it because in the recording okay so here I'm going to send my file so if somebody hit this request like if somebody go to this localhost 8000 right here so what do you want to print them so you just want to print them home and this home Emoji right here other than that like if you if they had like about section or Hussein web dev or context section or anything like that you're not going to be showing them anything so what I will do is that I'm going to run my file right here server is up I'm going to go into the localhost and now I'm going to just hit enter right here okay so here you can see we are now getting home and their Emoji will be encoded like this so that's awful I thought I'm working in express.js but I'm not I'm gonna have to remove that home Emoji I'm gonna have the semi file and rerun my server and refresh it and now that emoji is gone anyways so there's the first round so we are now getting this home page right here so now let me just stop there and now let me just run there one more time and we are getting some sort of error I don't know why let me just clear there run it and now server is up anyways so now let's just uh go to any some sort of other route like I don't know about section or something like that and if you hit enter and here you can see our server it's hung up and it doesn't have any idea like what in the world should I do now you didn't provide any else class right here what the hell should I do so now I'm gonna close this or stop myself right here remove them and now let's just provide another route right here so else if so if somebody hit this URL print them each one of home if somebody hit this URL which I'm about to write so request.url and it will be now equals to like some sort of about okay so we just wanted to print them like res dot in and we wanted to just print them like uh H2 maybe H2 of about section or something like that and let me just close that right here so I'm going to send my file and now I'm going to run my uh file right here and it is saying like server is up and now let me just go into my local host and forward slash about right here so I'm gonna just go to about hit enter and what are we getting we are getting this H2 and about section right here okay so everything is working the way we expect him to work what if you go to just uh home section let me just remove that and hit enter and here you can see we are now getting this home section right here so everything is working that way we expect them to work so we can all support different kind of others like uh SF of request or URL and it will be now set to you uh I don't know contact or something like that we can show them a form like res dot send or not send but rest dot end in this case and we can send them through like something else like H3 um I don't know um contact form we can also even show them a counter form if you want to but in this case this is just a simple routing so I'm not going to go into that much detail I'm gonna have to stop the server and relaunch it one more time and now I'm gonna have to go into the context section so I'm going to hit enter and here you can see we are now getting this H3 and this contact right here so that's there and finally like if somebody hit like I don't know some gibberish like let's suppose if some user hit this gibberish and hit enter so our server will hang up so finally we are going to be writing like else right here and let's just write like a wrist not uh res Dot N and let's just write like uh I don't know H1 404 page not found or something like that that's not found and let's just put our set first but before we do that I'm gonna have to close there let's just put our surface right here okay so if somebody's if somebody hit this board so you want to show them home if somebody hit this board we want to show them about section if somebody hit this port we want to show them context section or something like that if somebody hit like this gibberish or I don't know if they don't know about some sort of a specific URL and they had like Jose webdevo for some reason so we just want to show them this 404 it's not found or you can even remove this photo for or just show them like page down phone if you want to I'm going to sell my file close it and rerun my server one more time or the server is up and now here you can see it will not use us like page not found and if I just write Hussein web dev hit enter it will still gives us like 404 page not found in that surface or Roundy face and now if we just write like um contact page it will give us contact page if you just write about it will shows us about section if you just go to the home or if you just put it forward slash or if you didn't report anything at all so we are going to be just getting into the home right here and you can even specify the URLs anchor tags or something like that which will redirect them to something like let me just write restart and and you can also create uh like I don't know a href and Filipino equals to Ports slash like um contact page or something like that and let's just close the original I'm gonna have to close it manually uh contact page zoom out and let's just close our anchor tag right here okay so here this means like it will just generate that uh button for us that anchor tag it will brings us to this contact page right here okay so I'm going to sell my file close my server rerun my server one more time and I'm gonna have to refresh my browser and we are not getting that button ahref right after the end of the new line or something like that you know I'm going to comment this line out and let me just run that one more time server is up refresh him and here you can see you're now getting this contact patch if you click on him it will now bring this to this contact page right here so you can specify this kind of routing if you wanted to or you know let me just uncomment this line out why are we getting that same error uh let me just close the semi file close there and re run it one more time and now let me just refresh it we are getting the home but we are not getting that hey I'm gonna investigate there you don't have to worry about this stuff you know you can write it uh okay that's because we are ending our requests that's why oh my God I'm kind of a dumbo so I'm gonna just I'm gonna copy that and I'm going to just create some sort of a section uh let me just create a section right here section and inside this section what I'm going to do you know I'm going to acquire these back text right here back it's in here as well and here I am going to just write H1 of home and let's just close there and here I am going to also point this anchor tag right here so you are now providing this section tag right here if I can get it like so and inside this section tag we have this H1 of our home and we have this href so let me just run my server one more time server is up let's just refresh it and we are getting a pure text oh that's because of this vectors come on Jose come on uh we can provide forward slash and N or something like that uh I'm gonna have to close that and set that to a single code like so and you know what we have to deal with a lot of stuff that's why I don't like using a vanilla node.js for this kind of stuff so if you are going to be using uh what do you call it I'm going to remove the stuff from here we are going to be using Express yes for this kind of stuff you can provide the I can show you there but it will take a lot of your time and this stuff is like unnecessary so I'm going to uncomment this line out and let's just stop it and let's just play it one more time and refresh your contact pads boom everything is working the way we experiment work all right so that's a basic routing in express.s I mean in node.js and I just forgot to mention one more thing I'm going to open this file and stop this server Run This Server one more time and I'm gonna refresh my browser right here right click on it and open our inspector go to the network panel refresh it I'm going to click on this uh localhost right here and here you can see if I just come to this link it will give us their 200 right here that's working that's why we are getting there if you go to the contact page it will gives us that 200 right here so that 200 is still working but if you just like let's suppose if you go to about section it will still work but if you just write like I don't know Hussein web dev and if you hit enter so the thing is that we are also providing a 200 and okay for this 404 not found page as well so we have to provide a different kind of headers for this stuff like let's suppose we have to create that header for all of them so I'm going to just write like a rest start right um writ and head this is just a short way for creating that um status code and uh status message and all of that okay so I'm going to just type like uh content forward slash type content for slash time and will be now set to text forward slash HTML okay so I'm gonna copy there I'm gonna place it right here I'm gonna copy then I'm gonna place it right here I'm gonna copy there and I'm going to place it right here but in this case I'm gonna just provide like four or four and page in our phone and now I'm gonna just set there two um bad so if you sell a file and now we have the stopwatch server and we have to run our server one more time and now I'm gonna just refresh my browser and here you can see it will now give us like 404 and status is bad so that was just a little bit of thing which I want to mention before we're getting into the next chapter alright so now let's talk about serving a file so let's just create a folder give a name of like serving pages okay Pages index.js file right here and inside this folder I'm going to create a new folder give a name of like public because we are going to be putting all of our assets inside there I'm gonna to create two files like uh I don't know home.html I'm going to create one more I'm going to give a name of like about.html and now inside this home.html file what I would do is that I am going to just write like home right here H1 of like home page or something like that okay and I'm going to also provide like lorem ipsum some W text right here and now I'm gonna also pour this uh logo or icon right here so this is a home page I'm going to copy that I'm going to place it right here and this is a bound page or you know I'm going to select this home hit Ctrl D and change that to about page right here I am going to just write about a b about I don't know yeah I'm gonna just click on this emoji and this one will be some sort of a button or something like that like button of hello or click me or something like that now I'm going to sell my file now we registered these two uh files right here like home.html file and public I mean like about that HTML file as well so now how can we serve this file into this node.js okay so first of all we are going to get our thing oh I have to stop this server right here and now let me just get my import of HTTP from HTTP and we will also need one more file uh one more import to be precise which is FL FS for file system which you just saw a second ago and now let's talk about creating a server you already know like how to create a server in node.js http.creating a server and it will take two parameter like creating a request and providing response entered and now let's just listen not sanitize or something the server.listen and let's just listen on Port 8000 and let's just provide our callback function right here like callback function of server come on server up baby so yep there's all the thing which you already know so now let's just talk about this basic routing so I'm going to just provide a basic routing right here so if uh request.url is equals to this home page what do you want to do uh you know I'm gonna part L Steph of request.url is equals to uh about section what do you want to do with that otherwise what do you want to do so now let's talk about first of all this uh request or you are if we hit that so what do we want to provide we have to provide response dot right head so right head first of all and we are going to be providing the 200 status and the status will be okay and we also have to specify the content for slash type and it will be now set to text forward slash HTML because we wanted to send HTML file so now the next thing which you have to do is that we have to get our FS module and we have to write like fs and then read file which is this file right here so the home file so now what do you want to do I'm going to go back One Directory I'm gonna just go to the public like so and inside this public file I have this home.html file right here so we have this public folder and inside this public folder we have this home that HTML file I'm going to put a comma in there and we also have the prior or error if we have one and also we wanted to get our data okay so now inside there what do you want to do you want to just check or error if you got some error so if you want to just throw throw that error you know I'm going to remove these curly braces from here okay our code is already looking clutter because this is norges and now let's just send this data let's send uh this data which we are getting right here from this function so now if you sell file and now what I'll do is that for me I'm going to cut this stuff from here so let's just first of all take care of this first home page and then we'll take care of the other one so I'm going to just run this server and server is up now let's get into it I'm going to remove all of this stuff from here hit enter okay so uh okay we are getting through a new error and through new error no such file or directory is found so you know what what I'll do is there I'm going to push this score from here and I'm going to make it like public like so and now here what I'll do is then I'm going to just pull out a forward slashing comma in there and now let's just run it and I guess you know first of all let's just clear it out like so and now let me just run it refresh this browser and here you can see we are now getting our HTML file right here okay so here you can see we have our home page and we are getting our logo and we have a little bit of aluminum epsoms right here okay so this is how we can provide uh or this is how we can serve HTML files right here we will take care of their public and this stuff I can take care of that but I'm not going to because it will just waste your time and you're going to be learning about Express yes anyways so I'm going to place this code right here so now let's take care of this about URL so if somebody go to their about URL so what do you want to print them I'm going to copy all of the entire thing and I'm going to place it right here so everything is totally okay but instead here we are going to be just specifying the about.html file right here so if you have some sort of error catch that error if you don't then just end there um I mean like just Port the data okay so now let's just stumble server and run it one more time server is up and now I'm going to go to my about section right here and here you can see we are now getting this about page we are getting over emoji and we are also getting this button right here okay so about section is also working and now finally let's just find else class right here I'm going to also provide these few things or you know we don't have uh any patch for your final four or four page we can create a page and we can like copy all the code and paste it right here but we don't have to what I'll do is that I'm going to just pour just a simple message or something like that I'm going to stop myself right here and I'm going to just write like you know what first of all let me just copy there and let me just place it right here and this one should be 404 bear request and also res Dot N and I am going to just provide like H3 uh 404 page nerd phone surface and now let's just close our H3 right here okay send my file and now let me just go back and this is how all of our code looks like and I swear to God if you start using express.js you will love Expressions because writing all of this score will hurt your finger and we are just talking about two pages like the home page and just above page and we have to do all of this configuration and we are not done with like graphql and all of this stuff so yeah node.js is good but not for this kind of stuff so we are going so you have to learn about express.js if you want to become a banking developer if you want to use node.js as a backend language for your website and stuff so then we we just have to click on this button so the server is up and running and now let's just go to some sort of uh blah page and it will now gives us 404 page not found let's just go to or inspector and also to this not Source come on the network panel let me just refresh that and blah 404 bad requests so everything is working the way we expect them to work and by the way you can also learn more about these HTTP status code and all of that stuff but trust me I show you more than there like more than enough you already know how to serve a Pages how to do routing in a HTTP module and over an HTTP module is and how to create your own server and all of that that's more than enough like you won't even use node.js for this kind of stuff but I just show you anyways because later if you go to some sort of interview and your interviewer asks you this question like product routing or how we can serve HTML Pages or images or css files by using just node.js not um Express yes so you be able to answer them anyways so there's a great tool to learn so the next thing which you have to talk about is something called npm nor package manager so I just removed the other crap from here and I want you to just go to this link which is npmjs.com nut.org and node.js that was.org o.dev but in this case we are working with npm.com so we have to just write npm.m npmgs.com there we go so what in the world is this thing which we are currently learning about so npm or node package manager is the world's biggest code store out there so once we install node.js in our machine so we get npm for free so like if you just write npm-v and it will give us that npm version right here and we've been using it for a real long time in this course but as I said like you don't have to worry about that for now I'll show you later so now it's later so why in the world we are using this npm so npm allows to do three things so it allows us to reuse our own code in other projects it also allows us to write our own core it also allows us to use code written by other developers some other developer previously faced some sort of a challenge or a problem and they decide to make a package on there and they just push it to npmjs.com okay so we can use that same code and we can also share our own solution like let's suppose if we Face some sort of a problem and we want to create a package on there and we can push it to npm and a lot of people will use it so yeah this is what npm is and this is the official page of npm you can donate money to them if you want to and in npm side you can found lots of great code bases but also the worst one as well and you can also found like smaller packages and you can also found a huge Frameworks and libraries like bootstrap or I don't know Frameworks or libraries like react.js vue.js express.js all of that kind of stuff is available inside this npm right here so let's suppose if I just search for like um I don't know which what can I search for I'm gonna just search for something called a Norman well you know I'm gonna just search for express.js right here okay so here you can see it will now gives us that Express exact match we have Express validator which allows us to validate or code or user and all of that kind of stuff it has something called middlewares which you don't have to worry about there for now and it will also gives us the cores which is used with this Express yes so I'm going to just click on this first link right here and it will now brings us to this express.js right here it will even gives us the basic setup how how to run your express.js servers so in Express yes if you want to create a server so this is how you're going to be creating a server and this is how you are going to be listening to it and these are routing and you can also shorten this code to error function if you want so MIT license the weekly download is that amount I can't even count how much this amount is and the origin is 14 and by the time I'm recording this value and um the size is that much and you can also see the collaborators and you can go to the GitHub repository if you want to and you can learn more about that if you wanted to okay so let's suppose if you just search for react just so react just let me just search for that or let me just search for react okay so here you can see it will now gives us like react as a JavaScript library for building the user interfaces and I have a complete six hour only of course on reactors if you want to learn more about that and react the JavaScript library for creating a user interface this is how you are going to be creating a functional component of a counter and this is the home page of react.js react.js.org and this is the GitHub repository made by Facebook meta and nowadays it is calling itself is a meta and MIT license the version and check out how many people have installed how many people have installed this is just a weekly download of react.js anyways so this is npm and now let's just install a few things uh so what I'll do is that I'm going to install something called Norman right here and here you can explore countless other things like the possibilities are like endless you can found a lot of cool repositories and a lot of crap ones as well so I'm going to search for that and here you can see it from Long uses Norman so word in the word is their package which I'm installing right here cool logo so normal is a tool that helps develop not just best application but automatically restarting the node application when the file changes in the directory are detected okay so yeah we are going to be installing there but before we do we can install like all of our dependencies either locally or globally so we can uh install this Norm on like globally like then we don't have to install it in each of our project we we just have to install it once and we can reuse this npm uh I mean like this normal again and again but if you use this flag like dash dash Dev we can just use it in in that specific project but you will not be able to use it in different kind of projects so for now we are going to be installing it by using this npm install dash dash seven all of that kind of stuff so now it's time to create a new file like so on another file by folder I'm going to give a name of like npm and now I'm going to go into that folder right here so I'm going to just use CD 11 and hit tab hit enter and now here if you want to install anything we have to first of all use npm you know I'm not going to be using this terminal I'm going to use this terminal so you guys can see everything a bit better so what I'll do is that I'm gonna double click on it copy the file path and I'm gonna just write CD you don't have to do that by the way I'm just showing you like how to do that and let me just make it bigger or not that much bigger let me just zoom in a bit so if you want to install some sort of a dependency inside your specific project so for that you are going to be using npm first of all like npm and then the project name so in this case I want to first of all go to the 11th folder and now inside this 11th folder what I want to do I want to install like uh I don't know I want to install Norman so I'm gonna just write npm and then I can either use install or I can just shorten there to use I so I can either use ins install or I so in my case I'm going to just use install and I'm going to just write like um dash dash save and what was there sell and Dev so I'm going to copy there right click on it and copy it and this will allows us to let me just write install this allows us to install or our dependency locally just in this project so we will be able to use their dependency of Norman just in this specific project but will not be able to use it in other projects okay so you will see the result just in a second so what I will do is there I'm gonna just write nordmon and I'm going to hit enter so it will just install that Norm on in my machine right here okay so let's just wait for it to install because it's not that much huge thing to install but it will take a little bit of time to install it in my machine and congratulations we successfully installed Norman right here but if we open this node uh I mean like npm right here so what are we seeing let me just zoom in a bit so what are we seeing we are first of all getting this package Json and here you can see we have this div dependency and we are just installing this node Mon and this is the current version by the time I'm recording this course and now if we open this package.json so check this out I'm going to show you what a page.json will do for us but before we do that I'm gonna open this new modules for uh folder right here and there are a lot of folders right here and these are all the folders which this package like this normal is depend on so now let's talk about this package Json file so why do we need this package.json file because it records over project dependencies and configures or projects so like let's suppose uh if we create some sort of a project and we use our different kind of packages like later we are going to be installing like if you follow this playlist so we are going to be installing like no more and we are going to be installing Mongoose Express yes scores dot EnV and all of that kind of stuff so we cannot push all of their dependencies to our GitHub repository and we want to just push our code and we want to just provide this package.json file and later and and later if some developers wants to use this project so they just have to use npm come on they just have to use npm install or just install and hit enter will install all of the packages which this project is currently using okay so you will see that just in a second but this is how we can install there and now let me just show you how we can uninstall a package from our project so instead of writing uh npm I or npm install we can just use npm un or npm uninstall like so and I'm gonna just give a name of like normal the one which we just installed and I'm going to hit enter okay so it will just remove there from our packages already so now we successfully uninstalled Norm one and here you can see there we don't have any packages nothing inside there so what I will do is that I'm going to select all of this file and I'm going to delete them okay so the next thing which I wanted to do first of all I'm gonna have to create an initial repository for myself so I'm going to just use npm in net Dash y you can provide like you know first of all let me just show you there one by one so if you just write npm init Dash y first of all it's gonna ask like the package name which you're currently creating so I'm going to just write like um I don't know Jose package or something like that and I'm going to hit enter and sorry name can no longer contain capital okay so we have to specify just wasn't like a lowercase version will be there description will be the best um pack package um anyway hit enter entry point will be just index.js file cool just come on we don't want to provide there and it will be just like um come on github.com web dev my GitHub repository keyword will be like uh awesome and Java Script or something like that author will be Hussein web dev license will be MIT and hit enter yep we want to create the hit enter and now if you do that and what are we getting we are getting this package.json file right here and it will just records all of our dependencies and it will allows us to configure our project okay so now we create that package.json file right here so the next thing which you have to do is that we have to install our normal one more time so I'm gonna just use npmi this time npm install and then node mon but before I do I'm gonna write save Dash Dev and then Norm on you can install it globally if you want to nowadays I guess you don't have to like provide this dash dash 7 Dev no more but if you want to provide it so that's cool if you don't so that's completely okay too anyways and there's another story but let me just wait for it to install so we successfully installed Norm on right here and what are we getting we are getting these Dev dependencies and our normal is right here and we are using the their 2.0.22 version by the time of this recording so now we successfully create our package uh directory right here and we install a normal so now let's just create a script for us so we are going to be writing start script first of all and if somebody used like if some other developer come to our project and they want a user so they have to use npm start to start this project so we are going to be writing like um uh not yeah we are going to be writing node index.js file okay so this is just a file if you just write like um let me just go into that 11 directory and if you just write npm start so it will also start our project currently we don't have nothing inside there so that's why it will give us this error but later we are going to be adding other stuff so then it will work now let me just add a comma and I'm going to just write um Dev right here because we're currently in the development modes we are going to be riding npm run Dev okay so then we have to use Norman and then index.js so now I'm going to show you what this node mode will do for us but before you do that I'm going to create a new file right here and I'm going to give a name of like index.js right here first of all I'm going to or I also forgot to get my uh what do we call it type and let's just set there to module and save this file right here okay so now we totally create our setup so let's just get our HTTP so I'm going to use import HTTP from HTTP module and what I'll do is that you know what I'm going to just copy the why would I write all of that stuff so I'm going to copy there and there's a lot of gibberish so you know what I'm gonna go into this HTTP module copy all of that paste it right here and remove all of these comments from here as well okay so we just have uh word server let me just go back so currently we are just importing what HTTP and we are creating a server and if we um hit that 800 like home page so you are going to be just getting like uh hello from node.js server right here but now let me just use my Nord package manager I mean like the normal dependency which we just installed so what I will do is that I already create that script for myself which is the dev script right here like normal index.js right here okay so now let me just use node uh not node but npm run Dev and if you hit enter so it will just wait for our changes okay so npm run Dev oh yeah that's because we are currently inside a workspace we gotta go into the 11th directory so cd11 and then we have to use npm run Dev and if we hit enter so it will just wait for our changes and anytime we made changes we don't have to click on this button to stop our server and restart our server we just have to write our code and test our code like let's suppose uh let me just go in here and I'm going to just write like a localhost hit enter and it is now giving us this hello from node.js server right here okay so if I made some sort of changes like uh I don't know uh I'm going to just change that to like instead of hello from node.js to like uh was good bug so now I'm going to sell there and now let me just refresh it and here you can see and we don't have to no longer click on this bound to stop word server and to restart our server so that's completely awesome so now we successfully install uh what do we call it that a normal right here inside your project and we also learn about what a npm is and how to install packages how to uninstall packages and we also learn about how we can create our own scripts right here if you want to create some sort of other script so you're going to be just providing a comma and then you're going to be providing the script name and what their script should do okay so this is how you're going to be creating this script for your project so in my case if I just write like start so it will just start our project but for there we have to start in stopwatch server but now currently we create this script for us like normal index.js so anytime we save our file so it will just refresh our server so like let's suppose if I change these bugs and let's just remove there and just watch what happened like when I sell my file I'm gonna hold Ctrl and hit s and here you can see it will just restart my server right here so if I just come to my link right here and here you can I mean not link but my tab right here I'm gonna click on it and here you can see it will just remove that box so I no longer have to click on this button again and again to stop or restart my server again and again okay so that was it about node package manager and there is one more thing I wanted to mention which is called a git ignore file you don't have to worry about that if you don't know what uh GitHub is and git stuff is so if you want to learn more about git and GitHub so I have a complete course on that as well which you can check it out totally for free and now let me just create this file which is dot get ignore okay so this this git ignore file will allows us to ignore the files once we are pushing this code to our GitHub repository so like let's suppose um uh what should I do I'm gonna go into my GitHub repository real quickly and I'm gonna create um you pause this tree right here so I'm going to just click on it and new Repository and you know what should I have to do that I'm gonna create create a private repository and I'm going to give a name of like um I sell apples okay so this is going to be my repository name I don't have to provide any description you can skip ahead if you don't know what a git and GitHub is so feel free to skip this um section of the video so if you do know that uh then there's completely okay so I'm gonna just create this repository Right Here and Now what I'm going to do is that I'm going to just copy that code from here and now let me just create a new file right here I'm going to give a name of like bugs or something and uh I'm going to open my terminal right here and right click on it and paste everything right here so it will just first of all create or you have repository and initialize to that project I know I'm going to something else but this stuff is important so I am going to go into this file refresh it and itphone log uses this readme.md file and I sell apples and now what I'll do is that I'm going to close that from here and now I'm already inside this package uh I mean like I'm already inside my folder which is this box folder right here so what I will do is that I'm going to create a touchstar index.js file right here okay we are now inside come on so I'm gonna use npm init y to create that package for us and what I'll do is that I'm going to install that Norm one more time like npm install um Dev Norman I'm going to hit enter so it will just install this Norm on inside this bugs uh repository right here okay okay so be successfully installed there so I'm going to open my visual story recorder right here let me just open that and here you can see we have these packages and these files right here I'm gonna write index.js file and console.log uh come on console.log I sell apples and now I'm gonna just write dot get ignore okay hit enter and now what I'll do is that let's suppose I don't want to push this node modules folder to my GitHub repository like this is a huge folder like there are a lot of files and folders like countless files and folders so if I just want to ignore this file I mean like this folder so for that I'm going to have to first of all write a forward slash and then I have to just write like node and forward slash uh not forward slash but um underscore there we go underscore and modules okay so now if you sell or file and here you can see that grayed out right here and now if I just use like get status and it will now gives us all of the file except this node modules right here because we ignore that okay so now I'm gonna just write like get um add and now I'm gonna just use git commit Dash bugs or something like that and now let me just push there to my GitHub repository okay so it will now push all of that code into my GitHub repository and now let me just refresh there and here what you will see is that you will get all of the points which we have right here except that no modules right here so now the next thing which I want to show you is that I'm going to exit out and let's suppose we'll lose this repository like uh or maybe we are working with someone else computer like we removed probably remove this repository first of all we have to close the from here as well let's just remove that from here remove this repository from here right here so I just lost this repository in some case so now let's suppose if you are working on someone else's computer or I don't know we just launched this repository and we want to get it again we push our code into our GitHub repository so I'm going to just click on it I'm gonna copy it and now what I'll do is that I'm going to open my terminal right here I GitHub crash course what the hell uh these stuff are important like later you'll uh push your code into your GitHub repository and it wants projects and all of that kind of stuff so that's why I'm taking a time to show you all of that trust me these stuff are important so I'm going to use get clone and then I'm going to come on get clone and uh quotes right click on it and then hit enter right here okay so it will just clone or repository right here I sell apples and now uh if we open this repository right here So currently we have this package.json file package log.json file but if you try to use that Norman right here it will not work because we didn't install that Norm on but if you want to install it first of all we have to go to isil Apples and now we just have to use npmi or npm install so I'm going to just use npmi and hit enter and what you will see is that it will gives us that node modules folder right here once again and now you can successfully work with Norman or any kind of directory or packages you have installed it doesn't matter if you install Mongoose or I don't know cores or dot EnV or anything like that so you if you just use like npmi it will take their time to install that packages which you have stored inside this package.json file right here and then you are going to be good to go so I am sorry if you think like that wasn't helpful I really sorry about that but uh believe me like you will use all of the stuff which I just showed you okay like all of their GitHub stuff their stuff is important so I'm gonna just go in here and remove that Repository delete this Repository uh I want to delete this repository I sell apples yeah I have I've read the and come on come on okay I have to type it manually oh Zane a web dev forward slash I sell apples okay so now if you click on it circle now just remove the repository oh come on so now we successfully removed that repository from there anyways so that's how you can work with npm and that was npm crash course that wasn't a crash course that's a full course on npm so I hope you enjoyed it so let's run this conversation by learning about node.js dependencies there are a lot of node.js dependencies which I am going to also show you in a second so now let's talk about these two main node.js dependencies so the first one we have is a V8 engine and the second one we have is a liberty and yeah you heard that right I'll leave you in so what in the world is a weird engine so we add engine converts or JavaScript code into machine core so that our machine can understand and you guessed it it is written in C plus plus programming language and on the other hand we have a library and libu is just an open so course library with a strong focus on air synchronous input output and it is already written in C plus plus programming language so now let me just go into a little bit more detail into the Sleep UE and then you will get to know what it is and why it is so labview is a multi-platform support library with a focus on asynchronous input output and it was developed specially for node.js and it is written in C plus plus programming language and it offers features like file system events something which we have learned very shortly asynchronous DNS something which you don't have to worry about but if you do you can check out the documentation which I already show you an asynchronous file system operation something which we already did full feature event Loops which we'll see in a second okay so that was a review so here is also a list of other dependencies by which or node.jss depends on okay so we already saw liby and V8 engines so now let's talk about these so the first one we have is a ll HTTP which is designed to not make any synchronous call or allocation then we have a c areas and notice this is a c or C programming language area for some asynchronous DNS resolution request which you don't have to worry about if you do you can check out the documentation about that then we have open SSL which is used for implementation of any cryptographic functions and all of that kind of stuff and finally we have a z-lab for a fast compression and decompression so now let's talk about a new topic which is called a thread so what in the world is a thread and I'm assuming that you've already heard this word somewhere before but you don't know the meaning of it so a thread is just each unit capable for executing code is car a thread who said that big bro Google okay so this is a thread so now what is the purpose of this thread we already learned about the description of this thread but what is the point of this thread so anytime we create different kind of files like we create a file for multiply we create a file for right to a file or read from a file or we have some sort of a grading function we have different kind of file and they do exactly different things and this is our program process and it will just create a thread for this file for this file for this file and oh for all of the files but as you know that node.js is a single threaded which means that it can only do one thing at a time why because it uses something called JavaScript programming language and you already know that JavaScript is a single threaded programming language which means like JavaScript can run one code at a time I mean like JavaScript can do only one thing at a time so what's going on like why do I show you these threads and then I show you like note this is a single threader which means like it can only do one thing at a time so the solution is something called You Guessed It event Loops so what are event Loops so the event Loop will be generated in the thread and the role of this Loop is to schedule which operation order should be performing at any given point so this is the whole point of this event Loop okay it's like event Loop is generated in each trade and the role of this event Loop is to schedule the operation like uh where this operation will be performing and when okay so now let me just show you one more diagram to clear this out so here you can see we have a JavaScript right here and we have some process so if this process is complete then we are going to be passing our callback function and you already know what a callback function is but if you don't I'm going to show you that in the next slide as well so if our sum process is completed then we are going to be running our callback function okay so what in the word is a callback function a callback function is asynchronous equivalent for a function and a callback function is called the compilation of a given task and called the helpers in preventing from the blocking of the chord but here you can see the blocking I should have colorized this blocking anyway so node makes heavy use of callback functions as we saw and we are going to be also seeing in the future like how much codex we need to use once we are writing a node.js code okay so now let me just show you how all of that will work so here you can see we have this simple code right here which do not use a callback function right here so here you can see we are just getting our FS module right here and then we just log to the console start and then we are just reading some sort of data and we are storing it inside this variable we have this test.txt file inside there test.txt file we have some sort of a data which will just show us like content from the TS test.txt file okay and then we have just converting their data to um like to string we can also provide that UTF ad if you wanted to but anyways there's other story finally what we are doing is that we are just writing ended so start read some data and then end it and this code is without a callback so the result will look like this so first of all it will start start is right here then it will wait for it like it will not show us that it ended it will wait for this process to end like it will wait for this file to read and after that it will show us this ended right here so like this is the result first of all we would have start and then it will read the data from that file and then finally it will show us their end date right here and by the way I'm gonna also show you that code in action just in a second so now let's refactor this code to use a callback function so here you can see and by the way this code is a blocking code like it is blocking our code right here like it will not gives us this ended it will first of all read this data and then finally it will give us this ended like gate will block our code why because we are not using a callbacks but on the other hand if you are using a callbacks like with callbacks so this is how our record will looks like so here you can see we are importing our FS module and then we just write like Program start and here we are reading their data but here you can also see that we are providing this callback function right here and here we are using like a ternary operator you can also part FNL sermon if you want to so if you got some error print that error other ones just print like the data which this test.txt file have and then finally we are just logging like program ended right here so in this case we just use the Callback function right here so our program will not be blocked it will first of all run this code this process will take a little bit of time so it will just push there to something called event Loop which I'm going to show you just in a second so first of all it will print us like Program start then it will print as program ended and this process will take a little bit of time so when it's done it will give us their result at the end right here so here you can see we have Program start program ended and content from their test.txt file okay so this is asynchronous and this is totally synchronous and we already saw the example of that but on that time I told you like um I'm going to expand all of that stuff later on in this course and that was a time I expand all of that and I'm gonna also give you the example of callbacks and non-callbacks function as well just in a second so now it's time to put all of that in action so here you can see we have a node process right here and we have a single thread and inside this single thread what are we doing we are first of all initializing our program then we are importing module this is going to be the second step and then we are registering our callbacks this is going to be the third step and then start the event Loop but this event Loop will only start if our code contains some sort of a logic which will take a time like let's suppose we are reading some sort of a data or we are just waiting for some uh I don't know like we are waiting for some user to enter their data or something like that like those code which will take time so it will start the event Loop and inside this event Loop and inside this event Loop we have a thread pool and inside this thread pool we have four threads right here so it will just push over heavy data like those data which is taking a time it will push it to these threads right here so threads inside a thread pool and this thread pool is coming from this event Loop and this event Loop is available inside a single thread let me say that one more time because I know it will confuse a lot of people so first of all we have some sort of a node processor right here and then we have just a single thread like this entire area is a single thread and inside this single thread first of all we are initializing our program then we are importing modules then we are registering our callbacks and then we are starting the event Loop but this event Loop will only start if we have some sort of a heavy lifting data like those data which will take a little bit of time like I don't know creating a directory or uh reading a file data or replacing some sort of a data or a pending data the things which we already covered in this course and then it will start this event Loop and once it started so it will give us something called a thread pool and inside this thread pool it will give us four threads right here so it will push that heavy lifting data into these thread pools and it will gives us this kind of result right here so first of all it will run our program so it will give us the result of those things which is not taking our time and then finally it will give us the result of those things which is taking our time so this is what's going on behind the scenes so I hope you totally understand it so now let me just give you example and then you will get to know what I'm talking about so what I'll do is that I'm going to create a folder given name of like 12 and BTS I had saying this part but anyways let me just create a file index.js right here so I'm going to save this file save a bar to this one uh I'm going to just write a blocking code okay so blog do you have a logo some sort of icon for blocking anyways we don't have any icon for blocking so I'm gonna first of all write a blocking code so I'll just write start uh Consular log start and then what I'll do is that uh you know what um I'm going to just write alert and I'm going to just write blocking or a block would be fine and then I'm going to just write it console.log off and right here okay so we have these three lines of code right here and the first one will give the star the second one will give us this alert which will block our code and the third one will give us this Android here okay so I'm going to just run this code and we cannot run this alert because we are working on a node.js I'm gonna have to create a new file index.js and not JS but HTML would be fine I'm going to use doc type HTML right here oh you know what instead of doing all of that I'm going to close there I'm gonna copy this code you know I'm going to delete this file right here I'm gonna copy this code this blocking code and I'm going to run it right here so I'm going to open my uh console right here go to my console really quickly and I'm gonna pass this code right here so we have this blocking chord so we just provide a console.log of star it will block work code and then we have this console log of n so if I hit enter so what are we getting first of all we are getting this start word here we are getting this start but it will not show us this Android here because this alert will block our execution so here you can see we are getting this block and if I hit OK and then it will give us this end so we are getting start and block our code and then if we hit OK then it will give us this Android here so this is what we call blocking code so now let me just write a non-blocking code so what I'll do is that I'm going to just write a noun blocking I'm going to delete this coding and I'm going to just write non uh blocking code right here so what I'll do is that I'm going to just write start right here and then I'm going to use something called a set timeout which you're already aware of there if you are coming from JavaScript background and then here what I'll do is that I am going to just provide like one second of duration and then alert and here I'm going to just pass like non blocking y it will not block our code because here we are passing a callback function right here okay so finally I'm gonna just write console.log of n so now let me just say over there and I'm going to copy this entire chord I'm going to refresh them and it will take a little bit of time come on and I'm gonna just place this coding right here so non-blocking code and first of all what we have is that we have a start then we have a non-blocking code which will take one second because we are providing this callback function right here and then we have an errand so what do you think will happen so I'm going to hit enter so it will give us a start then end and then it will take one second and then it will give us non-block and call right here so I know this was a horrible example so I'm going to close that and now let me just show you a real word example of this I'm going to remove this coding and by the way that involves some sort of working with the console and I know some people will say like that's another code you're going to be writing but anyways now let me just give you one more a real world node.jsd example or Jesse that's not a word by the way I just made this up without um call back okay so without a callback first of all I'm gonna use use the fs or you know I'm going to use import FS from FS right here okay so I'm going to just try to console.log and here I'll just provide start okay start and then I'm going to use let data will be equals to FS dot read come on read file and here I'm going to just provide test Dot dxt and I'm going to just create this test.txt file right here test dot txt file and hello word okay semi file and then I'm going to log there to the console like data.2 string right here and I'm gonna also finally write my end okay so our program will start it will take a little bit of time and then it will end so now if I execute this program so I'm going to hit enter and I'm going to execute this file but it is giving us this error read file sync rather than come on saying because this is a synchronous version so I'm going to save my file and it is still giving us that error read file sync test Dot I guess the directory is not found or what is going on no such a file or directory you know what I'm gonna put this test.txt I'm going to grab that and put it right here and now let me just run my code while it's freaking out I'm gonna run it and it is now giving us start then it is giving us hello word and then finally it is giving us this end so which means that it is blocking our code right here let's suppose if you have like 1000 line of code right here or a thousand line of some text right here which will take a little bit of time so it will block work code so first of all it will run this start then it will block or code at this point it will read all of the content and then finally it will give us this Android here which means that it is blocking over code so this was the example of blocking the code and we use this sync right here but now what I'll do is that I'm going to just change there to read file and I'm going to pass my um what do we call it callback function so error and then we want to get our response from this function so I'm going to use a turn operator right here if you got some error then lock that error and if you didn't got any error whatsoever so then I want you to print that rest dot to string right here okay and I'm going to remove this console log from here I'm going to send my file and uh what I'll do is that I'm going to also remove this data from here as well so now here we are specifying this callback function right here so it will not block our code so what I'll do is that I am going to run it okay so here you can see it will now give us a start then it will give us n and then it will give us two string function right here res would be fine I'm going to sell them and let's just scones along there one more time so it will not use this buffer we can also provide the utf-8 right here so UTF 8 and then we can specify our comma and now let me just run there let me clear this out and now let me just run that and here you can see it will first of all it gives us the start then it will use the end and finally it will read this code and it will give us this result right here okay so this is something called a non-blocking course so I hope that helps if it didn't you don't have to worry about it because this is just a behind the scenes stuff you don't have to worry about any of them so now let's talk about events in node.js so I'm assuming that you are already aware with the concept of events from JavaScript so we are going to be also writing events in node.js so I'm going to just create a folder give a name of like o13 not o13 but just 13 would be fine and events and inside this folder I'm going to create index.js file okay so I'm gonna just remove that and now if you want to get events so first of all we are going to be writing uh import and then we are going to be writing event uh emitter okay come on evnt and oh you know what let me just remove these curly braces from here event e m i Double T E R and it will be now we are going to be getting that from events right here okay so now we get there now let's create an instance of that event so I'll just write creating instance come on instance uh of that event so now if you want to create an instance we are going to be providing new and even emitter event emitter right here so this is a Constructor function if you are coming from a JavaScript so you probably know this stuff which I'm talking about like Constructor function all of that custom emitter I'm going to just give name of like custom emitter like so and it will be now equals to this instance now inside this event emitter Constructor we have a few methods so the first one is car let me just write the first one is card on so this on method allows us to listen slash register uh for an event okay and then we have this um whatever you call it once which will also listen slash register uh for an event but the difference between this on and once is that anytime you run your code or anytime you call this event so this event will fire but if you declare your event by using this once so that event will only fire once like not more than that it will only fire once and this one can fire like a lot of times which I'm gonna also show you in a second and then we have another one which is car uh emerit okay so emit allows us to emit or call an event so the one event which you are going to be registering by using either on or one so it will allows us to run or call or emit that event okay so these are the description so now let me just write it custom emitter uh which we are creating right here and I'm gonna be using like on in this case okay so we can call this event like as many time as we want so I'm going to just write a response and then here I'm going to just specify a callback function like name and ID and here I'm gonna just pass this callback function right here and I'm going to just log to the console this data like uh I don't know uh data or uh yeah I'm gonna just write like a user and it will be now equals to the username which you're going to be specifying and then we're going to be providing some sort of ID to it so like uh we are going to be also getting this ID so once we are calling this emitter so now we registered this event by using this on method so now we have to call this event so that we can see the results so what I'll do is that I'm going to just write like custom emitter and then I'm going to use a Metroid here okay so I'm going to just give a name of like Risk I don't know response would be fine or you can give a name of like event or something like that you can give any name you want and here we are going to be passing this video to this argument so I'll just write uh John and I'm going to just write or you know what my own name would be fine Jose and here I'm going to just write like uh I don't know 18 would be fine or 17 I'm going to provide 18 18 is fine so now if you run or code and what are we getting we are getting this user from Hussein and we are getting this ID of 18 word here it should be like there there anything like their Edge would be fine so this is how we can register our event and this is how we can call it but now let me just call it one more time so I'm going to just duplicate there and I'm gonna just change that to John and John will be like uh I don't know 29 years old and him will be equal to 10 euro come on 10 year old Alex will be 10 year old so I'm gonna save this file and I'm gonna run it and here you can see it will run it as many times as we want them to run so like we have uh user Hussein ID of 18 John 29 lx10 okay so I'm going to clear out the screen and now instead of using this one I'm gonna change their two ones right here once like so I'm gonna save this file and now if I run my file so what are we getting we are getting this user right here and use it as Hosanna ID is 18 right here and here you can see we are reusing it but it will not allows us to call our event more than once because the name is once so this is how you are going to be registering your event and this is how you are going to be calling it for here so you can either register it by using on or you can register it by using one so now you know the difference between both of them okay and you can provide any kind of logic right here like console log or reading a file or reading some sort of a data or creating a directory or anything like that and then you can specify these arguments you don't have to but if you wanted to you can specify these arguments right here okay so there's events in node.js alright so the final topic of this course will be about streams in node.js so what are streams and why do we care about streams now first of all let me just create a folder give a name of like all 14 of streams and inside this folder I'm going to create index.js file and now here we are going to be talking about the streams so what in the world is a stream and why do we need it so in node.js streams are a way to handle reading and writing a data they provide a way to work with a large amount of data such as reading of files or writing of files or maybe receiving a large amount of data over a network connection without loading the entire data set into a memory and stream can be readable writable or both and can be used with a pipe data between different sources and destination and by default it accepts these um 64 kilos of data so you can either write it or you can either you can either read it or you can either write it okay so now let's create or first of all let's just get over function from their events not events but FS module so I'm going to just write create a read and then stream so this is going to be the function name from the fs module okay so now the next thing which you have to do is that we have to generate a lot of data so that our buffer American reader so what I'll do is that I'm going to just create a new file I'm going to give you a name of like let me just go back I'm going to give a name of like I don't know data.js would be fine it will allows us to like generate a lot of data I'm going to import the fs from FS module and now I'm going to just zoom in a bit so I'm going to just use for let I equals to zero is less than some sort of a long number right here and then finally I plus plus so we are incrementing our i fs write files not come on right file synchronously so we are going to be creating this file like data.txt file and we are going to be rendering this kind of content like I and we are going to be getting a new line and finally we also have to provide this flag of a right here so now if you sell there it fell it read over through all of these texts and it will generate these amount of um numbers right here so just wait for it I'm gonna just zoom out a bit and here you can see we don't have no file nothing inside there so now if you run this code so it will take a little bit of time because it is generating a lot of data and finally it's done I'm going to go into this data file and here you can see all of these numbers like there are a lot of them and it will take a lot of time to read or write this data okay so I'm going to just close them purpose of this file was to just generate their data.txt file for us anyways so now we already get our function right here so next thing which you have to do is that we have to create our stream so I'm going to just write like cons stream and it will be now equals to ice cream it will be now equals to read uh create Radiance create read stream there we go and then here we are going to be specifying over data.txt file the one which we just created with a lot a lot of text right here okay and then here what we have to do I am going to just put a watermark like a high Watermark right here so what in the world is this High Watermark property or flag if you want to call that so the high Watermark property controls the size of the buffer or we can increase that or we can decrease that if you want to so in this case like maybe I'm going to just provide oh you know what uh I'm not gonna be providing that right now I'm gonna just remove that from here in this case and now here I'm going to just write a stream uh stream which we've just created and then I'm going to use my on event right here so anytime you call it we want to get the data and we want to give a label of that event of data and I'm going to pass let me just go in I'm going to just pass my data right here and I'm going to lock that data to the console right here okay and finally we can also get a error event but that's another story we will just take care of that in a second so I'm going to just go back like so go back and I'm going to run this file right here so here you can see it is now giving us this buffer right here so it is saying like forehead add four zero or more bytes right here so that is giving us this huge amount of buffer right here but it is not giving us all of the entire data right here so we can also provide that High um Watermark or something which I just say so high water Mark will be now equals to like this uh huge amount of numbers like this would be fine all you know I'm going to just provide four zeros right in there I'm going to clear the terminal and I'm going to just run this code right here so here you can see it will now increase there does it increase it or not I bet it didn't anyways so I'm going to just close that so now it's time to read the actual data from this uh stream right here so I'm going to just comment this line out and now let me just read this entire data so I'm going to just write constrreme s t r e a and there you go and it will be not equals to that same method which we've just import and here we have to specify that data.txt file and here we just have to specify encoding and UTF add with no dashes and with nothing but we just have to specify this UTF airport here so in this case it gives us a buffer in this case it will just encode over data to UTF edit and it will give us the actual content so I'm going to just save this file and I am going to run it okay so here you can see it from now read all of their data from that file right here so here you can see it run and read all of that data which we specified inside this data.txt file so I'm going to just go at the end right here and here you can see it will now read all of that data from this file so there's powerful so streams are very very powerful in node.js so yeah you don't even need to work with the streams if you don't want it to like I barely work with stream like not damaged but just a little bit anyways so thank you so much for watching if you like this course so please check out my other courses I have a lot of courses on friend development and also with back-end development and this course was the part of my month stack course uh which will be like which I already show you this let me just show you that one more time so which one is node.js let me just show you there so we are currently learning this merge stack right here so we are done with this node.js the next thing which I'm gonna do is that I'm gonna create a course on this Express yes we which is a superpower for this node.js okay like you will love expresses you will love Express yes I promise and after that we are going to be jumping into mongodb for our databases and we are going to be also learning about something called Mongoose and I already have a course on there if you want to check that out you can use react.js without even this tag if you want to or if you want to learn it for just specifically this tags so this course is like more than awesome which is like seven hour of content or something like that and I've created like 10 project and I'm just creating more and more projects so thank you so much for watching and this node.js was the part of my course next if you join me or if you want to join me we are going to be learning about Express yes
Info
Channel: HuXn WebDev
Views: 12,990
Rating: undefined out of 5
Keywords: node.js, nodejs beginners, nodejs for beginners, learn to code, nodejs, how to connect nodejs with sql server, how to learn programming for beginners, how to learn coding for beginners, node.js bootcamp, learn nodejs, how to become a software engineer, node.js tutorial, coding bootcamp, web development for beginners, nodejs tutorial for beginners, node js for beginners, free bootcamp nodejs, how to learn programming, learn node.js, nodejs 2023 course
Id: EsUL2bfKKLc
Channel Id: undefined
Length: 176min 29sec (10589 seconds)
Published: Wed Jul 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.