Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll create our first simple application to be run on the Node.js platform.
Commands
Running a node application
node <filename>
Running the node REPL (Read-Eval-Print-Loop)
node
To exit the REPL use CTRL+D once or CTRL+C twice.
Documentation
Node.js Installation Guide
-
0:00
When getting acquainted with any new technology,
-
0:02
it's customary to create a Hello World application.
-
0:06
We'll be using Treehouses online coding environment work spaces.
-
0:10
You'll be able to write your no dress up locations and run them in work spaces.
-
0:14
No need to set up your machine for development.
-
0:17
If you want to use Node JS on your local machine, I've included an installation
-
0:21
guide for Marco S, Windows, and Linux in the teachers notes.
-
0:25
Let's click the launch workspace button along with this video and jump right in.
-
0:30
Your workspace should look like this, with a coding area at the top, and
-
0:34
the console or terminal down here at the bottom.
-
0:38
The reason we got the terminal open here,
-
0:40
is because Node JS is a terminal based application.
-
0:44
To make sure you've got Node JS up and running, type node then a space -v.
-
0:50
Hit return, and you should see the current version of Node JS running.
-
0:57
Now, don't worry if your number is different.
-
0:59
It means you've got the most recent version of node JS running in your
-
1:02
workspace.
-
1:03
You can still follow along.
-
1:05
Node JS, like JavaScript, is backwardly compatible.
-
1:09
Anything you'll learn in this course will be good for newer versions too.
-
1:13
New versions may have new features, performance improvements, and
-
1:17
security updates.
-
1:18
Let's take a look at the file that we have here, app.js.
-
1:23
Now, I've called it app.js.
-
1:25
Other people use a different convention.
-
1:27
Some people use index.js or main.js.
-
1:32
It really doesn't matter what you name the file, because in order to run it,
-
1:35
you need to write out the filename.
-
1:38
To run the code in the app.js file, type node, then
-
1:43
the file name, app.js, and hit enter.
-
1:52
And it runs the file.
-
1:53
Well, there's nothing there in the file right now.
-
1:56
So having no output is expected.
-
1:59
We can clear the console as well, by typing clear.
-
2:04
Hit enter.
-
2:05
And now we've got some fresh space to type into.
-
2:08
Let's write our first Node.js application.
-
2:11
We can use the console object.
-
2:15
And the log method to print the text, Hello world.
-
2:23
Be sure to include a semicolon at the end of your line for completeness.
-
2:28
Save the file.
-
2:30
And let's head back over to the terminal.
-
2:32
We can press the up arrow key to the terminal to go back to the previous
-
2:36
commands we've run.
-
2:37
Press it once, and we see the last command clear.
-
2:41
Up again, and we see node app js.
-
2:45
Hit enter and it says hello world.
-
2:48
That is our first node JS application.
-
2:51
There's another two helpful methods on the console object.
-
2:56
There's error and dir.
-
3:04
The error method is ideal for logging out error messages.
-
3:13
Then we have the dir method.
-
3:14
When passing in a JSON object, it will print out the values and
-
3:19
the keys in a human readable format.
-
3:30
The DIR function is great for inspecting objects and
-
3:35
seeing what values they contain.
-
3:38
Save the file and move over to the terminal.
-
3:41
Hit the up arrow, hit enter, and run the app.js.
-
3:45
You can see that Hello world.
-
3:48
Oops, something went wrong and
-
3:50
that just an object is all printed out in a human legible format.
-
3:58
Node also allows you to experiment with JavaScript in the terminal.
-
4:02
Type node without any filename and hit enter.
-
4:06
Doing so, brings up an interactive JavaScript console.
-
4:10
A console like this is known as a read evaluate print loop or REPL.
-
4:15
As it reads your code that you enter,
-
4:18
it evaluates it or runs it, and then prints it out.
-
4:23
So I can type 1 plus 2 and the console reads and
-
4:26
interprets my code and evaluates it to 3 and prints it's out.
-
4:33
It can even set variables.
-
4:36
It can set the constant name, to equal Andrew.
-
4:43
Then, when I type name, Andrew is returned.
-
4:48
Now you've seen Node.js in action in two scenarios.
-
4:51
Firstly, you saw that you can write programs and then
-
4:54
run it with a node command and then the filename containing the JavaScript code.
-
4:59
Secondly, you can play around with node in the read eval print loop.
-
5:04
Where you interactively type JavaScript code and see what the results are.
-
5:09
To exit the REPL you can press Ctrl+D once or Ctrl+C twice.
-
5:15
What we've done is quite novel.
-
5:17
We've created a JavaScript program outside of the browser.
-
5:23
You've written your first Node.js application.
-
5:25
Writing an application in Node.js is writing a JavaScript application, but
-
5:30
outside of the browser.
-
5:32
Let's review what we've learned about the console in the next exercise.
You need to sign up for Treehouse in order to download course files.
Sign up