Real-Time Web with Node.js Hello World
This course has been updated! We now recommend you take the Complete Intro to Real-Time course.
Transcript from the "Hello World" Lesson
>> 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: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: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: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 process.stdin.read and we could read a character from the standard in input, if we were to pipe some input into our node program.
[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.