Node.js Tutorial For Absolute Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video we're going to be covering the fundamentals of nodejs now if you've been following my channel for a while then you may know most of the stuff it's geared more towards beginners but of course it never hurts to to brush up on the basics now I know if you search for nodejs and YouTube a million different guides come up but unfortunately many of them are just too complicated and drawn-out or they don't explain enough so I wanted to create a simple spoken guide for anyone that's wants to start working with node but doesn't really know much about it as far as prerequisites I would suggest that you learn at least basic JavaScript before even thinking about nodejs you don't have to be a pro but you should at least have a grasp on basic JavaScript programming concepts so here's a plan for this guide what we're not going to really focus on building a certain application we just want to get you familiar with nodejs and how it works all right I'll show you how to install node and windows will look a little bit at the repple that's included that's read eval print loop and that basically allows us to run JavaScript in the command line NPM or node package manager is a huge part of nodejs which you may have used before it allows us to install packages and modules for node we'll talk about how modules work we'll look at some examples of some of the more popular modules the package.json file which is the main manifest file that you need to have in your node apps and projects and then we'll look at a basic web server alright then we may go may go on from there depending on how much time we have left I don't want to make this too long at all so what is no js' it is a JavaScript runtime and it's built on Chrome's v8 JavaScript engine all right so v8 is extremely powerful it's written in C++ and it's what's used in Google Chrome which is in my opinion the best browser by far you may think differently but of course you are entitled to that so note is essentially JavaScript running on the server for years javascript was thought of as a client-side language that only ran in the browser this made it very fast but also extremely limited and it was mostly used for things like user interface widgets sliders things like that but now it can be ran on the server with no js' and we can build in-depth sophisticated real-time applications with JavaScript nodejs uses an event-driven non-blocking i/o model and if that means absolutely gibberish to you that's fine let's go over it so this is one of the parts of node that is one of its biggest if not its biggest advantage but also one of the most confusing to understand especially for new developers node uses a non-blocking i/o model now I'm guessing that some of some of you have experience with traditional server-side technologies I'm going to use PHP and Apache as an example so when you build a PHP application on an Apache server each connection or request spawns a new thread which takes up system memory and eventually maxes out whatever that the maximum memory load handles all right so it essentially needs to wait for one process to finish before starting the next and this works in it asynchronous way note however operates on a single thread and uses non-blocking i/o calls which allows it to support tens of thousands of concurrent connections which are held in what's called an event loop okay and that's why it's called an event-driven system so the way that node works optimizes throughput and scalability in web applications with many i/o operations and of course this makes no extremely fast and efficient so if we could see the event loop this is kind of what it would look like it works on one single thread it waits for events and it uses an event emitter class to bind events to listeners and it triggers a callback when an events detected which all happens asynchronously and it's non-blocking now I know to some of you that what I just said may still not make any sense but just know that with traditional server-side programming you would have to just wait for one task to finish before you can start the next with no js' it's on a single thread and you can have tens of thousands connections at once okay so old tech shouldn't say old but other technology such as PHP and Apache is synchronous nodejs is asynchronous so let's take a look at some of the things that we can build with nodejs so it's used extensively for restful api s if you want to the ability to accept get requests to get data from a database and then serve it to a client or accept post requests or put so you can update delete requests all that stuff and I actually have a couple videos on how to create REST API using node so I would definitely suggest looking at those after this video real time services such as chat chat applications games it works really well with socket IO blogs content management system social applications utilities and tools pretty much anything that you can think of that's not CPU intensive so NPM is like I said extremely important and a huge part of nodejs it stands for an node package manager or sometimes node package modules and it basically is it's used to install node modules or programs makes it easy to specify link dependencies modules get installed into the node modules folder so if we go ahead and we run NPM install Express that's going to install the Express module into a node modules folder alright and if we don't have a node modules folder it will create one for us now you can also install modules globally by adding the dash flag and that will install it on your system so that you can access it from anywhere not just in that directory are not just in that application so some popular modules we have Express which is a complete web development framework for creating powerful applications Kinect is an extensible HTTP server framework and is basically that the the baseline for Express socket IO is a server-side component for working with WebSockets POG which used to be called Jade is a popular template engine inspired by Hamill it's also the default template engine for Express or and Mongoose are wrappers to interact with the MongoDB database which is extremely popular with node CoffeeScript which is a CoffeeScript compiler CoffeeScript is a superset of JavaScript Redis which is kind of a no SQL database but also a caching system this is the Redis client library so these are just some examples of some of the thousands of modules that are available package Jason is a really important file that is used in node packages and applications and goes right in the root and it tells NPM how your package is structured and what to do to install it alright so it holds things like the name of the application the version description main is basically the file that is the entry point to your application in this case it's server J s but it's often called app J s or index J s and you can call it whatever you'd want really author license things like that there's a lot of fields that are not here that are not required as well it also lists all your dependencies so all the modules that you want to use in your app can go on this file as well in this case we have body parser express in J s and it has the version for each one all right now if you want to specify an Astra's care that will basically stand for the latest version of that module all right you can create a package.json file manually but you can also run NPM in it and that will set it up for you it'll actually ask you some of these questions and create it for you all right so enough with the slides let's jump in install node and we'll start to take a look all right so we're going to jump in and install node on this Windows 10 machine so if you go to node.js org we just want to click on the button here we're going to get the at this time it's version six point nine point one this is the recommended version so that's what we're going to grab all right we'll download that it's very small file twelve megabytes let's go ahead and open that and we're going to click to run and let's just go through this just your standard Windows installer Program Files is the default folder we're going to click Next and install alright that's it no js' and NPM should now be installed now before I open up the Windows command line I just want to show you that in the Program Files there's now a folder called nodejs and then you have this node dot exe file I'm going to open that up this is the repple the read eval print loop program and it's basically a command line and we can run JavaScript in here all right so if we just type in let's say a number it'll give that back to us we can do addition for instance okay that gives us back four we can basically do anything we can create variables so we'll say var X equals five and it gives us undefined because we did we're not actually returning a value because we're just setting a variable but if I were to type in X and enter you can see it gives us five okay we could say X plus two gives us seven we could use for instance the date function if we were to or the date object rather if we say var d equals new date and then we say D you can see that it gives us the current date what else can we do we can do loops so if we say for let's say we have a variable I equals 0 and then we'll say as long as I is less than 10 then we want to increment I plus plus and if we put an opening curly brace and click enter it actually takes us to the next line so we can we can have multi-line commands here all right so let's say we just want to console dot log we'll just console.log I okay we click enter put a closing curly brace click enter and it gives us 0 through 9 all right we can also create functions so if we want to say function greeting and that'll take in a parameter called greet okay we can go on multi line here and then let's just do console dot log greet okay and then if we do greeting and we pass in for instance hello world enter gives us hello world all right so we can do anything that we can do in JavaScript now there are some things we can't do that have to do with the browser for instance we can't use the document object because we don't have a document if we were to for instance say console.log document says documents not defined if we do document dot get element by ID that has no idea what we're talking about okay so it's JavaScript without the browser now I'm going to close this up and I'm going to open up my Windows command line and we can basically get into that same environment by just saying node all right so now we can do one plus one or whatever okay whatever we could do in that repple environment but I'm going to get out of this with ctrl C twice and what I want to do is create a file so I'm going to go go into my C Drive and then let's see projects and I have a test folder in the test folder I'm going to create a file called app J s ok and I'm going to open that up and we'll just use sublime I was going to use something different but we'll just use sublime for the text editor so in here let's do console dot log hello all right now in my command line I'm going to CD to that folder which is projects and then test and let's do node and we called it a pas ok so we can actually run that file from our command line we can put a function in here you put our greeting function pass in a parameter of greet and then console.log whatever we pass in and then we can just run it down here and we'll pass in hello world ok so if we run it with node and we don't even need the dot JSP can just say node app and we get hello world all right so what I'm doing here isn't really useful it's not something you would really want to do it but just to show you that we can we can run JavaScript from the command line we can run the JavaScript files from the command line as well so now let's start to create a very basic node application so let's see in this test directory will keep the app j/s there but we need to create a package.json file okay we talked about this so let me just clear this out and what we're going to do is run NPM and NIT now when we run that it's going to ask us some questions here the name of our application and whatever's in parentheses is the default if you just click enter so let's just call it my app the version ok the default is 1 0 0 that's fine description we'll just say simple test app entry point this is the main file we already created a file called app J s so we're going to keep that test command we don't need this stuff git repository author you can put your name there if you'd like license and then it's just asking us if that's ok and we'll say yes all right so now if we look in our test folder we now have a package.json file okay if we open that up it basically just has all the stuff we just entered now what I want to do is open up app KS and we're going to create a very very basic web server probably the most basic web server you could possibly have all right with something like PHP you take a PHP file and you upload it to your server and Apache or whatever server you're using takes care of the all the requests and all that stuff with note it's a little different you have to kind of create your own server so we're going to build the simple server that's on the node.js website so let's go ahead and create a variable now I'm going to use the es2015 syntax and use constants Ted of var you can definitely use var if you want but it is almost 2017 so I do want to keep this kind of modern so we're going to set Const HTTP now HTTP we're going to require that and that is a module we're including a node module now we didn't have to do n p.m. install HTTP because it's a core module it's already included in the system if we were to use something else like Express we would have to install that first so now we're going to do is create a couple variables we're going to create one called hostname and we're going to we're working on our localhost so I'm going to use the loopback address which is 127.0.0.1 all right then we're going to define a port we'll set that to 3000 so now we want to do is create a variable called server and we're going to set that to use the HTTP module and we don't want to call a function called create server all right now this takes in a function we could do this and then pass in request response but I'm actually going to use an arrow function because I want to keep this es2015 so we're going to put in a set of parenthesis pass in request response and then use an arrow function alright so we now have access to these two objects so we want to set a status code okay when you make a request 200 is the code that basically says that everything's okay so we want to say res dot status code set that to 200 okay then what we want to do is want to set a header we want to set the content type in the header so we can say res dot set header and if you look at the documentation for the HTTP module it'll show you all this stuff so we want to set the content type we want to set that to text slash plain all right and then we're going to take that response object and we're going to call dot end all right we can actually put something in here that will get output into the browser so we'll just say hello world all right so now we want to do is take that server variable and we want to call dot listen and this is going to take in the port the hostname and a callback which we're going to use an arrow function and then in here we'll just do a console log and we'll say server started on port and then we'll just concatenate that port variable all right so let's go ahead and save that and then if we go back to our text editor here we run node app you'll see we get server started on port 3000 alright and we get this blinking cursor so that means that it's running so if we open up our browser and go to HTTP you could do localhost or 127.0.0.1 n't do much all it does is output some text but it is in fact a server now if we want to actually load a file we can bring in the filesystem module so if we say Const FS and we'll set that to require FS and then what we'll do is let's see we'll go right here and let's say FS and it has a function called read file which does exactly that it reads the file and we'll pass in let's say index.html and that'll have a call back please use an arrow function and then that will have an error and let's say HTML check for the error if there is one then you can do whatever you want with it I'm just going to safe throw error and if there's not then we're going to continue on and run the server so we want to take this cut it and then we're going to put it inside here just tab this over all right so now in here what we want to do we want res dot set header that's correct and then we want to do for end I don't want to actually output anything what I want to go in here and say res let's do res dot right and we'll put in here the HTML that's coming from up here so let's go ahead and save that and then we'll go into our folder and let's create a file called index.html and we'll open that up and let's just put in some basic HTML tags we'll say hello there and let's wrap that in an h1 all right so if we save that and we need to restart the server so we'll do a ctrl C and then restart it with node app we go back and reload now notice that it's actually showing us the HTML tags and that's obviously not what we want but the reason it's doing that is because if we look over here at the content type we have text planes so it's looking at it as plain text what we want to do is change this to text HTML all right and then of course we need to restart the server and if we go back and reload now it's parsing the HTML because the browser is getting that content type and it now knows that it's supposed to parse the HTML now this is obviously just scratching the surface we haven't gotten to installing modules yet and using third-party modules so what I would suggest now is that you check out the Express j/s crafts course because we not only use Express but we also use I forget exactly which template engine we use but we use a template we use body parts or some other modules so it's definitely the next step that you want to take now that you know what nodejs is you have it installed we worked a little bit with some of the core modules and so on so I would suggest you check that out and thanks for watching subscribe leave a like whatever you can is great and I will see you next time
Info
Channel: Traversy Media
Views: 965,431
Rating: 4.9317446 out of 5
Keywords: nodejs, node js, node.js, nodejs beginners, node.js tutorial
Id: U8XF6AFGqlc
Channel Id: undefined
Length: 24min 46sec (1486 seconds)
Published: Fri Nov 18 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.