Check out a free preview of the full The Hard Parts of Servers & Node.js course
The "Modifying the Node Server" 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 lines of JavaScript code to modify the server by declaring functions to run on request and client error events.
Transcript from the "Modifying the Node Server" Lesson
[00:00:00]
>> Will Sentance: We set it up, now, let's use one of those methods, those functions, that gives us edit access. Which one are we gonna use, Roman? Next, can you see? Before that one, Virginia?
>> Virginia: Listen?
>> Will Sentance: Listen, let's use it. All right, let's first,
>> Will Sentance: Set up with listen, does it do anything interesting in JavaScript?
[00:00:22]
No, it's a function which we grab from here that has an edit access to this HTTP and therefore, this open socket. And it's going to do something in node, and it's going to set port.
>> Will Sentance: And that's what it does. Sets port to 80 which tells the computer's network to open up at port 80.
[00:00:47]
You're ready to receive measures why? Because whenever a browser opens a website it sends a message it defaults to entering at port 80. Okay, so this may again, see I'm sorry it's so repetitive. But it's just this, this is all that node is and we have to get it right, otherwise we just lose it completely.
[00:01:05]
Okay, but we get to use another edit function, which one are we using now Michael?
>> Michael: On.
>> Will Sentance: On, here we go. We're going to take from the server object the on function, which also has node edit functionality, can edit the underlying instance of the HTTP server that we've set up back here.
[00:01:29]
Server.on.
>> Will Sentance: And the way on works, and we figure this out by just going to documentation node. But the way it works is, actually, you know what? I'm gonna start off by saying, in the background here we always have built in to node. The fact that if a message comes in it will scream out request, that's always there.
[00:01:57]
It would scream out request, it will scream out client error, that's just built into node, okay? That was as soon as we set up the server, it had these ready. But they've got nothing to happen on them screaming it out right now, we need to set that up.
[00:02:13]
And it is literally these words, these are like keywords effectively, like node key words. And so we are going to set that up, we're gonna say that if node you scream at request, run, auto run, what function Sarah Rose?. If we scream at request and, do on incoming, fantastic.
[00:02:29]
Thank you. Do on incoming, there it is.
>> Will Sentance: What's this gonna set up? In node it's going to take this word, and this function, and set up down here a more nuanced auto running process. It's gonna say, if you get the word request screened out from node, then auto run what function, Sam?
[00:03:03]
>> Sam: DoOnIncoming.
>> Will Sentance: DoOnIncoming. Fantastic from Sam.
>> Will Sentance: There it is. Okay, good, I mean, it's very similar to what we've seen before, but now we did it manually. We could have actually paused to one and coming directly into create server, and it would have put request and do on incoming in the background, set up for us automatically.
[00:03:25]
The only thing that pauses each grade server means it didn't have the request in there, that's was just done for us when we did it before. Now we're taking more control. And we've taken more control so we don't know how it actually works under hood. As it turns out that these messages get flashed out also in interesting times.
[00:03:41]
Including if you get an error object, corrupted amount message, you get different message flashed out. Different events, so say it did include events and they're emitted a different event emitted and events have names the names request. I can prefer to consider my messages within node that are flashed out at certain times automatically when something happens.
[00:04:04]
Let's have a next one. Server door on, client error and we're passing this time, which function Virginia?
>> Virginia: DoOnError.
>> Will Sentance: DoOnError. Excellent, thank you Virginia. It was funny saying my sister's name. All right, she was the sister that was closest in age to me growing up, I have four younger sisters.
[00:04:30]
She was one who was as that picture at the start of with my background. Yeah, she was the she's working a very cool stuff. She works in the government, working on Brexit. No, she stopped doing Brexit she was too depressing. So now she works on environmental policy, which she's also notice environmental policy policy when Brexit happened.
[00:04:50]
So every piece of the government became. Okay, Brexit, there you go, that's my political commentary. Nicely neutral. Everyone is exhausted by Brexit, everybody, any person of any position. No, yeah, that's something we can all universally agree on. All right, so this one, is this on method, which doing anything that interesting in JavaScript?
[00:05:11]
No, I did my answers has no voice, so he need to say no. Good job Rich. All right,
>> Will Sentance: And there it is, it's instead doing stuff a note is grabbing that word and saying, hey, node if you send out the flash message the event emitted is called an error.
[00:05:31]
Because you notice the inbound requests message is corrupted, then please run, what function Virginia?
>> Virginia: DoOnError.
>> Will Sentance: DoOnError, fantastic, doOnError. And there it is.
>> Will Sentance: So not radically different, but we've now introduced a major feature of node, that of the event system. But what it does, I guess is gives a bit more nuance to how our auto-run functions actually end up running.
[00:06:03]
It depends on which event triggers which auto-run function. And we've got to take control of that.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops