Node.js Tutorial For Absolute Beginners
- January 9, 2024
- Posted by: MainInstructor
- Category: BASIC C Go JavaScript Node PHP SQL Web Development
Video Title: Node.js Tutorial For Absolute Beginners
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
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Amazing!.
10/10 thanks
hame basic website ko node js pe kese karna he
Best Node.hs intro ever
May I know what all should be studied in JAVASCRIPT?
Hi,
Is it possible to include print API where we can configure number of copies, black and white/color print in javascript
After so many wasted hours trying to understand few things on getting started this helped me
Concise and great. Thank you so much from Phetchaburi, Thailand.
Thanks. This tutorial helped me get a quick idea of Node.
Like -subscribe ,whatever that u can lol you got me
Tks for sharing the contents ! No music, no hurling 🙌
cool
thank you so much!!
Awesome video appreciate the knowledge.
What a great, concise overview. Thank you for sharing, Brad!
Thank you!
on going learning
very informative but also you are very boring. Just really monotone.
Thank you
I get stuck at 14:30 "Error: Cannot find module" Module not found." 🙁
If node js has http module built in, why would one need to use Express module for web server?
If I want to prepare a production environment on windows server with node js and express web server, do I have to follow certain steps or instructions other than CPU and ram configuration?
Okay, but why are you using the .js text file instead of node to code. I thought I was going to be coding on the CMD…
Brad, you are the Man!
I just simple search "Traversy Node". By dar the best beginner vídeo for node.
Funny to listen about Chrome being best browser so far in 2021 😀
great explaination.. this vdo vry helpful to me.. thanks
Is this safe?
I know this is old, but wanted to try asking anyway.
Does node js only serve files or can it be fetched requests and the like? How well does it play with packages like React?
13:50 why i dont have that text document, my document is notepad . pls teach me :<<
Excellent!!!!!
Thanks. Great tutorial.
When I go to localhost:3000, it tries to download the file instead of opening it in the browser. Could someone please tell what I've done wrong?
This guy! Damn!
Really liked it…..Big thanks to u man..keep it up
Really liked it…..Big thanks to u man..keep it up
Great tutorial. Thanks for cutting to it without trying to be a comedian and/or side tracking to a bunch of crap like sooooo many channels do.
How should I start learning coding if I have zero knowledge of anything at all?
when I create app.js it makes it app.js.txt…I fucking hate Windows man nothing ever goes right
3:29 System memory of what? I would have loved to see you dive deeper into explaining this a bit more detailed.
This is what I was looking for.
I finally retyped the code, saved, and restarted everything. It is now working like
Brad's example. Thanks
Great course. Looking for assistance with error being received from server:
TypeError: res.Write is not a function
at Server.<anonymous> (C:projectstestapp.js:15:13)
at Server.emit (events.js:315:20)
at parserOnIncoming (_http_server.js:874:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
I did not see anyone else with an error to reference from this course, so wanted to see if anyone might have a suggestion on a resolution. I have replayed the course a couple of times and rechecked my code against Brad's code, but still receiving error and no longer able to reach the server. Any assistance is appreciated.
You need to run the school system
you could use nodemon
terroriser