slides: https://docs.google.com/presentation/d/1bcQZYTbS0CoRkeOdnH9frxBwg5UOZY3TcizXRzYxbQU/edit?usp=sharing
open index.html in your browser. In the console you'll see hi.
on the command line, run node index.js. You'll see hi.
Same code, different environments.
open index.html. Click the link, you'll see hi in the console.
The function called on the click is triggered by the browser's event loop. Node's event loop works the same way - an action causes the event loop to trigger any registered callbacks.
node index.js.
Open http://localhost:1337 in your browser.
This is how the node.js event loop works:
- Your browser made a connection to the server
- the OS notified the event loop that a connection was made
- the function registered as the callback was called
- you sent hello world to the browser
- node is passive, waiting for the next event to trigger
the-not-so-good-way.js: shows how synchronous programming isn't the node way. ties up the event loop, you shouldn't do blocking actions when possible.
the-node-way.js: let I/O operations happen asynchronously.
npm is node's package manager.
the package.json file represents what dependencies your node app has.
to create package.json, run npm init, follow the prompts.
install dependencies by running npm install <name> --save. The --save modifier adds it to package.json. Use --save-dev if it's only for dev environments (like test dependencies).
install dependencies: npm install -g mocha, npm install chai
to run tests: mocha index-spec.js
"sinatra for node.js"
to use the express app generator, install express as a global module: npm install -g express
running express test will create a folder called test with an express app in it.
npm start to run it.
learning module.exports and require to load local dependencies.