Node JS Tutorial | Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back in aliens my name is Devin 20 and this video will talk about no chance so we talked about what is no J's and white is getting so famous to understand this let's start from somewhere else see let's talk about JavaScript first and we all know JavaScript is not the best language available nowadays right so we have so many options right we have Java Python JavaScript I mean of course there are other language as well but then JavaScript is getting that edge so you can find the JavaScript in top three languages but why let's go back to 1995 when JavaScript was created now javascript is going to complete its 25 years right so in 1985 when javascript was created it was basically a web browser language which simply means a client-side language so javascript was used only on the client-side on the browser's right and all the other languages like Java Java was released in the same year but then the other language is like let's say Python or C C++ they were used everywhere on the machine side that means you can use these languages for the standalone application you can use this languages for the web applications these opposite applications right but if we talk about JavaScript it was a browser language and so if you want to be the website you can use HTML for designing you can use CSS to make it more attractive and you can use JavaScript to make it more interactive and that was the only use of JavaScript way back right so and that's why I was not much interested in JavaScript because I was not into different in part I was more into the back-end services how javascript has a programming language okay so first of all HTML and CSS is not a programming language lot of people think that but javascript is actually a programming language which works on the browser so how exactly you can run a programming language on our browser so of course right it's a language which has to be converted into a language which can be understand by computer or this or the machine and that's where we have a engine in between so JavaScript as a language as a dollar part we write code right we so we build the application in JavaScript which will work on the browser of course and the browser has to interpret browser has to convert that code into machine and that's why we have a concept of JavaScript engine no JavaScript engine will convert your javascript code and we'll put it into machine code okay maybe with the help of compiling or with the help of interpreter okay but do we have only one JavaScript engine that that's not the case we have many web browsers right we have Chrome we have Firefox we have movies to have Netscape and now we have Safari we have opera so when you have all these browsers of enable they use different engines they use different JavaScript engines now it's their choice which one - which one to use right now the first engine was spider monkey so spider monkey was developed by at Netscape basically by the creator of JavaScript itself right and it was developed in ten days so the entire JavaScript language with the compiler or with the engine was developed in ten days that straight right so Netscape was using using spider monkey but then we got some other engines as well now since we have different browsers now one of them is Internet Explorer it was using chakra and then we got chrome soft us after so many years we called chrome now chrome has built their own JavaScript engine which is called v8 or Microsoft edge is rebuilt using chromium based browser so basically it uses v8 now so v8 became the most popular JavaScript engine so yes we have spider monkey which is still famous we have v8 and then we have other engines as well in fact if you can just Google the JavaScript engines you will see a list there and you can just explore them but the most famous now is v8 but why we it got so famous it's because v8 was converting the code back into a machine code which makes it much faster it was having some internal companies which was making the JavaScript engine faster and that's where some people thought hey now javascript is getting faster so JavaScript parsing is getting faster can be used JavaScript not just in browsers but everywhere now since JavaScript was easy we had a big community of JavaScript now can we use JavaScript everywhere and that was the question right so they thought can be used Java for mobile application can be used javascript for desktop applications can use javascript for server of course not the entire committee was thinking in that way can use JavaScript on the server but then someone thought hey let's try it and that's where they used v8 to build a runtime environment which will work on the machine okay and that they named it as no Jesus not know Jesus is not a programming language is not a framework it's basically a runtime environment where you can run JavaScript so using node.js you can run JavaScript on a standalone machine not on the browser but on a physical machine now using node.js you can build standalone application I mean that's awesome right and then some people thought hey can we use JavaScript for mobile application yes and that's why we caught some frameworks which using which you can build applications on mobile so basically cross-platform applications you can build the same application which works on iOS which works on Android they're coming back to node.js so node.js is a JavaScript engine which runs your JavaScript on a physical machine right okay but then what if we can just add a HTTP component or there should you be module inside Noches that means the moment you have that HDV power you can use node.js on the server side and that's what that's what we got we got node.js which has a lot of different modules some are in build some you can download from the internet so we have a big community now they build this modules and you can use them and one of the module is HTTP now that means you can use node.js as a server side component so javascript can be used on the browser JavaScript can be used on the on the server side and that's where we have a concept of full stack development rights are in full stack we have front-end we have back-end then we have the database as well now you can do everything with the help of JavaScript so front-end we had a JavaScript before and thanks to the new frameworks we have react we have angular so that's different which takes care of the front end then we have the back end which is no J's runtime environment but hold on do we have do we have the framework to build web applications now if you are coming from the job background we have spring web right we have spring and spring MVC now if you are coming from let's say Python we have django what framework do we use in in JavaScript node.js is not a framework right so we have to use some framework and that's why we got expressjs hot express in short so we got node.js which is the sour side one time we got Express Reyes which is the which is the which is the framework for developing web applications and then we have different end frameworks which is angular or react oh and we can use MongoDB as a database so we can use mean or moon stack to build the entire web application from start to end using one language which is JavaScript and - the reason Java switch is getting so famous now and that's the reason why node.js is getting so famous in fact node.js has one more thing called NPM which is no the package manager now if you are coming from Java background since I'm from Java background I can take more examples of it in Java when you build an application you use some external dependencies let's say in your application you want to use some database you need some connectors in between maybe you want to work with files you want to use the image compressor you want to use video converters so you can use those jar files right you can use those dependencies luckily in node.js we have a concept of NPM which gives you all those packages which gives you all those modules right so they have the huge database of modules you can use them you can build your own modules as well and that's that's the reason why no cheese is so powerful so just to reiterate what is no cheese no J's is a JavaScript runtime environment which uses the v8 JavaScript engine okay maybe in future we'll be having all the browsers working on v8 it is that powerful so so you can so if you love node.js so you have to thank first JavaScript developers they have to thank Google as well because they have given you v8 and of course you have to thank the no js' team welcome back aliens my name is Devin Reggie and in this video we'll see how to install node.js and Visual Studio code on windows machine now see you can do this code on different OS you if you are using Linux or Mac or Windows it works on all the systems so no J's and reuse code works on all different platforms and first of all for the if you want to continue with this series if you want to see how the basics of node.js you can use any IDE which you prefer I will be using visual vs code which works perfectly for nodejs so you can use that and it also has some extensions you can use now first need to verify in your system do you have no jails see the thing is when you install when you work on some projects or when you have when you install some software's there might be a chance that those projects or the software's need node.js as a dependency so maybe in your machine you might be having node.js by default so how do you verify so you can just go to CMD Oil comment on maybe terminal or command prompt and here you can simply search for node V which is version so if it is giving you the version that means no T is installed so you can continue with that otherwise it will show node is not recognized so that's perfect right so in this machine I don't have noticed another thing is even if you have not installed in your machine it might be a old version and we want to work on the new version so at least the second-last was and not the latest so let's install node or let's upgrade note if you have so the best option is if you already have not in your machine just remove it so that you can you can do a fresh installation so what we will do is to install node let's go to the official website of node.js so let's go to browser and search for node.js and this and it will give you a list of websites and of course we need to go to the official one which is node.js dot o-r-g and if you can go there before installation I just want to show you the website it tells you a lot of things the first thing it says is no J's is a JavaScript runtime okay so which we have talked about this in the earlier video built on Chrome's v8 engine so it is build on v8 as we have discussed and and if you go to the about a section of node.js you can see it gives you a sample example it explains the concept it supports why it is even true and why it is a synchronous and you can have a look on look look at those things but we want to go to the back to the home and you can see we have an option of download for Windows now depend upon what OS you have if you are using linux mac and windows it works on all the different OS and if you can just click on the Downloads tab it will give you different options there are two things actually here with one is LTS and second is current LTS is long-term support whenever you go go with the latest version that means it will having a longer support we want to go for the current one and while you are learning you can choose any one right you can choose LTS or you can use into is current one but yes when you when you are going for a production software when you are building the the DL projects then you have to think about these versions now when you click on current of course depend upon which also you have you can choose I'm using Windows and 64-bit so I'll be using 64-bit MSI so you can just click on this and it will download the node.js for you so as you can see I have the node.js setup in my machine and we just have to install this now how do you install it it's very simple right you just have took the double click it and it will give you some options we have to say next you don't know about and we don't have a choice we have to accept the agreement say next and this is a path you have to mention of course the default works say next nothing much here it says add to the path yes it will be adding to the path let's do that ok now this is important this is important tab so so it says automatically install the necessary tools and note that this will also install chocolatine not chocolaty is NP m4 for windows and we have talked about npm before right so it's a very handy tool so it's like I would recommend you to take this and say installation it might take more time so when you say take and it will not download a lot of different tools for node.js and if you are serious about this make sure that you have all those tools available with you say next time click on install yes now to take some time for the installation now as you can see this setup is complete you just have to click on finish now since I'm doing this for the second time it has not given me extra prompts which I got in the first installation so you might get those prompt about installing the chocolaty and it will take more space so you just have to say yes and click on install now once you click on finish you can see oh it is still continuing but that's the prompt I was talking about ok so it is downloading the extra tools so first it will get the latest version of chocolaty and by the time it gets the installation let's head towards the website of chocolaty so let's go to the website of chocolaty now if it's also chocolaty this is a website so it's a package manager for Windows so we can use this tool for the extra modules you want ok so everything is done it has installed this software click on enter and I guess we have the setup ready now but how do you verify this and for this you have to go to go back to a command prompt and say node B and O it is still not installed maybe we have to restart my CMD command prompt and let's say we and you can see that we got the node version so no it was your starting point I didn't which is that which is the current release so you can use that ok now once you've got node and I mean depend upon when you are watching this video maybe have take after one year you might be having a new version so even if you have a new version it might work the thing is it goes for three different releases and not two different version systems in fact we'll talk about that in detail later but we have the bug fixes which so the last part will change depend upon the bug fixes the middle part will change based on the money increments and the first part will change be depend upon the measure increments and whenever you get a minor increment or patches it will not change much in the system but yes if you have a major version it might be having some new features it might remove the old features so you need to check those as well okay now once you got node of course we want to write some code we want to run the first code of course whenever you learn a new language a new framework or a new tool the first thing you do is you don't hello world we want to do that here first but of course will not be doing that in this video but we need some Sophos for that where do you like your code and for that you need a editor you can use notepad as well okay but we'll be using some advanced editors which is one of them is vs code in this machine I do have vs code and if you want to install vs code just go to Google search Mafia's code that's the website download this set up it's very easy to install the Avs will actually click on download for Windows and it will do the installation now how it looks like so I do have the vs code set up here let me open that and that's how vs code looks like which is Visual Studio code this this is where you can create folders you can create files and you can run it in fact you can use envious code for all different programming for all different frameworks it supports majority of them so you can use it so that's it from this video if you have talked about how to install node and how to install vs code in fact they are not in this relation of vs code you know how it works so that's it from this video in the next video we'll see the first code or we'll write a lot of oil we'll do some addition of two numbers just to understand how this Tool Works welcome back aliens my name is Devin ready and in this video we'll start with node.js so we'll write our first code and we all know right when you start a new language a new technology the first thing you want to check is by printing hello world that's the in operation of that technology right even when you when you work on blocks and the first thing I do is I print hello voice okay anyway so when you let's let's write a code to print hello world on the console so when I say concered I'm not talking about browser console I'm talking about the command line so we will write a JavaScript code which will run and it will print the output on the terminal of your machine OC and or the command prompt that's that's interesting right so notice that we'll also see how to add two numbers so that we can see that it's above its we can write JavaScript code and you can add two numbers just to just to start with it cool okay so that's not our intention to learn how to add two numbers but our intention is to check can we print hello world can we add two numbers and then we'll build a simple HTTP service see what happens is when you talk about node.js it is very famous in the world of back-end development right and we need to see some code of course that's not the code which we write on the person server but at least we can get started so without any do let's get started so I will open my visual studio code this is this is the idea we are going to use you can use any idea which you prefer in this let me create a so let me just go to terminal and let's do all these thing in a particular folder so in my terminal I will simply just increase size of the terminal so in this term now I will say let me get a full a super folder and we'll name this folder as let's say node X node examples and let's switch to this particular folder and in this folder I want to get a file now how do we do that so just click here and let's create we have to open that folder first open folder okay that's our folder click on select folder and here you will see your folder is open let me close that now in this particular which is node X I want to create a file see the name of the file is not important what is important is the extension now since we are working with no where Jas is JavaScript so the extension has to be JavaScript here so let me create a file name called as app dot j s you can go for any file name this is a very common common name now the only thing I want to do here initially is I want to print hello boy right so how do we do that do we have to get any functions if you are coming from other language like C C++ Java we have this habit of getting functions right but here we can run this crab diet place so we can directly run the code which is console dot log the same goal which you write on the browser's and here I will print HelloWorld nothing's nothing more just in a world and what do you think will it work I mean first of all how do we done this code so we have it on the code it's time to run it right so let's go back to terminal and I mean this terminal now and I want to run this code now the way you can do that is by saying node there's the come on you have to pass and be the name of the application which is app dot dot J's which is the file click on enter and you can see ricotta Doughboy so that's white we can write our first hello world code like this the next thing I want to check is how do you add two numbers because this is where you will use variables right and and we all know Java screaming language so it should work here well just check how that works let me just create three variables I will say bad we can also use that ladies debate away but let me use where here where equal to seven and where B is equal to so those are the two variables I have I want to add this to so I will say C is equal to a plus B a very simple calculation and here I want to print the output is and let me print C that's it and you have a choice of using a semicolon if you can skip it that's your choice if you are coming from Java background you want to use codon there for semicolon if you are coming from let's say Python you want to avoid it so JavaScript has both the options and take the same option is there with Python as well so I will say enter and you can see the output is 9 so this works so you can print hello boy you can add two numbers but now I want to do something else and that's something else is I want to create a simple HTTP service in in node.js sin is famous for web development right so I mean of course you if you are running or Gia's maybe that's a target to learn web development and build backing services now for that you have to first understand what is web service or what is web applications say you have a client which is your browser and then you have a web service which will be on a server now this server need to have a unique IP address normally whatever server you access may be it may be google.com I mean Google server or Facebook server every server will have the IP address and the only way to reach that is with the help of IP address but then they don't know about the IP addresses right when you go to Google you don't mention the IP address what you mentioned is the domain name and then we have some Dana servers in between they will manage this naming conversion so when you say google.com they will send request to a particular IP address so we have the entire table there so basically if you want to access any service or any server you need to enter the IP address but then one sawa can have multiple services right so HTTP is one of the service maybe you want to access the FTP service maybe you want to access the SMTP service so every service will have a different port number so let's say we have a server maybe we have multiple servers every server will have the different IP address and each server will have multiple services and how can we differentiate between these services is with the help of port number so if you want to reach to our HTTP service our particular server you have to mention two things the first thing you have to mention is the IP address then you have to mention the colon and then you mention the port number so whatever service we will create we have to do or both we have to go for the IP address when you want to run this and then you have to also mention the port number okay that that looks fun let me just remove this so this is empty I will save this and I will go back to my browser because that's where I want to run this code right so I will just right click on my pro chrome and here I will say new community mode here I will enter the IP address which I want to hate so the the service is on the same machine right so I can go with this address wall you can also mention localhost that's your choice but then only doing this will not work you have to also mention the port number right so I will say colon and then you have to mention the port number but which port them I will be using so you can so you know if you go for a 220 tutorial online they will prefer 9,000 but since I'm coming home shower background I have worked on Apache Tomcat which by default gives you 80 80 and for me it makes much more sense to use this port number but that's your choice you can use any port number which you want you can also use 8,000 won or eight seven eight eight seven six that's a choice I'd but use something which is easy to remember and nine thousand is easy to remember some people use three thousand but let's go with 8000 I love that port number and when I say enter so you can say we got four not four that's the cooler now we got error because we don't have this service running so let's create a service so let's go back to visual studio and here I want to be the service so how do you do that actor is very simple you just have to create use a function called create server now create surveys of is a function which will give you a service which will create a service for you which will get a sum of for you and this server takes a function so whenever you call this server it will execute a function and let me say function now if you're coming from JavaScript you know what I'm talking about but this is we are passing a function a function and this function will accept two parameters now the first parameter is something which you received from the client and then the second parameter is something you want to send to the client right so when you have a client-server architecture applying sensitive questions are sends the response so you need two objects your request and response well there was extra semicolon then it may complete that here okay so we've got a function here and then this function will will do something so we have to send the response back to the client right now how do you send others falls back to the client so for that you have to say re s dot and you have to mention a function which is right and in this double quotes have to mention something so let's say I want to print welcome back alien that's it I just want to return this on the on the browser but will this work let's try so let's go back to our command line and here I will say node app J's but we got an error it says create server is not defined or that V R we are defining it here right the thing is create server is a function which you are calling right so we have to mention where this great server exists so we have to we have to call it from somewhere so create server is a function available in a module called HTTP now what is module see when you talk about JavaScript or when you talk about a node.js it has multiple modules available now some of the modules are in build and some of the modules which you can create so it helps you to achieve a concept of modularity where you can have different components working together so let's say if you want to build the application connect these three modules build an application something like making a prism right just mix it up and you will get something new so we have to use a module and the module name is HTTP now how do you include that module how do you fetch that module now for that you have to first create a variable let's say so DP and you have to accept the module right so for that you have to use a function called require and in this require you can mention the module name which is HTTP serve to mention require function and you are saying hey I want HTTP module which will which you can save your in HTTP but this name can be anything doesn't matter but it makes sense to use this now once you've got your module ok record some warning here it says ok because we are not we are not using that HTTP yet so let's use it so here in this create server you need to if you want to power create server you have to use HTTP dot blade server because great summer belongs to a should HTTP our job is done let's try now let's go back to our terminal let me just clear the terminal first and let me say node app dot J's enter ok something happened you can see there is no error now and if I go back to the browser and if I say refresh ok so you can see it is trying to connect and record for not for again that's weird that's weird so what went wrong same thing is we are we have free to this server but then we have not mentioned the port number so this server has to listen to the port number which is 8080 and do that you will simply say listen after this bracket after this have to say dot listen and mention the port number 80 80 and simply say safe I hope this will work this time let's go back and say no dab Dargis okay there's no error and this time you can see you're not even getting the next prompt it is simply this waiting for your your work to be completed let's go back to the browser and say refresh I hope this will work okay something is not working okay what went wrong so go back to our code see when you say our D is dot right you're saying welcome back aliens what if I say this is walking okay so this is a to send the welcome back alien to the client but it is not sending it the thing is you may want to send multiple statements so it is not just one statement if you look at the browsers or if you go to any website you will see multiple strings that multiple content are some are images some are strings some are text or somebody else are numbers so you want to send all those things together but in this case I'm simply saying one one statement so my server is actually waiting for more statements but we have to inform the server hey that's the end I don't want to send anything else so you have to also mention end at the end so when you don't want to say anything more to send after doing the changes let's reach out the app let's go back here and record the output so as you can see we got the output as welcome back alien and that's how it works right so we have to say and we have to say we have to mention end here otherwise if you just want to send one statement or instead of using end you can actually write end here that works that was in fact you can also mention the header several Center responds it's always better to have I mean of course you can send the data but also send the header information maybe something like these status so whenever you get the response do you have to sign status 200 when you get either you can send status in for hundreds so for not for is file not found and then they are different type of others as well now how do you send the header so you can say are the yes dot you have to say it right and in this bracket you have to mention two things you can mention the status and you can also mention the quantity type you want to send so the content type I want to send is okay so I will say content type txt slash HTML so basically we are saying whatever I'm sending is of type HTML and Industry text HTML some time you want to send the data in JSON format so you can mention the jism format there or you can you can mention XML format there so that's your choice what you are sending and if you are coming from other languages like Python Java you do the same thing that as when you mention the content type when you work on the web service so that's it from this video well we have seen how do you get things how do you get started with nodejs creating a simple hello world code then addition of two numbers and then seeing a simple web service welcome back aliens my name is David Mindy and in this video we'll talk about how node.js works see when you say no js' we can build the applications in JavaScript which we can run everywhere not just on web browsers that's right when javascript came into picture it was mainly for the web sites so if you want to make your website interactive that's why we have used to use javascript and then it used to run on web browsers but now we want to run javascript on on desktops we want to run javascript on mobile application and as a server as well and that's right we can build the back in the applications in node.js so that means we can use node.js as a back-end server but that's how it works to understand how nodejs works as a back-end server we have to first understand how web server works see when you say web server it will be having a web application right and then that web application will be accessed by a point now this point can be a web browser this point it can be a mobile application now when you have a web browser that means a web browser will be expecting a HTML page but let's say if you are requesting this server with the alpha mobile phone that means you don't want the entire application you just want the data example let's say if you have a cricket's code app on your mobile phone now this app will have everything they entire layout but then what it will not have is it is a real-time data so let's say there's a there's a match going on you want the real-time data and that data will be given by this server so that means we have a server and then we will be having a client now in this case let's say you have one client and this one client says hey I want the current status that means I want to know the score of the of the match the current score now in this case this server will say ok I will give it data but then from where this server will give you data maybe the server is requesting to some other server to get the data maybe this service is getting data from the database maybe the server is getting data from the file system now if you if this server is requesting to some other server or to a database it will take some time like maybe one second or two seconds or maybe half a second see if it is a very simple task if you want to add two numbers or will say ok that's a number so if you want to add two plus two then size 4 but if it is an i/o intensive work where a server is requesting to some other resources that's a tricky thing right because if you have once hour which takes three seconds that's tricky right because if you have one client and one client think 2 or 3 seconds what if you have multiple clients what's like what if you have 10 clients if you get requests from 10 clients that means to respond to the tenth client it will take 30 seconds I mean that's that's weird right who will wait for 30 seconds how do you handle this how do you handle multiple requests at the same time now in this case if you have different type of servers example if you talk about Java now in Java we use a tomcat server now if you don't know about Java that's fine just try to understand the concept behind it so Java is a language if you want to build a web application you need to use a server which is Tomcat the tank it will say ok see I can handle multiple requests now how it does that you will be having a concept of threads so any Tomcat server will be having multiple threads so every time a client sends a request the Tomcat server will assign one thread to it what if you get second request you will be having seconds well so if you have 10 requests you will be having 10 threads running in parallel that means if if a request takes 3 seconds every client will take only 3 seconds not 30 seconds ok that's awesome right but then by default Tomcat will provide you 200 threads oh that means we can only have 200 clients what if you have 210 clients the last 10 clients will have to wait for 30 seconds what about seconds it is but they have to wait right because you have a limited number of threads but then most of the cases you can increase the system speed right but that's a different thing so you will be having multiple threads now coming back to node.js in no chance we'd run JavaScript right and JavaScript language don't have a concept of multiple threads oh now how do we achieve that now first of all when node.js was created they had a choice all right either they can introduce the concept of threads inside JavaScript okay but then it will lose the meaning of JavaScript right it will lose the real sense of JavaScript so they went for the second option which is let's not use threads but then how will you handle multiple requests so let's say if you are using a web server which is note which is built using node.js and you if you get a request no Jess will say ok it's a way simple request I can return it example 2 plus 2 as is 4 but what if the server is responsible to send a request to the other server and get it data now in this case when client 1 comes to this server will say hey I want the current score now in this case no J no J's will say ok I'm handling your request no J's will have only one threader so that that one thread is has sent a request to the other server it is waiting for the other server to respond him it will take 2 seconds what if in that time you've got another request from client to the client to has to wait for the client one request to get over when client 1 request is over then then so will say hey client it is your turn now client will say ok I want the same thing maybe the football score maybe some other games call now in this case I will take another 2 seconds or 3 seconds what if in that time duration you have 100 requests of course not everyone will wait so is it legs able to use node.js in as a web server ok but they have a solution for it ok don't worry so what not--are says is okay I'm a single to the application that means I can only accept requests I can do that right so no J's can handle request but what no J's will not do is waiting for the server to respond the other server to respond so the single trader will not be blocked for a one-line or for single client okay but then how it will do it how it will work so no J's uses two concepts one is a synchronous and second is non-blocking i/o now let's focus on non blocking first now what is not blocking i/o means is whenever you get a request of a client the single thread will will send that request to someone else the current thread will not be busy working with that request okay so let's say we have some we have some workers there who is working with working for this server so no J's will say hey worker I got a request go to the server and get me the response welcome we'll say ok I will do it the second client comes and then the node.js single thread is still open while it is still accessible second client will say ok I want is football score the this server will say hey second second worker go then and get me this code right or get me a data that's how it works so no J's uses the concept of non-blocking i/o which simply means it will not wait or it will not block itself for one request yeah but then what if a client says hey I want to find a factorial of this number and then it has multiple calculations now in this case when you have multiple calculations it's a complex task white which has to be done by this thread and that's where we don't normally prefer to use node.js for CPU intensive work we use node.js mostly for the i/o intensive so whenever you want to interact with the external system maybe database maybe file system or maybe it work that's where node.js is so famous but then we have one more question what if client sends client one sends a request to the server and the server says hey worker go and fetch data and then this single freeze is accessing decline to write or working with Klein - what if the request or the response of client one comes from the other server how it will execute the remaining statements and that's why we have concept of a synchronous we have a concept of callback so the moment you get a response from the data server or from the database you will execute a callback function okay so that's like--that's that's why it is called a single you don't understand this more when see once you see the practical video right then just to get the idea we use a concept of synchronous which is using using callbacks and we have a concept of non-blocking i/o okay but then we have one more question if nodejs doesn't support multiple threads how we are able to create this workers because maybe you will be having Phandroid workers what are they if they are not threads see to implement this nodejs uses the concept of libby UV now Libby UV is a special library built for node.js but then we can use it for other applications as well it provides your concept of non-blocking i/o now it is built in C language which which uses the system kernel and kernel has multiple threads so in node.js you are not using multiple threads but behind-the-scene your kernel is implementing multiple threads so this workers which I'm talking about are threads so when you send a request to the server nodejs it will be single threaded but behind the scene they are using multiple threads and that's what makes node.js fast and flexible welcome back Ariane's my name is 720 and in this video we talk about NPM so I hope you are enjoying these series on node.js and one of the most important topic in node.js series is NPM NPM stands for node package manager but why do we need it first of all going to talk about node.js it is very famous right now it is famous because it provides you a lot of different features one of them is non-blocking i/o which we'll talk about that later and the other one is NPM the package manager see what happens is as a developer when you build applications when you build websites or whatever type of application you build you need to use some external libraries you have to use some external dependencies so you can do everything from scratch right you can write the entire application by yourself but then we are living in a world where you need you need to do things faster plus there are certain things which are already there right so example if you want to build if you want to find a way to connect with a database right so you can you by yourself you can build an API which which will help you to connect your occasion with the database maybe you want to do some validations for the objects so let's say you you have an object which has other properties and you want to violate it now one way to do that is by building a module by yourself which will right at your object so those are ways right so you can build everything by yourself but then when you have a common task when you have a common problem since we have millions of developers and they are building the same thing which view you are doing so why can't we create a module which will be used by everyone so let's say I'm solving a big problem so what we can do is we can we can convert that into small problems right and then every problem or every solution will be one module and maybe what I can do is I can I can push this module to the repository so that the entire world can use it right so when you build all the application will be having some power and features right so you can put that in modules in the same way when I talk about node.js node has lot of inbuilt modules one of that we have already seen which is HTTP but what if you want to work with file system we have a module for that what if you want to work with cryptography we have a module for that so node.js has some inbuilt modules but what if you want extra modules what if you want to use someone else modules and that's where we have a concept of NPM which is node package manager now this concept is there in multiple languages when I work on Java we have languages there we have dependencies so I can use that so we used to use maven there or Gradle to fetch that but there were different approach now in terms of NPM it has millions of packages available that's right so last year when I when I saw the article if they have mentioned that last year it was they have completed completed 1 million packages on on NPM that's that's awesome right so that means when the moment you have a problem in your mind there is a solution available and that's the awesomeness about NPM so basically you can use all those packages so you can just fetch them install on your machine and use them and you can also push your packages so let's say if you have a module which you have created so you can push that module to the github and the entire world will use it and that's awesome now let's go to the website of NPM to get more information so this is the official website of NPM which is NPM J is calm and the the good news is NPM ok good news or bad news I'm not sure but NPM is now part of github and github is now part of Microsoft so so you can see it says build build amazing things using in JavaScript so it will take you from somewhere to everywhere using NPM that's what they promise so it has multiple packages we had to focus on the free one so this is the NPM free one which we are going to use so come back here now when I say it has millions of packages how will you get those packages now first of all way to search for the package so let's say I want to search I want to do some socket programming I can search for socket dot IO now using socket dot io you can create real time service we will timed it out data sending it outside so you can see you can use this socket IO and look at the weekly downloads and that's so famous right so it says around 15 million per month that's huge number of usage one of the package which we are going to use in this code is maybe a MongoDB we are going to use MongoDB for connecting connecting PTT base so this is the official one so you can use that and soon we are going to use Express J's now Express is a framework for web development in in Java in node.js so if I go back to Express that's what you say this is fast uh opinionated minimalistic web framework one node ok we'll see that this is a simple sample code but then how will you get this module in your project or in your in your machine that's where we have a concept of we have to come we have to execute this command but we will do it of course you can do it in normal command prompt but then if you remember we were writing some we were writing a code right before in the same terminal here two things which we have done before it has the commode have to pass to check the npm version so yes when you install NP when you install node.js you will get npm as well so here if i want to install that I will simply say NPM install Express this is the comedy up to execute to install packages that set and once you install the packages you can use that in your in your project so it says installing Express so it takes some time but then after is how will you use it the way you have used st TP now HTTP is a inbuilt module in node.js and that's why we don't have to download it from from the from NPM so you can simply say the quad and you can mention the module name the same thing goes for Express so just mention a require expense it will give the Express framework and that's it that's how you install it I guess there is a error no okay so we got Express so you can see in our modules folder you will see Express here that's right so we got Express but then we have only installed one package right why we got all these other modules so the thing is when you want a module maybe that modules dependent some other modules so that's what you say so those are these secondary dependencies so you won't Express Express once all of the modules and that's why you call all the modules here so likewise you can you can download multiple multiple modules the way just doing the same thing right one thing interesting here is if I go to node.js and if I minimize this on node X and minimize this there's one more one more file which is package log Jason again we'll talk about that in detail later so we have the lock Warren and then we can have also normal version which we can edit now this is where you mentioned what you need in your project so which modules you need so we have we have used Express so let me just search for Express here and you can see we are using Express version which is four point four point seven point one it will give you the exact version so there are different ways you can mention the version which you want to install so when you went be installed and p.m. when we install Express we have not mentioned the version so if I go back to the website so it should show you the version you can see we got four point 17.1 but what if you want to use their newer versions how do you do that so when you click on this versions tab you can see all the older version and if you want a specific version here let's say if you want to go forward fourteen point four point fifteen point one and if you want to install this dependency then you have to mention the version number as well so you can see we have a command here which is nbm so you can also use I or install that's your choice you have to mention the name of the package and the pose which is four point fifteen point one but there are other options as well so what if you want the latest version or you want diversion about this version so you can specify all those things with using special symbols which we'll see later when you talk about the packages in detail later so that's how we can use the existing packages but what if you want to create your own module it's quite possible you just have to get a module which we'll see later how to create a module but once you have your model ready with you which you are using in your project the same module you can publish on on NPM right so everyone can use it so let's see if I can make a video on that how to publish it on modules but as of now we have we have understood what is NPM and B M stands for node package manager which manages all your packages so we have millions of packages available online and if you if you are trying to solve a given problem break down that problem and one of the problem solutions will be available in the packages not one many actually welcome back aliens my name is 720 and in this video we'll talk about modules in no Cheers now one of the reason why I know Jason is so famous is because of NPM right which is no the package manager and we have discussed about it right so last year we caught one more than 1 million packages or modules on NPM so that means we can use all those modules to build your application within both our modules now you can imagine modules as small encapsulated units which you can reuse which you can share with everyone and which are easier to maintain now ok so I know it is getting confused right nothing about this we have a big application or we have a big problem now to solve a big problem the first step should be break down your big problem into small problems and then each small problem will have a small solution and we can name that small solutions are modules right or maybe you can just break break that small modules into more small modules and then you can have a small part now the advantage of making that small part is it is easier to maintain because it itself it has less code it is easier to share and it is easier to debug right so maintenance debugging and sharing is is the important thing now this concept is there in most of the languages or technologies and this concept is called modularity right so you have to create small modules so example maybe you can create a module using which you can connect with database you can create a module using which you can can you can write data to the file maybe you can you can create a module which will be good for calculations so maybe you can get a small module calculation which will have addition subtraction division multiplication and you can use this module in any of your project you can you can share this module with everyone in this world and they can use it of course for that you can just you just you just have to publish your module to the NPM and even if you don't want to publish you can reuse your own modules right so there's a thing but now in node.js they have in build modules if you remember in one of the video we have talked about HTTP module which is in build module in node.js so if you if you want to use it you just have to use a function called require likewise we have multiple modules and you can use it so basically node.js has some individual x' you can create your own modules as well I know that's exciting right and then you can use some third-party modules if you want from NPM so in this video our main focus would be how to create a module and how to use it so we are not going to use any existing modules we are not going to use any module from NPM but let's create one now for that let's create a small code so we have AB dot J's here and I would just wanted to add two numbers and then we have seen it write how to add two numbers but in this case I will be getting a function to add two numbers so let's say I have console dot log and I just want to print the output so I will say output is and plus I will say result okay I don't have this variable yet but hold on let me get this variable so as a result is equal to so I want to create a function which will give me the output but which functions are let let's call that function has ADD and this will take two parameters one is four and five and this return the result okay so 4 plus 5 is 9 right so this result should get 9 but holdout even if you try to on this code so let's run this code let's see what happens I will say app dot J's you will get an error it says add is not defined that's right we have this function we are using this function but where is defined it's not an inbuilt function and so basically if you have to create one so let's create a function here now how do we do that now if you're familiar with JavaScript it's very simple just a function mention the function name and you have to pass your parameters I will say a comma B and here you just have to say return a plus B that's how you add two numbers quite simple right now what do you think will it work let's try let's go back to our console and say I've dot J as it worked you can see we got the order as well it says the output is 9 so this is working but then where's the model here the thing is whenever you want to perform operation and if you want to get a module the best thing you can do is get it is create a separate file you can see in this case we just have one function maybe you have more functions so you can put all those things in as a bit file let me create one then so let me just go back to my app and here I will create maybe calc dodges that sounds better now in this calc I just want to create this function so I will go back here I will say cut and I will paste it here so basically I have this function as separate J's file and I want to use it here now all this work let's try let's see if this works and if I say no J's record the output that's weird maybe I have not saved it I was expecting a rebuttal anyway so you can see we got an error it says add is not defined and that's right in AB dot J's we don't have app functions defined or add function defined okay now that's simple right we just have to inform app hey you are looking for add but that is not doing this in the same J's file is there in the gap in a separate module but how will you do that and to mention that we have to use a very powerful function which is require and it's not that powerful but it's powerful the behind-the-scene so require a function you just have to mention the name of the module or the file in this case the module name is Galgo jeaious so you have to say calc Dodgers and then this calc dodgiest belongs to the same folder and that's the reason we are going to say dot slash this is not a inbuilt module right so we have to we are creating our own in the same directory so if you say dot slash and you have to mention the module name now will this work let's try ok or because that either now what is going wrong here now first of all when you say require require will simply say hey I want Cal DOJ's but then it will not continue that scope after this line if you want to continue this scope if you leave if you want to reuse that character jeaious let's put that in a separate folder or separate object will say Cal so now what we're doing is we are creating a reference to refer to this particular module which is calc in this case this name can be anything okay so it's not compulsion that your module name is Cal J so if you have to give calc it can be anything it can be ABC it can you pick your doesn't matter but once you've got the reference you can simply call this ad by saying Cal dot ad is that simple right now if you are coming from other language like Java or C sharp quite simple right it's just an object and you're calling a function not exactly let's try let's see what happens let's run this code again you got the other it says calc dot ad is not a function and that's weird it's a function right we know it's a function we have K to that function here so your thing is if you remember when we talked about modules in the start of the video I've mentioned that modules are encapsulated unit which means encapsulated means this functions will be available in the same module of course for this for the reason for security but then I want to use this function outside the module in some other module is it possible there's easiest but how we do that so we have to mention see in this caliber J's if you have ten functions maybe you don't want all the functions outside now those function will be used in the same in the same J's file okay but then what if I want to use two function outside now for that you have to use a very special command which is module or a statement module dot exports so basically you have to export all the function which you want the the outside wall to use it right but then how do you do that you have to say modules dot export and you have to mention add what you want to export so in this case I'm exporting add is that simple but will this work let's try so this is one thing so by so if you this particular function to be used outside just mentioned modules dot export equal to add and let's get back here hope this will work oh it's not working it says not a function now in this case you have to mention the label as well so you have to say modules not export dot are equal to ad so basically you're saying this add to this ad which you can use it here okay because if you don't if you don't mention this ad then the only solution you have is go back here and don't mention ad so you just use calc as a function because you are exporting and which goes to the calc here so this can become ad and that's a bit complicated so that's why I don't use this F syntax here so it works you can see now the exporting so the actual syntax we are going to use is you have to say modules dot expose dot add equal to add so that you can simply use gal dot add but it's it looks comfortable right looking at this code now so basically calc is an object ordered reference for the module and you're calling the add function and then you have to use this now does that mean okay so let's try this will this is it working so it is working but does this mean that you can have only one function what if you have multiple functions and you want to export them in that case if you have multiple function let's say I have one more function here which is function sub and this will take of course two parameters a comma B and here I will say return a minus B now in this case you have to say module dot exports dot sub is equal to sub now you can use this sub outside so you can call this function servant because it will it will work for you simple right okay so this looks cool now I want to so that's one way right but then don't you think this writing these modules multiple times is time-consuming so you can skip that you can simply focus on your next ports so you can say expose dotsub and expose dot a day when that works let's save this and let's verify that if that works so it is working so I stopped using what is you can use exports as well because you know behind the scene they are mentioning they're basically using a shorthand which is exports are equal to module dot export okay so this this is this is something which is behind the scene you don't don't have to mention that so even if you say expose dot add it it works so that's one way the other ways what if you don't want to use this way so it's quite simple you can come back here and you can say exports dot add is equal to function so you can use you can do this as well you are exporting here itself okay now this also works we can say we called the output so basically that's how you create a module in nodejs it's quite simple right so we have seen how to use existing modules we have seen in this video how to create our own module and try it out create multiple modules maybe you can add two numbers where you can create a module which will which will calculate the plan number or it will check the prime number which will calculate the factorial your choice right maybe you can create a module which will write data in database try it out and let me know in the comment section if you are able to do that and if you want to use a modular module you have to say require and one thing important if you want the functions from the module to be used outside use exports very important module in nodejs see in the earlier video we have talked about modules right so now we know that if you want to work on a project you maybe want to get your own modules and you may want to use some third-party modules and then we have some modules in built in node.js as well and one of them is file system now see when you have a programming language when you want to work with projects of course you need to work with files as well maybe you won't write something in a file maybe you want to read a file so how do we do that now thanksfully node.js has a file system module which we have talked about so let's use that filesystem module in this video and we'll try to write a file we'll try to read a file and let's try to append a file and maybe we can try to delete as well now I'm not going for any complex example let's go for simple examples just to understand how do we use a filesystem module and then we'll also see how to use in Bill modules right that's the idea here now first of all I don't want to use any external library or external quote so let's remove that this is what we have done in the earlier video so here I want to use a file system module now first of all which file I'm going to work with so if you remember see we can work with any file here we can work with text file JS file Java file doesn't matter right so the file should be readable that's it now if you have this file here you can see we have this gorgeous let's read this file is it possible can we read a programmer programming file is it possible and this is yes and this is one of the assignment which normally you get in colleges or hot online competition how do you read a file let's try that so I will read this calc file and let's read the content of this file and let's print that on a console now for that we have to first of all get the filesystem reference or filesystem module here and we know if you want to get a file system set you see require and in this single code you have to mention the the name of the module which is FS in this case instead of going for filesystem they go they went with FS that's a smart move and let's put that in reference or the let's put that in reference which is FS so we got FS which is a filesystem reference here and now we want to read a file now how do we do that so we can say FS dot now if you say FS you can see we have a list of commands here on these stuff functions we need to use the function which is read file let's use that now this read file will take actually three parameters one of them is optional I guess the second type is optional the first parameter I have to mention here is the name of the file and the name of the file is Cal dot J's let's use that the second parameter which is optional which is you have to mention the type of the file or the format of the file or the encoding to be specific so let's mention the encoding in this case the encoding is utf-8 let's use that so you have to check whatever encoding is there in the file and then your pure space for that here it is optional I if you skip it will not read the file in ideaa format but it will not give you either at least so the first parameter was the file in the second parameter is the encoding the third parameter is a callback function see what happens is when you say read file now hitting a file will maybe at it's a time consuming operation and then nodejs as a feature is called non-blocking i/o which means when you say read file it may take some some time maybe five seconds ten seconds maybe one second you don't want to stop the execution you don't want to pause the execution so maybe read file process is going on and then in the backend and then you can continue the remaining statements and then but how do you know that the read file function is done its job I mean it will come back right so you have to call up call that function so again we'll talk about this in detail once we start with once we talk about that concept of non-blocking i/o now depend upon when you are watching this video if you're watching this at a long time that video might have gone to the top of the playlist so and it doesn't matter so let's use a callback function here so whenever it completes the reading of the file it will call this function but then this function will have two parameters the first one what if there's an error right so the first parameter will be function called the error and the second parameter is the data now once you complete reading the file that data will be stored inside this data variable of course this can mean him doesn't matter and then let's do the process here so basically what we are doing is we are saying hey read the file in the uto format but then value doing a file if you get the error this error will have the information but what it is no there is no error in that case we'll be having data here and once you call it data of course you can check for the exception if you can check for the others as of now we don't want to focus on those things let's print the data as it is and how do we print data it's very simple you say console dot log and let's print the data as it is simple right so this should work let's go back to our console and here I will say let's run this file which is no app toward jails enter and you can see in the output in the command in the in the terminal we got the code that's awesome light so this so the content of this CS dot J's or Karagias is there inside is there on the console that's awesome so that's how you can read a file but what if you want to write a file let's try so what I will do is in this case now if I want to write a file it's very simple you just have to use a function called write file the way we have ratified of course we should have a write file here now this write file will take three parameters the first one is the name of the file so let's create a new file just for the experiment we say calc dot J's calc 1 tortillas the second parameter is the code which you are the content you watch which you want to write in this case I'm writing up cool and that's why I will say console dot log you know if you heard about this concept where a software can write a software so that's what you are doing a one app or one code can write another code I know we are doing a very simple task but we can do that right so here I will point done so I will just say console dot log done and then the third parameter is after completing this job after writing the detainer file if you want to plain something or if you want to do some operation you can do that here but what if there's an error so let's also take the handle of the error and let's print something I would say console dot log and we'll say data saved I mean of course you can write anything you want but let's say data saved what if what do you think will it work let's try so go back to my node and app dot J as you can see we caught the output it says it has saved and interestingly we got another file so the file was not there so your code is actually creating a file that's awesome now if you jump back to this file you got calc 1 dot J's so that that's why it is creating a file and then it is simply writing content but what if what if you write content in gal dot J's what will happen I might do the same code which is console dot log done in kalto just now which is already there so what do you think when you write that will it be the last end of this code or will it replace the entire code so that's that's a catcher question pause this video now I don't you come your comment will it replace the entire code or will it append it at the end so let's try I hope you have it in your answer let's try and you can see it say there are saved and it is replacing the entire content that's what right does so it will wipe up the content and it will write the count it will write the new content there but we don't want to write a new content right we want to append it every existing file now in that case what we can do is instead of using write file we can say a paint fight okay now once you say a pen file let's try it on this code let's see what happens if I don't this code it says there are saved but what happened to this file and you can see in this file we got a pen so it is adding the data at the end that's what we wanted right so we got the content so you can use the right file which will replace the entire content you can use a pen file which is append your data at the end so that's what that what that is working I just want to check one more thing how about deleting the data deleting the file so let's say we have this calc one dot J's which is not required I want to delete it so let's go back go back to the code here and this time let's dis let's delete it so which function we have to use here of course there should be a function right we have function for writing reading and for deleting of course we should be saying delete right but unfortunately we don't have that method name so the method name here is unlink so we have to use unlink and in this you have to pass a parameter now you can guess of course the file name should be I mean the parameter should be the file name right so we'll say calc window just as he filename and then you have to mention the callback function now what does callback function is now once you delete this file what you want to execute so I will say hey once you do it a file in cases either give me the handle so that I can do something with the error but in this case I don't want to do anything with the error I just want to print delete it that's it let's save this and I hope this will work so it's me sane node apt or jeaious enter and you can see it says deleted and if you see the file system or the file explorer we don't have calc 1 dot JS anymore that works so that's how we use unlink to delete a file so we have talked about how to create a file how to open a file how to read and then we have seen how to do it a file I hope you enjoyed the video let me in the comment section and do stuff of other videos bye bye [Music] welcome back aliens my name is w8d and in this video we'll talk about expressjs say when you want to make a website when you want to make the entire website using javascript so basically we can use JavaScript everywhere right the front-end back-end database and the framework as well now when you say front-end we have all used javascript in the front-end right and then we can also use javascript in the backend thanks nodejs do we have any framework for web development see we talk about different languages like we have Python we have Django framework we have a Java for that we have spring framework now when it comes to JavaScript or when it comes to node.js the web framework we are going to use here is Express juniors but then why do we need a framework C no J's provides you HTTP module right using which you can create a website where a user will send a request and you have to send a response that works but when you say you want to build a web application it will not be having a simple request response thing right you will not be saying hey server and some will say hello boy that's not how it works you build a website where you have multiple type of requests maybe you are requesting for an image maybe you're requesting for a detail maybe you are sending the data to the server and also there will be different type of lengths right so you're requesting a home page you're requesting a contact page you're requesting for the data about the user right all you are requesting for all the products from Amazon so basically you send a request for a particular endpoints so from a different type of response which you need you have to send a different about request and on the server side your function there will be different functions available right and that function will be executed when you send that above request and to handle that it's it's good to use a web framework and Express Reyes is an amazing flame verb it is fast and it provides you minimum feature so that you can build an amazing website I mean I'm talking about the backend per actually taller strength is more let's go to this website which is expressed as calm and if you can see it says it is fast uh opinionated minimalistic where framework for nodejs say fast it makes sense opinionated means it will allow me to do whatever you want see some of the frameworks they provide you their own basic feature they will say here this is how you have to do it this is how you have to do it Express says do whatever you want so that's the definition and this is the latest version we can use so let's start let's build a simple application using expressions now when I says simple I'm literally talking about a base application of a basic application so let's go to vs code and this way let's build it now to start with let's write a code which will return you hello world I mean that's how you start every time right to get started you have to make sure that you in your machine you have no J's and we just read your code in this machine I have no J's which is 14 point 1.0 let's verify once I would say no what version and you can see this is the latest version which we got and you can also have latest version for your school now once you have that let's get started now first of all if you want to use Express J's or Express framework in your project so let's first get expressing our code so let's say Const I want Express in this machine so in this in this project so I will say cost Express equal to now how do you get it so of course we have seen this function which is require and in this require will mention Express VBA we did Express and as we have talked about this before you have different modules available in nodejs some of the modules are there in the system and some of the modules are coming from the external server what about Express now basically Express is coming from the third-party server or basically you can say NPM okay but will this work because we have not installed Express on this machine so before installing it let's try to run this code and let's see what happens but even before I think we have to do some more thing we have to also initialize we have to also start the Express and we have to get all the functions all the properties of it and the way you can do that is by saying Const app now in this app I want Express functions to be there so I will say Express this is how you may utilize an Express so we got Express handler we got Express started and you can use app to do everything with with a that's now but will this work let's say save this save this and go back to your terminal and this is where I will say no word and let's run this port will say app so we can also say app or you can see app dot J's both works let's say app and we got error let's see what at this error is so this says cannot find a module experts that's tricky why it is not there same thing is whenever you work with modules which is coming from the third party the first thing is you have to install them and once you install it it will come in that folder so the folder which we are working with is known X now in this folder will be having the the node modules folder ok so how do we get it so first of all let me say NPM and p.m. the command is you can you use installer you can use I both works in fact if you don't know how to use this you can just go to NPM jas and here you can search for the Perl which are searching for which you want so I want express so when you say search it will give you the exact match now how do we install it so there will be a command here you see that it says install NPM I express so you can mention I or you can mention install both works in fact you can also mention the version number which you want by defi it will give you this version but you can also mention the version by specifying a direct and the version number so let me simply say install Express let's get the default one and let's say enter now it will take some time to download link Express from the NPM server ok it's done and if you can see on the left hand side in the node modules if you expand it you will see the module of Express oh but hold on we got not just Express Express will be here but with Express you've got some other modules as well now this other modules are the dependencies for Express so you so for your project Express is a dependency and for express this all modules are the dependencies that works okay but at least we got Express right now it should not give you an error let me just run this code node and this time you can see we don't have any error that means Express is there and we can we can use it now but then what we want to achieve with Express as we and we want to run hello world okay that's tricky but then do you think we want to use a Express for hello world that's not the case right see if anyone you sent a request maybe that request is for the website maybe that request is for a particular URL now in this case I don't have any website I just want to run it on localhost with a port number nine thousand and whenever I request for this home page it should return hello world that's the first thing we want and as we as you can see we are not hitting any data the next thing is what if I want to fetch the data body particular aliens I want localhost for than 9-thousand slash alien because that's how you build a website right you will be having multiple you others so when you say enter here it should give you let's say no in 20 that's what we want and maybe I want a particular alien with an ID or 20 so that's how you build a URL so if you know the concept of list API that's how you build a UI you are I so you mentioned the website name then you mentioned resource what you want alien you have to also pass the ID which agian you want so I want the alien with the ID number 20 and this will change right and that's why we say it's a dynamic URL because this will keep changing based on the user someone will say hey I want 21 someone will say I will I want 28 so when you send a request for 28 your nodejs will use this 28 to fetch the name of the user from the database because you are saying 28 express or nodejs need to search about who is just 28 so it will find out for you to database it will say hey I bought the ID 28 I want the name and that's what it's dynamic because it will change according to users and maybe you want to pass the query string so basically when you say about 28 like this you're passing the parameter but what if you want to pass a query string now this thing changes depend upon whatever application you're building so you can also pass you can also say question mark and you can say what name is equal to Naveen I want to search data about Naveen or maybe I want to search the ID as 28 so this is the query string right so so you can use the query string or you can use parameters that's your choice I want to do this thing in this video in this video so let's get started now first of all you want to start this server because the saw is not running yet okay so how do we start this hour it's very simple you say simply say app dot you have to pass listen this is for simple right and then in this listen you have to mention the port number you can use any port number let me use 9000 here you can use any available port number so let me use 9000 and doing this let me just reset the server okay so the you can see it is starting the server that's why we require that prompt it will allow it will give it will give you that one turn in one stone body now once you've got this server up and running let's go back to the browser and let's send a request to the home page okay so that we have not got any error but we got cannot get slash but we have not got any error right we got something so your server is up and running what we don't want this what we wanted is to get hello world how do we do that see that's the first thing we want to we want to get in a world but what we can also do here is I want to show I want to print here if the server is studied or not how do we do that so what we can do is we can give a comma and we can pass a function here by saying request response and here we can say console dot log and we can range running now this we will see this away is running on the console let me stop the server once and that's this chart and you can see it is it's saying running that means how is running so if that was this our started it will print running but then this will not solve our problem right we want it to print hello world right so how do we do that not what to achieve that we have to understand the concept of routers now it is router or the outing is in your website will be having multiple links right so for different above link you will pass different requests and for the different requests will be having different response so how to handle a particular request is decided with the help of routing concept okay so Express has that concept in belt so let's use that so what we can do is we can specify the routes so we can say app dot you can use a method which is get so in HTTP we have different we have get post put and in it we'll talk about other methods in a separate video how to use how do we how do we use get how do we use post how do we use put or delayed get these simply fetching data from the server okay so when you say post you're sending data when you say get your receiving data so let's use get because we want to receive data and this gauge will take two parameters the first parameter is your the URL which you are liquid which you are heating so basically when you go to the home page you are requesting for this you as a widget slash so here you are requesting for the home page you're requesting for slash but once you send the dis request what will happen you have to execute the function right so you will say function here and this function will take two parameters by so this function will take two parameters and you have to execute something so what I would execute is I want to print in a world right so we can do that it's very simple you will say console dot log and you will find in a world simple right okay that's verify will this work so let me stop the server let's start again and go back to the browser okay we once we refresh the page you can see the request we have still not got to response yet but if you go to console you can see record to the world oh we were expecting hello world on the browser right something went wrong okay what went wrong is when you send a request and when you say I want a response you should not be saying console dot log right because console dot log will print on the console we want to send response on the browser in that case will not be saying console dot law we have to use a special object if you can see we have two objects here which is the question response the quest object is request object will have data which is going from client to server and response object will have the data from which is going from this server to client so I want to send data from server to client so we have to say response dot the method name is send and then you will send hello world this should work okay let's go back and refresh oh we got hello world err okay one before refreshing because it was analytics requests and it was it was waiting for response okay so we quite had a world so we have achieved our first thing we got hello world but then we also wanted to have this thing right when you say request for alien it should get it should give you data board alien how do we do that and for that we can create a separate function so we can say app dot gate and this time we are requesting for alien and it should execute a function when you request for alien what's our DQ are the yes it will execute and it will say this pose dots and will send a response as welcome back Kenya that's it let's see if that works let me just reset the server once go back here refresh oh it is still not working is it because of this flash let's try that also I was missing / there okay but we got welcome back alien right that's how you can that's how you specify the routing so for different urs you you will have different functions which is working for working for it okay what I also want here is I don't want to fetch I want I don't want to French welcome back alien what I want is if we if you pass the pitiable alien by saying 20 I want to return the be 21 kita 22 hush so based on what you send I want to send that data now of course I don't I will not talk like the entire logic because for that you have to write if else right so if it is 20 you have to return with Naveen if it is 21 Kiran if it is we do you have to return her it has the information you have to complete okay that's your assignment whatever we do is I will just show you how do you accept a dynamic URL so for that I will say again app dot get and then this specify alien now since I want a dynamic URL I was specified 20 and then I was a function request response and then we can respond it with hey Naveen okay this should work now let's try it's running refresh can see that we caught hey Naveen but the problem is the moment you change this URL will be Genesis 8:21 it's not working it's because we are still specifying 20 there we are not specifying 21 we want to make it dynamic because this will change so in that case you can hold that in a particular holder a place holder which is ID in this case or the pen and so instead of using so with now we got ID right so we can trace this data so I will say Const I would say ID is equal to how do you fetch it so we can use the quest object request object has something called as params and in this pattern you can specify ID so whatever you mentioned here the same thing you will assign here okay so which is ID ID a ID a ID that's your choice and then you can use this ID you can opt now you can use if condition and you can check but I will not do that I will simply print this ID as it is let's just show that it is dynamic and let's stop this running refresh and you can see that even if we say 21 it works even say 20 it works so what do we do you pass in the address by it will print on the on the page but that's not we wanted right we wanted to have a different code that's what you will do and the hint is you have to use if-else you have to check for the value of the ID well that works but what if you want to specify with the help of maybe the query string may be ID is equal to 20 now what will change here is you can see it is still going for Radian because that's a request you are passing the query string so you can change this alien as it is here what you can do is you can get that name so you can say Const name is equal to we are passing name passing ID so with the ID equal to request dot wait because this time we are going for body not params to specify ID now once you've got the ID you can come back here and you can say welcome back alien and you can also mention the ID here they start this server so you can start afresh so you can see we have worked with both now we can use the params which is slash 20 which is mostly used for the disk a P I but if you want to work with the browser way and when you go to Google and when you search something example if you search for Java if you can see Google google.com slash search question mark Q equal to Java this is the query string that's how it works your search for 10 is 4 that's how it works search and now it will search for the things ok that's how we use query string and pad an I hope you enjoyed this but the only okay there's one issue I know you might be thinking about this is every time you make a simple change you have to a study server right see if we don't wanna do that you have to install one mode model which is note mod using which you can actually we don't have to distract the server you simply have to save it will automatically study server behind the scene so yeah that's how that's much from this video way we have used Express layers to create some URL mapping in no Cheers so that's it I hope you enjoyed it in the comment section and those are some of the videos bye
Info
Channel: Telusko
Views: 124,570
Rating: 4.910913 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: vJEO57B05Sg
Channel Id: undefined
Length: 88min 9sec (5289 seconds)
Published: Fri May 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.