Getting Started with Node JS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Devin venti 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 inauguration of that technology right even when you when you work on blocks and the first thing I do is I print a low voice okay anyway so when you let's let's write a code to print hello world on the console so when I say console 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 or C and/or the command prompt that's that's interesting right so not just that we'll also see how to add two numbers so that we can see that it's a POC it's 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 an 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 this thing in a particular folder so in my terminal I will simply first just increase size of the terminal so in this terminal i will say let me create a simple 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 then the file is not important what is important is the extension now since we are working with no J's where Jas is JavaScript so the exchange in 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 the script directly 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 run 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 you run this code now the way you can do that is by saying node there's the come on you have to pass and the the name of the application which is app dot AB dot J's which is the file click on enter and you can see be courted oh boy so that's right 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 but just check how that works let me just create three variables I will say bad we can also use that lattice debater way 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 code inthere or semicolon if you are coming from let's say Python you want to avoid it so JavaScript has both the options you 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 see node.js is famous for web development right so I mean of course you if you are running or GS maybe that's a target to learn web development and well debating 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 there is with the help of IP address but then we 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 dinner service and 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 order and you serve all 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 it differentiate between this services is with the help of port number so if you want to reach to a HTTP service or 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 to mention the port number so whatever service we will create we have to do or both you 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 write like 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 or you can also mention localhost that's your choice but then only doing this we not work you have to also mention the port number right so I will say : 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 qualify 8 to 20 tutorial online they will prefer 9,000 but since I'm coming to our background I have worked an opposite arm cat 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 8000 1 or 8 7 8 8 7 6 that's a choice I'd but use something which is easy to remember and 9000 is easy to remember some people use 3000 but let's go with 8000 I love that port number and when I say enter so you can see we got four not four that's the code 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 get a service so how do you do that it is very simple you just have to create use a function called create server now create service 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 are coming from JavaScript you know what I'm talking about but this is we are passing a function in 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 our sensor responds so you need two objects your request and response well there was extra semicolon then it may complete that here okay so we 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 a response 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 defy that's where 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 pizza 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 HTTP 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 here in stdp now 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 have not we are not using the HTTP yet so let's use it so here in this create server you need to if you want to call create server you have to use HTTP dot create server because creates our 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 see a thing is we are we have we to this server but then we have not mentioned the port number so this server has to listen to the port number which is 1880 and to do that you will simply say listen after this bracket after this function 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 node app dot J's okay there's no error and this time you can see you're not even getting the next prompt it is simply is 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 just send after doing the changes let's reach out the app let's go back here and we got 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 nd we have to say we have to mention end here otherwise if you just want to send one statement instead of using and 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 we 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 head I so you can say ah des dot you have to say it right head and in this bracket you have to mention two things you can mention the status and you can also mention the Kuantan 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 XHTML sometimes you want to send the data in JSON format so you can mention the JSON 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 there as well you mention the content type when you work on the web service so that's it from this video where we have seen how do you getting 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 so I hope you enjoyed this video like me in the comment section and wait for the upcoming videos bye
Info
Channel: Telusko
Views: 142,693
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: U5h97cFmj8I
Channel Id: undefined
Length: 14min 23sec (863 seconds)
Published: Fri Apr 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.