Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "Calling Node with JavaScript" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Will explains what is occurring within the server when using Node to open a network channel that listens for incoming requests.


Transcript from the "Calling Node with JavaScript" Lesson

>> Will Sentance: We are finally goning to use our computers internal features and ideally open a network channel, a socket they call, an open channel to be able to receive messages coming from Sarah Roses Mac. We land in the computers internals, and we're gonna have access to it in JavaScript.

How, via setting up a Node C++ feature. How, via a built in label http.createserver. Let's start to see what it does in action. Here we go. So, I'm gonna start actually here, with the right-hand side for once. Http.createserver parentheses. Okay, folk, point to me on the board, everybody point to me, on the board, where is that gonna have a consequence?

In JavaScript? Might have a little bit of a consequence in JavaScript. In the computer's internal features directly, no. A-ha, exactly, where's my purple pen? This here is a command for Node internal features. To do what? To set up a network, we call it net feature of Node, specializing in HTTP protocol, okay, to receive HTTP messages.

Yeah, it's not that interesting, where it's really interesting is what it's gonna do in the computer's internals, with the help of libuv. Libuv is a bunch of pre written C++ code, technically built separately from Node, but its most prominent use is in Node, actually using lots of different libraries, but bunch of C++ code written to ensure that we can run Node, and we'll learn about how to run a little bit later on, on any machine, any operating system.

Linux, Mac, Windows. Libuv links up effectively between C++ code written in Node with any computer internal structure, whether it's a Mac, Linux or Windows. Okay, so it's going to, with the help of Libuv, set up in the computer internals and open socket, an opened channel to the internet.

Ready to receive HTTP messages and that`s it. That one line has opened. Our computer now Twitter's computer is now ready to receive messages. In one line, in one label. It opened that channel, in and out messages now, it is good to go. One issue, one issue. I actually, you know what I wanna do?

There are actually 64,000, this is slightly metaphorical, there are actually 64,000 entry points to my computer. Now it's not literally what it actually is, is I can use 64,000 different numbers from 1 to 64,000 to represent, to indicate, or to mark off, an entry point into my computer.

Not actually to carve out a little hole within the computer but I can give 64,000 different numbers to represent an entry point to chanel in my computer. Well, that's a bit of an issue, because when a say Rose sends out the message saying, hey, needing new Tweets, need Tweets, need the HTML, CSS, Tweets, images, etc.

Which entry point is she gonna come in at? Anyone know what the default, they call them, not entry points, they call them ports. Anyone know what the default entry port is for any HTTP message being sent from a browser to a computer that receives messages known as a server.

Anyone know the default entry point is? Yeah Mark.
>> Mark: 80.
>> Will Sentance: 80? It's 80. The entry point, it will default to be trying to arrive at, it's gonna try and arrive at Port 80. Well down, we already set up the background Node C++ feature. This instance of the HTTP feature of Node that set up this socket, how the hell do we continue to edit it and tell it actually you know what, specify the port to be 80.

Or node realizes that we're not gonna do all of the commands for the underlying C++ feature in one line. So what does it do? The other thing that http.createServer does is immediately, and this is crucial people, in JavaScript, it immediately in JavaScript returns out, it's a function. It's a pseudo function cuz really it's triggering stuff down here, but it immediately returns out into whatever's on the left hand side here, which is the label what Sarah Rose, whats the label on the left hand side?

>> Speaker 3: Server.
>> Will Sentance: Server, exactly, it immediately returns out into it, an object full of functions, methods, they call them when they're an object, methods, including ones like listen. All of which, when run, will allow us to edit the instance of the HTTP featuring Node that we've set up.

Give us edit ability over it. And that's exactly what we're gonna have to use, because we need to tell Node, to tell the socket to set up to listen at, what point Michael? But what port sorry?
>> Michael: 80
>> Will Sentance: At 80. And we can do that because we can go and grab that listen function, that's we're gonna do on the next line, grab that listen function pass it 80.

And because this listen function is really a trigger to edit stuff in Node, is this gonna do anything in JavaScript? Nothing that interesting, instead it's going to st up in Node. That's the famous purple N, from Node, so if a Node the feature to have the port be port 80 which tells our socket to open.

Open the door at port 80. Folk, we're now into receiving messages. If we go to, send a message to his computer, now at this point, it will land, it will be passed through into Node. In two lines this was done. Now, a lot of shit's going on behind the scenes, a lot of stuffs going behind the scene but it's two lines in JavaScript, to trigger a ton of, this is super sophisticated, opening a channel to your internet at a specific entry point?

Super sophisticated. We do it in two lines. Okay, in comes the inbound message. Inbound, you know, incoming message from Sarah Rose. In comes, hey its unforgettable. All right there incomes a message, weee, will libuv, it lands in Node. But here's the problem folk, this message,
>> Will Sentance: We wanna look at it.

See what it's asking for and send back the right tweets. Inbound by request, run code to look at it, you're gonna run code to look at introspect it, see what says. Something like this I guess. You wanna run some code like you know, if incoming message, then is pseudo code, then send back tweet data.

But folk, I wanna run this code in JavaScript, I don't know C++. When is this code gonna need to run? In fact, I've no idea. So Rose's, turning on her computer entering, at any time, day or night. Any time. When is this code [INAUDIBLE], if I run this code where I turn my jobs are done, jobs are single threaded asynchronous, it hits this line, and there's already a message of five milliseconds in, of when we hit this line, or not even that, 100 nanoseconds in, it's gonna hit this line and go, incoming message?

No problem, send data back. But, for the rest of the time, we've passed this line and we don't ever see it again. So, incomes the message, and there's no JavaScript code there to run. I mean, in theory we've been had this line run just again and again and again, like billions of times.

So, whatever message comes in, it's running. His next line's gonna run. That means we can do one thing in our app. It's not a very good model. To be able to only receive one message and had to run the same line just in case the message comes in any moment.

>> Speaker 5: [LAUGH]
>> Will Sentance: So we don't do that. But that leaves us in a bit of a conundrum. Sam, who does know? We don't know. But who does know when an inbound message comes in?
>> Sam: Node knows.
>> Will Sentance: Node knows. That's great. Node knows. And so, perhaps, we're going to rely on Node to automatically run this line of code for us.

Beyond that control. But how could we bundle up the code in order that it could be triggered to auto run by Node. How do we bundle up Mohammed?
>> Mohammed: Function.
>> Will Sentance: In a function. And that's what we're gonna do. People, again and again, we're gonna bundle up code in a function that we want to have auto run by Node to do stuff like look at incoming message, send tweet data back, when Node here is with the help of libuv that an inbound messages arrived.

How people, but what about what moment with my favorite emoji. The woozy emoji. I, you know that emojis having a lot of a lot of fun. It's very, it's, if any one says this emoji, you know they're having a level of fun.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now