# 2011-GHP-RM-WEB-FT Morning Review Notes
## Exit Ticket Solutions
### In your own words, what is Node? And why do we need it?
Node is a runtime environment that has the ability to run a V8 engine on environments other than the browser (e.g. operating systems, servers, etc.)
### What does module.exports do?
It's an object that tells Node which pieces of code to export from a given file so other files are allowed to access the exported code.
### Which of the following is not a built-in Node module?
- fs
- http
- path
- **curl** ☑️
### What is the order of output of this snippet of code:
```javascript
console.log('Hellow')
setTimeout(() => { console.log('a new') }, 5000)
console.log('World')
```
1. Hellow
2. World
3. a new
### What is the output of the following snippet of code:
```javascript
for (var x = 0; x < 3; x++) {
setTimeout(() => console.log(x), 100);
}
```
3, 3, 3
#### TL;DR Reason for This:
Variables declared with `var` only get given space (memory) once. In addition, it is beholden to its execution context (either global or function) so, in this case, global. `setTimeout` with the `console.log` creates a closure around the variable x, which has already run until the end, which x is 3.
#### Images from this morning that go into more detail


1. Hoisting Code Demo
`var` example:
```javascript=
console.log(x) // returns undefined
var x = 5
```
`let` demo:
```javascript=
console.log(x) // error
let x = 5
```
2. Closure Code Demo
```javascript
function outer() {
let counter = 0
function increment() {
return counter += 1
}
return increment
}
let newInstance = outer()
console.log(newInstance()) // 1
console.log(newInstance()) // 2
```
[Resource for `var` vs `let`](https://exploringjs.com/es6/ch_variables.html#sec_let-const-loop-heads)
## Unaddressed Questions From Exit Ticket
### Should node_modules always be in .gitignore, or do we have to push them to a repo initially?
### Answer
Yes! This is standard practice because `node_modules` have ALL the dependencies of our dependencies. Open it up and check how many directories it has when you just install one module/package like `Express`
That's why we can just rely on the `package.json` because once we run `npm install`, it will look through our dependencies in the `package.json` and install everything locally into `node_modules`. So, we don't need to push up `node_modules` to GitHub
### Can you go over the cat portion of our workshop?
### Answer
The idea of cat was to be able to read a file as an argument to cat (i.e., `cat bash.js`) and log out its contents to our terminal screen. We will use Node's `fs` built in module, more specifically, the method off it called `readFile`, which takes in a file to be read, encoding (use utf8) and a callback function
```javascript=
module.exports = (fileName, done) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) {
done(err.stack)
} else {
done(data)
}
})
}
```
Side Note: If you don't indicate the encoding, it will just log out numbers. Every character has its own number assigned to it.
## Examples I Couldn't Get To
[Event Loop Visualizer](http://latentflip.com/loupe/)
Draw the Call Stack
```javascript=
const hello = () => {
awesome()
console.log('hello')
}
const awesome = () => {
console.log('coolio')
food()
}
const food = () => {
console.log('pbj')
console.log('waffles')
}
hello()
```


Now, let's add some async code to it: Please note that I didn't draw the event loop and queue in every step for brevity
```javascript=
const hello = () => {
awesome()
console.log('hello')
}
const awesome = () => {
console.log('coolio')
food()
setTimeout(() => {
console.log('I want a cookie!')
}, 3000)
console.log('awesome')
}
const food = () => {
console.log('pbj')
console.log('waffles')
}
hello()
```

