Skip to content

Instantly share code, notes, and snippets.

@ColinTheRobot
Created April 29, 2016 19:50
Show Gist options
  • Save ColinTheRobot/738c84a5ca411186a405cf396f6a33cf to your computer and use it in GitHub Desktop.
Save ColinTheRobot/738c84a5ca411186a405cf396f6a33cf to your computer and use it in GitHub Desktop.
# House Keeping
Open your .gitignore in your student repo and add node_modules save and close the file
How many of you actually use or go to the wiki
### Objectives
- Explain what Node.js is & why it exists
- Compare and contrast Node/Express vs. Ruby/Sinatra/Rails
- Recall/Demonstrate the process of setting up a node and express application
- Create routes in express
- Use module.exports and require to organize code
# Framing
## What is Node?
Keep in mind, Node.js is strictly a tool to run JavaScript on a server – while it's possible to build web applications and APIs in straight JS, we'll actually be using a framework on top of Node called Express. We touched on that a little at the start of this week to introduce what a server and routes are
## Why do people love Node!?
It's new and hot in the industry. And as you will all discover programmers love new to the point of insanity. ...
Developers and companies are excited because it allows fast, scalable APIs and sites in JavaScript. Okay... so what?
There are really two big points here: We're _familiar_ with JS and being able to use it on the backend gives us the option to use a single programming language throughout an entire full-stack application. So when we get to Rails, you'll have to use two ruby on the backend and js, html and css on the front. and when you actually work on a full code base, Rails backends are frequently optimized with Lua which is a db scripting language, or Rust. so now you have two or three back end languages. plus a front end language, and maybe you run a framework too, so now it's not just only JS but a flavor of JS.
###### Async
We won't dive too far into this slash will talk about this much more in detail later. BUT, the other big difference is that Node.js is designed to be _event-driven_ and _asynchronous_. THe opposite of this are servers that can only do one thing at a time, Node purposefully sends nearly everything to the background and keeps going.
So in really simple layman's terms:
Imagine a paper delivery boy riding on his bike delivering papers every morning. Imagine he stops at each house, throws the paper on your doorstep, and waits to make sure you come out & pick it up before moving on to the next house. That would be what we'd call _blocking_ – each line of code finishes before moving on to the next line of code.
Now imagine the paperboy throwing the newspaper on your porch but never stopping his bicycle; never stopping, he just keeps throwing papers on porches, so that by the time you pick it up he'll be 3 or 4 houses down. That would be _non-blocking input/output (I/O)_, or _asynchronous_. This is an extremely awesome ability of node since I/O tends to be very "expensive."
Anywayyyyy...
### That's Node, SO...
Walk me through what your homework was yesterday and how it relates to what we understand as websites/webapps?
Cool, so what you all did yesterday was build an http server and interacted with it via routes, which touched a kind of quasi/pseudo db (your json file) in a very informal bare bones kind of way.
i.e We said, I'm going to listen to every request and depending on what it is (with an if statement) I'm going to output ufos or I'm not.
What do we mean when we say abstraction?
Obviously people have been building websites for a while, and as the years go on and technology improves developers have designed systems, tools, and patterns to abstract and organize the process.
So let's formalize/abstract what we did yesterday and the day before in the context of Node/Express.
# Start
http://expressjs.com/
How do we start? As we dive into more serious technologies the amount of set up becomes more and more so we're going to move through this briskly, but confidently.
npm init (server.js)
Our code is going to live in this file (for the most part)
we're going to change index.js to server.js
touch server.js
look at our package.json, what's in it?
How do we install packages?
we have a node app but how do we install express and other packages to augment?
npm install express --save
We need to set up a server now and express
```
var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
var server = app.listen(port)
```
How did we interact with with the url path yesterday? what are the pieces
aw: we just evaluate whatever was coming in from the server and evaluated based on whether it matches our conditional. Express let's us predetermine behavior and response based on what we call routes.
/user
/hello
So let's define a bare function
app.get
and how did we pass parameters?
```
?q=blah&foo=bar
```
we do have a second way they aren't a one or the other, we often use both
dynamic segments and query parameters
/hello/:name?human=true
app.METHOD(PATH, HANDLER)
```
app.get('/hello/:name', function(req, res) {
res.send({params: req.params, queries: req.query})
});
```
## You Do
Make five routes for +, -, *, /
and a fifth route /math/:operator
that can do all five of the above
that will add subtract divide or multiply depending on the operator segment.
(Router if there's randomly tons of time, see other lesson plan)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment