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

The "Creating a Server Under the Hood" 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 discusses what occurs when HTTP messages arrive in Node and how Node sets up two JavaScript objects automatically, one for the received information and one to send back to the client.

Preview
Close

Transcript from the "Creating a Server Under the Hood" Lesson

[00:00:00]
>> Will Sentance: Node inserts two inputs, two arguments automatically. One is going to be, well, let's see it, let's just see it. Here we go, here it is, let's have Sara Rose's message come in. Here comes Sara Rose's message. Here it comes in, she's asking for what tweets?
>> Sarah Rose: Sorry, we're asking for the Node tweets.

[00:00:22]
>> Will Sentance: The Node tweets, great long distance reading there, Sara. Incoming, here she comes. We'll learn more about how that's formatted, that message, so we can really investigate it, but for now, let's just know this. In comes a message, it arrived in Node, but problem, people, big problem. This message is formatted in a way that I definitely do not want to have to deal with in JavaScript.

[00:00:47]
It's a string of characters, so it's an HTTP message. I can actually get access directly. If I instead set up my open channel using the net, N-E-T-, built in label, then the data that would come in, I'd actually get it back in here in just the pure stream of characters, the actual message itself.

[00:01:07]
But because I set up this background feature, well, before we see that, before we see that, just a second before we see that, in comes the message. What do we want to have happen at that moment, Zep? Before we talk about any inputs, what we wanna have run at that moment, Zep?

[00:01:22]
>> Zep: Do incoming.
>> Will Sentance: Do on incoming function. Let's take it up here and it's going to, look at this, people, it's gonna come into into JavaScript, and folks, I'm gonna do everything that Node is gonna auto do with the purple pen. So Charlie, what's the purple pen, the Node pen, the automatic not done by us pen, gonna have happen in JavaScript at this point?

[00:01:48]
It's gonna execute code, so it's gonna need what, Andrew?
>> Andrew: Parentheses.
>> Will Sentance: Parentheses, Charlie, you're losing your job.
>> Charlie: [LAUGH]
>> Will Sentance: Parentheses, put parentheses on the end and execute that function. So we're gonna see this again and again and again cuz it's all that Node does. So we're gonna see this again, so we'll get very, very used to to this model.

[00:02:08]
But this function is auto running, purple means it's gonna have, in comes a message, and it's going to auto run this function. What does that mean? Grab that code, throw it into JavaScript, and start running it, and there it is. And what we want this to do, well, in theory, you want it to send a message back to Node to then send a response message, another HTTP message back to, say, Rose's Mac.

[00:02:37]
How are we gonna do that? We better have inside this function's code access to this inbound message, and somehow, access to this message going back so we can add stuff to it, some data, some tweets, some HTML, some CSS to send back. Do I get these two strings of characters directly available to me in here?

[00:02:58]
I actually don't, because Node wants to make my life easier. So instead, and this is the other big part of the process, we're gonna see this again and again, folks, so don't worry. Instead, the next thing it does down here in Node, as soon the message comes in, it's immediately going to set up a HTTP message ready to send back by the way.

[00:03:20]
But they're in a format that we don't wanna deal with in JavaScript. JavaScript is not great at parsing text, actually, it's not too bad at parsing text, but we'd rather not deal with it. Parsing, P-A-R, parsing text, it's not willing to do that. So instead, Node is going to automatically package up two JavaScript objects for us.

[00:03:45]
I'm gonna do them in purple because they're being setup by Node, but they're JavaScript objects. It's gonna auto-create them, number 1 and number 2. They're not going to get names by Node, by the way, which is gonna be weird, how are we gonna access them? Sean, do you know how we're gonna access if we don't get names for them?

[00:04:06]
What word would you repeat?
>> Speaker 6: Parameters.
>> Will Sentance: Yeah, but we'll see that in a second, all right. So it's gonna automatically create, when the inbound message from Sara Rose arrives, it's gonna immediately automatically create two objects. And these are the most important objects in all of Node. Folks, you may be like, my God, why don't I just build my nice pretty app?

[00:04:28]
But if we understand these two objects, we have everything we need to be successful Node developers. I started off my telling you about that Samer Buna guy, who like the Node authority. He's got a book called Node Beyond the Basics, and a list of 27 questions, interview questions that if you are asked in a Node senior interview, you need to know the answers to get a senior Node job.

[00:04:51]
We are gonna answer, and already, by the way, already we've hit three or four of them, no, more than that, seven of them. And I'm making that up, I have no idea, but many of them already because this is what's going on behind the scenes, two auto created objects.

[00:05:02]
The first one is gonna package up for us in a nice JavaScript object format, cuz we love JavaScript objects, they're really easy to access and get stuff out of. So we package up the important information from the inbound message. What's the most important information we got from the inbound message, Michael?

[00:05:19]
>> Michael: The body?
>> Will Sentance: Well, there's no body here, we'll see that later on. What's the-
>> Michael: The URL.
>> Will Sentance: The URL, and it's gonna give it a little property on this object called URL, which is a string of, well, not the not the domain, just the path which is the next bit here, the specific bit, it's slash node because that's what Sara Rose wants.

[00:05:38]
She wants the Node tweets, and it's gonna literally, Node, and I'll do an N to show you that Node is doing this, is gonna look into that HTTP text message. This is a trigger text, and grab out the URL, it's gonna parse it and grab out the URL and stick it in this object.

[00:05:55]
And that object is gonna be [SOUND] inserted a magic word automatically into this execution context. It was automatically created by Node, automatically putting parenths on the end of the function that we passed to it, do on incoming, to say when you get an inbound message, auto-run do on incoming.

[00:06:18]
And there's two parts to auto-running a function. One is running its code, and the other, raise your hand if you know, Virginia?
>> Virginia: Inserting the argument.
>> Will Sentance: Inserting the argument, which is good which is as good as saving data, that's what it is. It's inserting data to make it available in here and saving it in here, yeah.

[00:06:32]
It's not a crazy thing to say, but so that Matt wasn't wrong, but specifically, Matt was wrong, but specifically, it's inserting arguments. Okay, here they come, the first one is that first object with all the data we need. Let's stick it in, it's in the local memory, it comes in Node, people, without a label.

[00:06:55]
So how the hell are we meant to access a property of an object that has no label, Michael?
>> Michael: We have to call it something.
>> Will Sentance: Which we do. We give it a placeholder, we know when we say the do on incoming function, we now that when it gets run, it will have automatically, by Node, inserted two objects.

[00:07:14]
And so we give it two placeholder names that those objects will be accessible by. And we can call them whatever we want, as long as when we then try to refer to them inside the function, we refer to them with the right label. And so Michael, what do we give the object that's gonna be auto inserted, the first one with all the data?

[00:07:34]
What do we give it, what do I give it?
>> Michael: Incoming data.
>> Will Sentance: Incoming data, I don't call it record. We'll see that's what we always used for the term in general req, or requests, but I don't want you thinking that's like a keyword. It's not, it's whatever we wanna call it, okay?

[00:07:50]
There it is, incoming data, and what gets inserted? This whole object with a whole bunch of properties, by the way, I just put in only the URL, but it's a whole bunch of properties added on here, headers, body. Yeah, body is captured an interesting way which would we might see later on.

[00:08:12]
But all that data is on there, okay, I've got all the data. But now, I didn't bring all that data in from Sara Rose's request to not send some data back. My little message here, let's show then it's ready to go back, it's sitting there, it's sitting there ready to send back, Node made it for me behind the scenes.

[00:08:32]
It's sitting there, I wanna go back. How the hell do we get access to it? Well, wouldn't it be nice, people, if the second object maybe had a property on it called, I don't know, maybe data. Something like that which we could then, hopefully that object would go in here and then we could access and add value to it, add value to it and send back.

[00:08:55]
[SOUND] It's not how it does it for some reason. Instead, here's what it gives us. It does give us an auto created object automatically created by Node that, yes, is inserted in here as the second input. But that object does not have a property on it that we can add data to that then gets sent back.

[00:09:19]
Instead, that object has a bunch of, a ton, but we're just gonna use one for now, of functions, methods, including one called end JavaScript labels, for when we run this function. So this object is gonna get thrown back into Java script. We are then gonna grab the end function, run it, and whatever we pass to it as its input will be used to add data to this response message.

[00:09:58]
This object has all the data and we can grab it directly. This object has functions that when run from JavaScript will be labels to update the data on the message that gets sent back. Fundamentally different behaving objects, one has actually got the data on it we can access.

[00:10:16]
The other has functions that when we run them from JavaScript will be labels back into Node to add stuff to this message to send back to Sara Rose's Mac. Let's see it in action, in comes the second auto created object. In it comes, and this one gets what parameter name?

[00:10:38]
Rich, what's the parameter name for the second inserted object?
>> Rich: Functions to set outgoing data.
>> Will Sentance: Fantastic, fantastic, functions to set outgoing data. Is this a key word, Rich?
>> Rich: No, it's not.
>> Will Sentance: No, it's just whatever, it's just cuz they came in, the auto created objects are anonymous.

[00:10:54]
They got no name, so we better have given them placeholders, we did, no problem. In comes the object, it has on it a whole bunch of functions. But one of them is end and I think I have a feeling we're gonna use that function. And look at that, what are we gonna look for, Zep?

[00:11:14]
Which object by which parameter name are we gonna look at?
>> Zep: The end?
>> Will Sentance: Which object, which one, it's the functions to set outgoing data object, and then we're gonna look at what function on it?
>> Zep: The end.
>> Will Sentance: The end function and that's exactly what we're going to run.

[00:11:32]
Functions to set outgoing data .end, and whatever we passed to it, which is here, welcome to Twitter, say welcome. This function, .end, when run, has an intimate connection back to Node. It's a Node function, and it's going to send a message into node down to this HTTP message, where it's going to add welcome to the message.

[00:12:09]
And then that message is going to be sent back to Sara Rose's Mac.

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