This course has been updated! We now recommend you take the Complete Intro to Real-Time course.

Check out a free preview of the full Real-Time Web with Node.js course:
The "Hello World" Lesson is part of the full, Real-Time Web with Node.js course featured in this preview video. Here's what you'd learn in this lesson:

For this first example, Kyle begins by adding some code into "1.js". He demonstrates the way Node provides a hosting environment for the JavaScript code it compiles.

Get Unlimited Access Now

Transcript from the "Hello World" Lesson

>> [MUSIC]

>> Kyle Simpson: We're gonna start where everybody starts when they learn something, we're gonna start with the Hello World, okay? But we're gonna get progressively more and more throughout the rest of today, we're going to get progressively more and more sophisticated about our Hello World. Okay, so what I want you to do, [COUGH] Excuse me.

[00:00:21] We're mostly done with the slides by the way at this point there's a few more on WebRTC but the slides are pretty unimportant from here on out. So switch over to your favorite code editor and inside of that exercises folder or wherever you're gonna work from if you've made a copy of it.

[00:00:37] Inside of that folder, you should see an empty file called 1.js and I want you to open up that in your editor. You can see there that I put in a comment that says we're finally gonna write some you node. So every time I go somewhere and I teach this class or I talk to people about node and I hear this common thing where people say yeah, yeah I know about node.

[00:01:00] Yeah, yeah I've seen it, I'm pretty confident with it and then I ask them have you ever written any code? Well no, but I wrote a blog post about it so I feel pretty confident. I know JavaScript, so therefore I know node. I'm not saying that any of you are actually in that position but I would suggest to you that by the end of today you will have actually written node code, not just pretended to have written it, you will actually write it.

[00:01:26] So we're going to start out at the very beginning where all programs should start with Hello World. And I think you'll see that this is a significantly simpler Hello World than most people are accustomed to. I want you to write console.log("Hello World"), okay.
>> Kyle Simpson: And save that file and then from your command line, be in that same directory.

[00:01:52] I'm inside of my directory, let me make this bigger so it's a little easier to read for you. I'm inside of my [COUGH] I'm at my command line inside of that directory where my 1.js file is and I'm going to say node 1.js and I should get a Hello World out.

[00:02:11] And I want for you to see the same thing so jump up and down and scream at me if you're not seeing Hello World printed out.
>> Kyle Simpson: Well that's it, that the end in the workshop you've written some node code. So we can all go home because everybody's now confident with node right?

[00:02:43] I'm just kidding.
>> Speaker 2: [LAUGH]
>> Kyle Simpson: All right so not terribly interesting that we were able to write a console log statement but why did I start there? Well the reason I started there is because I think there are some misconceptions about how the node actually works. And you notice that I don't have any environment whatsoever.

[00:03:03] There's no DOM, there's no page, there's no doc type, there's no script tags, there's no nothing. I just have a single JavaScript file with a single line of code in it. How is it possible that that single line of code is capable of printing something to my screen?

[00:03:20] Have you ever stopped to wonder what must be underneath the covers for a node to be able to expose to me the capability to do something like I/O? And that's where I want to start with introducing node because the important thing to understand about node is it's not just a JavaScript engine.

[00:03:41] You can take the V8 engine, which is what node has done, you can take the V8 JavaScript engine and you can compile it and you can run it on your server. And you can even feed in some JavaScript to it and it will duly do that code. But guess what you cannot do with V8 alone?

[00:03:59] You can't see anything that it does. Because V8, as a JavaScript engine, the JavaScript engine itself has absolutely no concept of what I/O means. It doesn't even make sense for the JavaScript engine to know what I/O means because look at all the different various places it can run.

[00:04:16] It can run inside of a Raspberry Pi, it can be embedded inside of a lightbulb, it can run inside of a browser, it can run on a server, on a phone and ten million other devices in between. Every one of those as a different concept of what I/O even means.

[00:04:30] So the JavaScript engine itself doesn't have any concept of that. So node, what makes node special or important, is that node is providing a hosting environment for that JavaScript. Your browser provides a hosting environment for that JavaScript. The Raspberry Pi SDK provides a hosting environment for that JavaScript.

[00:04:52] And what it does is it extends the JavaScript from its pure native sense, the kind of sense that we learned about yesterday, into an environment that lets you do stuff with it. Like write to the screen or open up and read a file, or make an ajax request, or whatever, those environments are what extend stuff.

[00:05:13] So if you think about the way JavaScript works in a browser, there's a whole bunch of things you do inside of the browser which you may have taken for granted as well that's just JavaScript. And it turns out, well, no, it's not just JavaScript. It's actually an extension to JavaScript provided to you by the host environment.

[00:05:29] When you do console.log inside of your browser, the browser and the developer tools have provided an I/O hook that allows for console.log to work. And the same is true of node. Node has taken the standard input output stream, and it's given you a binding from lower level C layer into JavaScript.

[00:05:51] So that when we say consule.log, we're actually printing something to the screen. Let me prove it to you. I want you to take out the console.log part and I want you to instead do process.stdout.write.
>> Kyle Simpson: Save that and if I haven't mistyped something, go back to your screen and type in node 1.js again.

[00:06:18] You should notice something slightly different, does anybody see what the difference is? Theres no ending new line or carriage return or whatever. That's because the standard out stream doesn't have any of the semantics built into it like console.log is. Console.log is essentially a write lin, it takes whatever you write in and adds a new line onto the end of it.

[00:06:40] But we can actually deal directly with the standard I/O stream, the standard out I/O stream by saying stdout.write. And you probably could guess that we could also do and we could read a character from the standard in input, if we were to pipe some input into our node program.

[00:07:04] The way you do with node you could pipe a character in and you could read it using standard out standard in. There is also a standard error for the standard output. So in a very real sense in terms of mapping to Linux semantics, it's very low level, it has that sort of politics style interface exposed through JavaScript APIs.

[00:07:24] Console.log is a wrapper on top of process.stdout.write. And it takes whatever you console.log and adds a new line onto the end of it.