This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Exercise 1" Lesson
>> Scott Moss: Everybody has the repo. If you want to check out on step dash 1. If you check that branch out, you'll see a couple files in here. Let me get rid of this
>> Scott Moss: You'll see a couple files in here. So, you probably don't see Node modules, that's because you need to do an npm install.
[00:00:25] So you should get in the habit of whenever you get a new node repo, just run npm install from your command line. Just start doing that immediately. Whenever you clone it, whenever you pull it, just do npm install. So what happens is when you run npm install it goes, and it looks at the package.json.
[00:00:44] It looks at the dependencies, and then it starts installing these. All the dependencies with their appropriate version numbers. That's what npm install does, so that's how it knows what to install. And then it'll create a node_modules folder for you. And depending on what version of NPM you have, you might have all your Node modules installed flat like mine are, because I'm using npm version three.
[00:01:03] Or if you're using the previous version of npm, your Node modules will be nested. Because what happens is let's say npm goes to install this Node module body parser. Body parser is a Node module itself and has its own package.json, which also has Node modules. So npm will install those too, and then those modules might have modules.
[00:01:21] So, we recursively install those modules, so that's why you might have nested folders in here. But if you're using npm version three, it all installs flat, which is good news for Windows users. Because I know Windows, it won't chase that forever, it'll start erring out, if It has to keep looking for those files so they fixed that.
[00:01:39] It's just a bigger pain now having to scroll through the Node modules foler them looking for stuff, which you rarely have to do, but the rare case that you do, it's a little more painful now. So, that's what happens when you run npm install, so run npm install and get the Node modules working.
[00:01:55] If that's not working for you, let me know, but it should totally be working for you. So, what you're going to do, if you look at the server.js, there's a bit to do in here, and this is like an adjacent data object. So, what you're going to be doing is you're going to be creating a basic server with express that will send back the index.html file on this directory on a get request to the root.
[00:02:22] It should then send back JsonData on a get request to /data as well. So, what that means is, one I should be able to come in here and start a server on whatever port you want. And when I start that server, if I navigate to that port in the browser, I should see whatever this index.html renders, which is just an h1 tag that says, hey.
[00:02:45] And then, I should also be able to go to /data and see this JsonData output. So Now, I am going to show you what it actually looks like. I am going to check out the solution to show you.
>> Scott Moss: Oops. I'm gonna run Node server. So, I'm running my server.
[00:03:18] I'm gonna go to localhost:3000. It's just the port that I'm running on, and there's the HTML page. So' we should see that if we go into the root. And then if I go to data, I should see some JSON data there. So, I'm gonna use some hints on how to actually get past this, cuz there's a little context, let me check back out.
>> Scott Moss: So in server.js
>> Scott Moss: First of all, there's a module inside of Node called FS, short for file system, and it allows you to read files. So, remember how I told you to require built in node modules, literally using the require function. You can require a module called FS, and it has a method on it called read file.
[00:04:20] That allows you to read files. So there's a file called index.html. What you want to do is we want to be able to read.file on a request to the root, on a get request to the root, and we want to send that file back. So if you go look at the examples in the notes, it shows you how to set up a route.
[00:04:36] It also shows you how to send a response, It doesn't show you how to read a file though. So, that's the part you've gotta figure out. And then for the other thing, as far as sending data back on a get request to /data. Again, if you look at the example it shows you how to set up a get request to a specific route.
[00:04:55] It shows you how to send back a json response. The only thing you got to send back is just this data.
>> Scott Moss: So, those are the two things you're doing. And also remember you also have to have to have your server listen on a port. So, by default ,if you didn't have your server listening to anything, when I run this file, it'll just execute and stop.
[00:05:16] So, I want it be hanging, I want it stay on, I want it to be listening on the port that I can navigate to. And also, in the examples of the notes, it shows you how to do that, as well. So, if you have any questions on this, I'll be walking around.
[00:05:28] Anybody online, totally let me know. So, you have 30 minutes to do this, and then we'll come back. Mark?
>> Mark: There's a question on the flattened npm system, what happens if two libraries require a different version of the same lib?
>> Scott Moss: I haven't run into that scenario with npm three yet, but I know with the previous versions of npm a little prompt will come up, or actually I think they'll try to resolve it themselves.
[00:05:51] I'm thinking of Bower actually. Bower will ask you if you want a prompt. I think npm does a good job of resolving those, but If I had to guess what the behavior was for the flat one, I think it might take the later one, whichever one you installed latest.
[00:06:05] I'm not too sure though. I haven't ran into that situation before. I literally just updated to npm a week ago. But that's a good question. I would really like to know what happens. But I sure they thought of it, and they handled it pretty well. But I can see you having unexpected side effects if that were to happen for sure