The Hard Parts of Servers & Node.js

Calling Methods in Node

Will Sentance

Will Sentance

Codesmith
The Hard Parts of Servers & Node.js

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

The "Calling Methods in Node" 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 explores the inner process of using Node's createServer method to auto-run a specified JavaScript function.

Preview
Close

Transcript from the "Calling Methods in Node" Lesson

[00:00:00]
>> Will Sentance: Node is gonna auto-run the code, wrap it in a function, that's how you save code to be done later on. Auto-run the code function for us when a request, they call these inbound messages from Sarah Rose's computer after we tweet, they call it a request for data.

[00:00:18]
When it arrives from a user, we're gonna save, look, there it is, we're gonna save code, we're gonna to call it doOnIncoming here. You can call it whatever you want. Save it, and then have it auto-run when a message comes in. Virginia, I want you to, forgive me, walk up to the screen here and point where you think we're telling Node, this is the function we to have you auto-run for us.

[00:00:46]
Do you mind walking up and pointing to where it is?
>> [INAUDIBLE]
>> Will Sentance: I'm gonna bet you will never forget after this moment.
>> Virginia: Where it's actually asking it to do something?
>> Will Sentance: No, where we're telling Node, this is the function we want you to auto-run. Point to it where it

[00:01:01]
>> Will Sentance: She's going-
>> Virginia: This is the function.
>> Will Sentance: That's where we save it, and where do we tell? Let's give her a hand.
>> [APPLAUSE]
>> Will Sentance: Thank you, excellent, Virginia, very nice.
>> [APPLAUSE]
>> Will Sentance: Exactly, spot on. All right, so, people, it is at that moment online. There it is, http.createServer.

[00:01:19]
Inside the parentheses, whatever we insert there, that code, which in this case is gonna be the whole code of the doOnIncoming function, the entire code of the doOnIncoming function, we gave it that label doOnIncoming, it's all the code inside of it, we're gonna tell Node, please auto-run this function when a message comes in.

[00:01:39]
This may seem incredibly like, God, yeah, yeah, I used one line of Node before. I've seen this code before, it's easy. But if we understand how it's actually working under the hood, everything else in Node, we'll see later on, follows just so naturally. All right, so there it is.

[00:01:52]
We're gonna have to tell, well, let's first save the function. Virginia, tell me again what we do in the first line here.
>> Virginia: The first line, we're declaring a function.
>> Will Sentance: Yeah, declaring a function, declaring a function, exactly. We'll leave all this out because it's the same code.

[00:02:08]
We're declaring a function.
>> Virginia: With the label doOnIncoming.
>> Will Sentance: doOnIncoming, yeah. We're doing it just into memory. We should probably draw this out again but it's okay. doOnIncoming, there it is, doOnIncoming, saved. That's how I represent my functions, people, when I save them. There it is, doOnIncoming. And then, next line, we declare server.

[00:02:33]
We did it earlier, so just the same, declare server. We then run http.createserver. Michael, what does http.createserver do in Node?
>> MIchael: It triggers opening the port.
>> Will Sentance: Opening the port, opening the socket, using the computer tunnels. Excellent from Michael, exactly. And then we have it auto-return an object, or it returns an object, into server that has a bunch of functions that allow us to edit the instance of http.socket.

[00:03:04]
But we do one more thing. Because when that message comes in, we need some JavaScript code to run up here. And we need Node, when it gets the inbound message, to trigger some code to go and run up here. Where again do I insert zep? Specifically, if you can be as precise as possible with your technical communication, where do I insert the function that's gonna be saved in Node that's gonna auto-run when Node gets an inbound message?

[00:03:33]
>> Speaker 4: The parameter of createServer function?
>> Will Sentance: I parse it, it is my argument, my input, the whole function definition to the createServer function call. doOnIncoming, that means all of its code is gonna be grabbed and thrown down to here to be the function that auto-run this function when there's activity, when activity meaning inbound, some sort of stuff happens, inbound message.

[00:04:06]
Auto-run what function zep?
>> Speaker 4: doIncoming.
>> Will Sentance: doOnIncoming, and there it is. Auto-run this function. Fantastic, why? We don't know when the inbound request would come. We don't know, and so we have to rely on Node to trigger running some code in JavaScript that we can look at. And secondly, people, and we're gonna see this again and again, JavaScript's single-threaded and synchronous.

[00:04:32]
All the slow work, like, well, it's inherently slow, I guess. When a message comes in, we've no idea when it's going to come in. All work like that, including speaking to a database, including speaking to the file system, they're never going to sit around waiting in JavaScript. Because it can only do one thing at a time, so it can only speak to that database until it gets a result from the database.

[00:04:53]
Instead, any task that will take a long time will be set up in Node and then have a function attached to it that will be automatically triggered to run when the background task either completes or has activity. We're gonna see this again, and again, and again.

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