In this exercise, you must create a basic web server with Express. The server will send back the index.html file when it receives a / request. It will also send back JSON data when a / data request is received.
- [Scott] Everybody has a repo. If you want to check out on Step-1. If you check that branch out, you'll see a couple of files in here. Let me get rid of this. You'll see a couple of files in here, so, you probably don't see node modules, that's because you need to do an MPM install. So you should get in the habit of whenever you get a new node repo, just run MPM install from your command line. Just start doing that immediately.
Whenever you clone, whenever you clone it, whenever you pull it, just do an MPM install. So what happens is when you run MPM install, it goes, and it looks at the package.json. It looks at the dependencies, and it starts installing these. All of the dependencies with their appropriate version numbers. That's what MPM install does, so that's how it knows what to install, and then it will create a node module folder for you and depending on what version of MPM you have, you might have all your node modules installed flat like mine are, cause I'm using MPM version 3, but if you're using a previous version of MPM, you're node modules will be nested, because what happens is, let's say, MPM goes to install this node module, body-parser.
Body-parser is a node module itself and has it's own package.json, which also has node modules, so MPM will install those too, and those modules might have modules. So we're recursively installing those modules, so that's why you might have nested folders in here, but if you're using MPM version 3, 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. It's just a bigger pain now having to scroll through the node modules folder looking for stuff, which you rarely have to do, but in the rare case that you do, it's a little more painful now.
So that's what happens when you run MPM install. So, run MPM install, and get the node modules working. 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 big TODO in here, and there's is like a jsonData object. So we'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.
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, order the 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". And then I also should be able to go to /data and see this jsonData output.
So now I'm going to show you what it actually looks like so I'm going to check out the solution and show you. Oops. And I'm going to run node server. So I'm running my server, I'm going to go to Locahost3000 it's the port that I'm running on.
And there's the html page, so we should see that if you go to the root. And then if I go to data, I should see some jsonData there. So I'm going to use some hints on how to actually get past this cause there's a little context. Let me check back out.
So in server.js, 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 using require, literally using the require function? You can require a module called FS and it has a method on it called "read file". It allows you to read files. So there's a file called index.html what we want to do is we want to go to read that 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. It also show you how to send a response. It doesn't show you how to read a file, though. So that's the part you got to figure out. And then, for the other thing as far as like sending data back on a GET request '/data, again if you look at the example, it shows you how to set up a GET request to a specific route it shows you how to send back a json response. The only thing you got to send back is just this data.
So other two things you're doing. And also remember you also have to set up, you have to have your server listed 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. So I want it to be hanging, I want it to stay on. I want it to be listening on a port that I can navigate to. And also, in the examples in the notes, it shows you how to do that as well. So if you have any questions on this, I'll be walking around. Anybody online, totally let me know. So you'll have 30 minutes to do this and then we'll come back. Mark.
- [Mark] There's a question on the flattened MPM system. What happens if two libraries require a different version of the same lib... - I haven't ran into that scenario with MPM 3 yet, but I know with the previous version of MPM, a little prompt will come up, or actually, I think they'll try to resolve it themselves I'm thinking of Byra, actually. Byra will ask you if want a prompt. I think MPM 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.
I'm not too sure, though. I haven't ran into that situation before. I literally just updated to MPM like a week ago. But that's a good question. I would really like to know what happens. But I'm sure they thought of it and they handled it pretty well. But I can see how the unexpected side effects if that were to happen, for sure.
This course was created by Frontend Masters. It was originally released on 12/30/2015. We're pleased to host this training in our library.
- Executing Node.js
- Using Express
- What is middleware?
- Testing in Node.js
- Using Mongo with Node.js
- Data modeling
- Querying data with Mongoose
- Identifying sensitive routes
- Configuring the deployment