Introduction to Node.js Introduction to Node.js

Asynchronous Server Exercise

Check out a free preview of the full Introduction to Node.js course:
The "Asynchronous Server Exercise" Lesson is part of the full, Introduction to Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to refactor and fix a simple static asset server.

Get Unlimited Access Now

Transcript from the "Asynchronous Server Exercise" Lesson

>> Speaker 1: So now, what you're going to be doing is, you're going to be refactoring and fixing a simple static asset server. The server uses blocking synchronous code and crashes whenever a request comes in that it's asking for an asset that is not found. That kind of sucks. We don't want that.

[00:00:14] We just wanna get a 404, instead. Neither of those are desirable. So the blocking code is not desirable, and the crashing when the file doesn't exist is not desirable. So you got to fix that. You also have to teach the server how to serve the assets it has in the assets folder.

[00:00:30] So here are the things to do. Install all remote modules. Check the readme on how to run your server. Refractor the sync file reading code to be async with a callback, a promise, or a async await. Prevent the server from crashing when an asset is not found. Instead, respond with a 404 status code.

[00:00:50] Create some sort of router logic to handle incoming request for assets. That's a lot, I know. But I promise you it's not that hard. I'm gonna walk you through some of it right now. So basically, if you come into exercises, you go into api. You're gonna see some some stuff in here.

[00:01:06] You're gonna see your file called server.js and a folder called assets, assets is gonna have two files index.html and a style that dot CSS. Server JS is using that HTTP module that I said you would never use except for today. This is it. [LAUGH] They're never going to do that today.

[00:01:25] But we're creating a pretty simple server here. And what you are going to have to do is if you take note, it's pretty simple what's happening here. It's creating your server. This server comes with a request and a response object. The request is what's coming in. The response is an object to help you respond to the request.

[00:01:44] You don't really need to understand what those two things are doing other than that. And then what we do is we use the internal URL module that's built in to node, to just parse the URL and we get the path name, which is like the route. What route are they trying to hit?

[00:01:58] So this gives us the route. And then we have to do some if checks. If this route equals slash, which means they are trying to go to index, we're just gonna send back a 200 with a content type of text.html, and we're going to find the asset called index.html, and we're gonna send it back.

[00:02:15] And we're just gonna log before we do that. So what you're gonna have to do is a few things. To find asset function, if you go look at it, takes in an asset name. And then it uses fs.readFileSync to go find the asset folder and return it. That's blocking.

[00:02:31] We don't want that, we want that a synchronizes so that other requests can come in. We want to be able to request other things cuz the browser is gonna read this index file, and then index file if you go look, there is a link tag to some css.

[00:02:45] So it's gonna, the browser going to see this in the request this too but it is gonna be blocked because it's waiting for the original one to be done. You probably won't see it blocked because your own local and super fast, but this was deployed in there many requests coming in.

[00:02:55] It will be blocked until they read that from memory. Especially this a single file was big. But Noah's pretty fast, so you probably won't see the blog. But either way, that's gonna be bottleneck if we deployed this with millions of hits. So, you need to turn that into asynchronous.

[00:03:09] So don't use the file sync, make this asynchronous. So promises, callback, async/await, whatever you wannna do, and you need to handle it here. So that's one thing. The other thing is we're only handling if it's an index file, but what if style css comes in, which it will, cuz the browser's gonna ask for that.

[00:03:24] You need to handle that, too, and send that back. So this is where you would create some type of router object. That sounds fancy, but trust me, it's very easy. You could think of an object, think of some key lookup, some constant time machine, some state machine where keys are passed.

[00:03:41] Values are passed to assets like you can you can you can make it work the problem is if that route isn't this it will throw an error. We don't want that. We just wanted to say 404 instead. So if you look at what's happening with rest dot right head and rest are right, you can kind of put together how to send back a status code with some headers and actually how to send back a value.

[00:04:00] And then how to enter a request. You can put those two together. So once you've got that, all you gotta do is run the server which if you look in the readme, the way to run the server is just gonna be this command node exercises server.js. So if you run this, oops.

>> Speaker 1: If you run this server, you'll get server running at this basically localhost. So localhost 3000. So if I go to localhost 3000.
>> Speaker 1: I will get this, and it shouldn't look like this, since there should be some CSS that's loaded up, and that's why you got to fix it.

[00:04:35] So if I go look at the console, I'll see some errors here. It tried to get styles.css, but it freaked out and it broke. And it looks like the server crashed. So, yeah it looks like route not found. The server crashed because it looked for a css file and it wasn't there.

[00:04:47] That's not desireable. So you got to fix that. So, that's what you're going to be doing.