Transcript from the "Calling Methods in Node" Lesson
>> 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?
>> 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
>> 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.
>> Will Sentance: Thank you, excellent, Virginia, very nice.
>> 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.
>> 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.
[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.