Welcome back.
In this video we're going to write our first nude application and they use the term application extremely
loosely.
It's just going to be a single file with some basic javascript but I'll show you how we can interact
with node cons..
And we'll also see how we can run a file with node.
So those are the two objectives here.
The first thing that we'll talk about is the node cons..
So we've seen the javascript Council and the browser before which we can open up and we can interact
with we can type javascript commands in the browser can do things like alert and constant log or whatever
debugging that we want.
Well node has its own version of the console and it's a little bit different because it's all based
in the command line.
We don't get the nice browser interface.
There's no h l tools no inspector no elements because we're on the server side.
There is no.
Each time there is no CSSA to actually look at and interact with.
So the node console is just a place that we can open in our terminal with a simple command and then
we can start typing javascript commands and they'll be executed just like they are in the browser.
And to be honest it's really not that useful.
You might end up opening it once or twice.
Aside from this video but I do like to show it just so that you know it exists.
We can open it up by typing the command node and I'm actually going to show you how I can do it on my
computer in the terminal first just so that you understand it works exactly the same way instead of
the browser you use in cloud 9.
So I type node and my cursor at the prompt changes would make that a little bigger.
You can see if we get this or karet which means I'm now inside of the JavaScript console.
So if I type something some javascript like four plus 10 I get 14 or I can do things like hello plus
worlds just like that where I can do full javascript I could write a function if I wanted to.
I won't do that here but I can just interact with javascript.
It's something called a Ruppel are EPL which stands for.
Read evaluate.
Print loop.
I'm a big fan of drawing comparisons between languages and between different frameworks and technologies
.
I'm actually going to show you Ruby's own REPL which we will never use in this class at all.
But I just want to demonstrate so that you can see it works exactly the same way.
So we need to get out of here and to quit.
I can type Control-C twice and to open up the ruby cons. I type I Arbi it stands for interactive Ruby
and now I can type ruby code in here so I'll type a really simple line of Ruby like 40 plus 9 and I
get 49 and that's all I want to show.
It's a place we can write Ruby in our command line we type IRP and hit enter and we enter like this
new place where we can't type our command line things like LS or CD because it thinks those are Ruby
variables or methods or something.
Same thing when we go into the node cons..
Now I'll show you how we can go over to cloud 9 and start working in the node consul here.
It's the exact same command we just type node and hit enter or cursor changes.
Now I can start writing server side javascript.
So let's do something simple again.
Well just to constantly log a bunch of letters just like that and we get this printed out.
An important point I want to make is that a lot of the things that you're used to in the Java Sea are
of counsel and the browser are not available here.
We can't do something like alert if I try and do it alert.
Hello I get an error.
It tells me alert is not defined and that's because alert comes with the browser.
And even though we're in the browser here remember that we're not actually in the browser so we're typing
code in a browser but it's being run on one of cloud nine computers somewhere else on the server side
where there isn't a browser so it just like me going here and going into Knode and typing alert something
that doesn't exist because I'm not in the browser.
Same thing like the document document doesn't exist at all in code.
That's purely on the browser side.
So none of the Domm none of those events listeners in one of the selectors.
The manipulation all of that is only in the browser.
None of that exists with note.
That's all I want to show with the node cons..
As I mentioned we really won't be using it much but it just a nice introduction to how Knode works.
We can just start running Javascript in the terminal.
The next bullet here is much more important which is how we can run a file with node.
So what I mean by saying run a file with node is that we can create a javascript file.
Call it whatever you want and fill it with javascript code and then we can execute it in the command
line without a browser without any HD Melosi access just executed on its own by using the inode command
to use it.
We just type node and then a file name just like that.
And if we hit enter it will try and execute whatever file we provided as Javascript.
So let me demonstrate that now.
I'm going to in my command line make a new file.
I'll just call it hello.
Yes so touch.
Hello.
Yes and now will type LS make sure it's there.
There it is.
Let's open it up in our editor and let's add some simple Javascript.
So let's do a for loop for our i equals zero.
I less than 10 I plus plus and will just do a constant alt log.
Hello from Hello dot.
J S.
Just like that and we'll save it now.
I can use the node command to execute this file to actually run this code.
So again I type node and the name of the file which is hello.
Yes and that's it.
I hit enter and it executes the file for me and it constantly logs to this council here my terminal
.
Hello from Hello Dutch.
Yes ten times.
We won't be writing code like this very much what will actually be doing is stuff like this which you've
already seen where I run node apps.
Yes which is my file that has my server and it actually starts the server for me and it serves it on
port three thousand and I can go visit port three thousand now because I ran after J.S. with noter as
Noda executed the file.
So that's what we'll be seeing.
Use a lot more as.
But we can also just do this or we have a for loop and it comes to that log.
It works the same way.
It's just javascript code that's being run the very last thing I'll show here is another parallel between
javascript and node and Ruby so we can also create a ruby file and execute it.
Not with the node command but with the ruby command and I'm showing this to you again.
Not because you're supposed to be learning Ruby at all.
You definitely don't have to remember this but I do want you to see it so that you can draw another
comparison between node and Ruby and other server side languages.
This is something you can do and pretty much every server side thing which so I'll go over here and
make a new file called Hello RB and I'll open that file up.
RB is how we make a ruby file and I'll just do something simple.
Hi from hello.
RB And we won't use a loop.
We'll just do it three times and save.
That is what Ruby looks like.
No semi-colons or anything we don't need parentheses.
I'll close out if there.
And to execute that file instead of doing Noad hello.
Yes I run ruby.
Hello dot RB.
And it executes my file which is Ruby code.
It runs it as Ruby and it prints me.
Hi from Hello dot RB.
Three times.
OK so that's all I want to show here.
There were two objectives.
One show you the node cancel which you can open by just typing node with no argument and then run a
file with node which you can do with node and the file name and it needs to end and Dot J.S. in the
next lesson.
Good to have you write some javascript on your own and execute it with node