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

The "Preparing for HTTPRequestObject" 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 walks through the execution of JavaScript code that sets up the server to receive HTTP requests.

Preview
Close

Transcript from the "Preparing for HTTPRequestObject" Lesson

[00:00:02]
>> Will Sentance: So JavaScript code, JavaScript code triggers virus labels code written in?
>> Speaker 2: JavaScript?
>> Will Sentance: Node C++ features, there they are.
>> Will Sentance: There they are. But we don't just wanna do it in Node with C++, they're really only their choice so we can control?
>> Charlie: The computer.
>> Will Sentance: The computer's internals, exactly, thank you to Charlie.

[00:00:36]
The computer's internal features, there they are, perfect. All right, which includes features like networking, yeah, that's a networking feature for now. All right, now we're gonna write Javascript code to control and to setup an open channel to the internet hopefully. Let's do it all together, let's see if we have the core part of that Node down.

[00:01:10]
No pressure people but could call on any of you, at any moment. [LAUGH] Excellent. All right, Virginia. Line one, what are we doing, left hand side first, declaring what?
>> Virginia: A label called tweets?
>> Will Sentance: Beautiful. And the right hand side and yes, we are gonna go through each element.

[00:01:36]
First element we're adding to the array is? next element we're adding to array is?
>> Virginia: Hi. Laughing face.
>> Will Sentance: Laugh, I love seeing what each person calls it, it's definitely laughing, crying emoji. Jenny was wrong. There you go, laughing, crying
>> Virginia: And a more formal hello.
>> Will Sentance: I don't know how to draw this.

[00:02:01]
>> Mohammed: [LAUGH]
>> Virginia: I mean you made your own slides, so.
>> Mohammed: [LAUGH]
>> Will Sentance: It's better than normal.
>> Mohammed: [LAUGH]
>> Will Sentance: Mohammed's got feedback on my art.
>> Mohammed: [LAUGH],
>> Will Sentance: You'd like to come, [LAUGH] All right. Okay, next one, yeah go ahead Virginia.
>> Virginia: The more formal hello.
>> Will Sentance: More formal hello, exactly.

[00:02:28]
And then the next one?
>> Virginia: A waving hand?
>> Will Sentance: Hand waving emoji. So I'm gonna draw this one really well, no, that's, that's very creepy hand isn't it? Okay, I'll fix that. This is important material people. Okay, here we go. There it is, little hand waving emoji and the last one.

[00:02:49]
Goodness, I can't draw this, is the snapchat the ghost emoji. It is.
>> Will Sentance: I should learn how to draw this, guys. There he is. I'm actually putting way more effort into this than normal. There he is, okay, we saved our Tweets, our data. Thank you very much, thank you everyone, no?

[00:03:10]
No round of?
>> [APPLAUSE]
>> Will Sentance: Muted round of applause, did you put that in your annotations? More muted round of applause.
>> Virginia: Wasn't gonna clap?
>> Will Sentance: It was less than that, it was, yeah, exactly, that's true. All right, that's our data saved, Tweet saved. In our little JavaScript memory, which by the way, you're probably thinking, that's a bit of a weird place to store data in the JavaScript memory.

[00:03:32]
I have a feeling we might get it from somewhere else later on, we'll get to that. William, next line.
>> William: Then you're making a function definition called do on incoming,
>> Will Sentance: Do on incoming, how are we going to run this William?
>> William: Yeah.
>> Will Sentance: Are we developers ever gonna run this.

[00:03:50]
>> William: Are we going to know
>> Will Sentance: No. Definitely your first point yes exactly as well, but we don't win runs ourselves ever. We're instead going to tell no. Please run this, if you get inbound request. Perfect, all right. Next line, left hand side, Matt.
>> Matt: Next line would be we just did do it incoming, you were going into the function.

[00:04:16]
>> Will Sentance: We don't go inside a function until we do walk
>> William: Its the next line so server equals
>> Will Sentance: Excellent so to be clear. So how do we say the left hand side. There is a low quality technical communication Matt? How do we say the left hand side they're conserving sort of fancy technical communication anyone wants to give a shot?

[00:04:34]
>> Speaker 8: Label.
>> Will Sentance: Yeah.
>> Speaker 9: Define,
>> Will Sentance: You define a constant server, yeah, there it is to find a constant server declare it, okay. Now things get interesting people, server is gonna be the output in JavaScript of calling create server, and passing to the entire do on Incoming.
>> Will Sentance: The whole function definition does three thing.

[00:05:07]
This http.create server does three things, two in Node.
>> Will Sentance: One in JavaScript. Do also tell me one of those three things that it does? Yeah I fretted about it with Jeff
>> Jeff: Jeff, yeah, so he's going to set up an incoming message in C++.
>> Will Sentance: Yeah, it's going to in C++ set here an instance of the HTTP feature which is really just a network connection, with that code having a C++ that's gonna happen in the computers tunnels.

[00:05:52]
Where yeah, it's gonna set up an open channel to the internet, was there even challenge in the internet in networking Notes?
>> Speaker 8: Word.
>> Will Sentance: Not quite?
>> Speaker 9: Socket.
>> Will Sentance: Socket, exactly. Socket, perfect. Yeah, that's thing number one it does. Andrew, what's the next thing it does?
>> Andrew: Sets up some memory space for incoming data, and some member space forward the labor functions is set out going data.

[00:06:23]
>> Will Sentance: Not really until we get a message in, what's the next thing is gonna do? This is tough stuff. Virginia.
>> Virginia: Is gonna sup afrea to run the function doing the coming.
>> Will Sentance: Is going to take the dual incoming function and it's going to store to the autorun, when what happens?

[00:06:44]
Raise your hand if you know what, yeah, Mohamed.
>> Mohammed: When you get an incoming message.
>> Will Sentance: When you get an incoming message, spot on. So let's grab the do and incoming function and throw it into node. Okay, autorun do on Incoming, yeah people. This is a complex paradigm, but by the end of it you're going to be bored of it, because it's always this for the rest of the Node.

[00:07:11]
Whatever background feature we right now using the networking feature, file system feature, same paradigm, same structures, same process. Okay, so thing number one it does, set up this background feature. Thing number two it does, store function to be autorun. Okay, thing number three that it does at this moment is in JavaScript, who wants to tell me what it does in JavaScript?

[00:07:35]
>> Zep: Get a server object.
>> Will Sentance: That's an object which has a bunch of functions including listen and this function, all the functions that came out at that moment in that object are edit functions. To edit this background node C++ feature if we want to do well what do we want to edit Charlie?

[00:08:06]
>> Charlie: The port number.
>> Will Sentance: The port number. It doesn't default it would be nice, it would be easier for me to sort introduce this concept if it defaulted to a port, we have to set up our port. And that part we're gonna, so we're gonna use a lessen function which is linked so to Node.

[00:08:23]
Is it really a node functions or node labels, labels for node features. And this one is a listen one, we're gonna grab the listen function and pause it, 80. Who wants to be the lucky person? Who wants to come up here and draw me a line in purple, for where this listen function is going to actually have its consequence?

[00:08:50]
Michael my friend, up you come, you're the lucky to my favorite new interactive step. Give me a line were on the board it's going to have an effect, this server don't listen function. See you doing. Here.
>> Speaker 2: See, and there. Fantastic fantastic Thank you, Michael. The server not listen is having effect down in node, and it's setting what Charlie?

[00:09:19]
>> Will Sentance: Setting the port number.
>> Speaker 2: The port to 80, which is then doing that right here and opening up our entry point to our computer. And that little bit here, speaking to the underlying network, is done by C++ code, but it's a specific library of, it goes into node.

[00:09:35]
What's the name of that library?
>> Will Sentance: Libuv.
>> Speaker 2: LibUV, there it is. Okay, well done everybody. Three things, one setup the actual HDP feature of node that sets up the open socket. Two, several function to auto run by passing it into create server, into node, in auto run if inbound message comes in, three.

[00:10:00]
Setup in the JavaScript label server, an object full of, let's call them edit functions, functions that are node edits. And when you run one of them, as Michael rightly showed, it directly links you back to node to set up the port to be 80 which affects it can use internals of either port 80.

[00:10:18]
Excellent for Michael, very good of him to come up as well.

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