Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed typo libray -> library #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lessons/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ console.log("listening on http://localhost:3000");
Alright! A bit here but let's work it out!

* The `getRandomComplement` function returns a random item from the `complements` array. `Math.random()` gives you a random number between 0 and 1. We take that and multiply that by how long the `complements` array is (which is cool because we can add and subtract items from the `complements` array and not change this function). This is basically the same as saying we're going take a 0%-100% randomly of the array length. We'll end up with a number like `2.16231583252359`. We need that to be a whole number so it's a useful index, so we say `Math.floor()` which gives us that number rounding down, so `Math.floor(3.9)` is 3 (there's also `Math.ceil` to round up and `Math.round` to round how we normally do.) The result is we get a random number between 0 (the first element of an element array) and `complements.length - 1` (the last element of the array). We then use that to return a random element from `complements`!
* `res.sendFile(path.join(__dirname, "index.html"));` sends the user the `index.html` we just created. `path` is a libray for getting correct file locations. In this case, we're getting the whole path to it so Express can find it. `__dirname` is a special Node variable that's the folder of where the server.js file is. We know that index.html is in the same folder, so we're saying serve the index.html file found in the same directory as server.js.
* `res.sendFile(path.join(__dirname, "index.html"));` sends the user the `index.html` we just created. `path` is a library for getting correct file locations. In this case, we're getting the whole path to it so Express can find it. `__dirname` is a special Node variable that's the folder of where the server.js file is. We know that index.html is in the same folder, so we're saying serve the index.html file found in the same directory as server.js.
* `res.json({ complement: getRandomComplement() }).end();` - This is going to respond to the request with a `JSON` object, just like dog.ceo was doing. It's going to be a small object with just one key: `complement`. The value to that key is going to be one of the random complements generated by that function.
* `app.use("/public", express.static("./public"));` serves everything in the public directory publicly. We'll put stuff in here like images, client JS files, CSS files, and anything else we need users to be able to download from our server. Part of what this does is set all the correct headers which you need to do. The browser will request a resource like `my-styles.css` but the browser needs to know how to read that file: is it an image or is it a CSS file? (it doesn't look at the file extension). It then serves everything inside it from the path on the server `/public`. We happened to call the folder and the path from the server the same thing but you don't have to (though it's smart to.)

Expand Down