46
Node.js If you have not installed Node, do it now! http://nodejs.org Ask a staff member if you need help! 6.470 Day 4

If you have not installed Node, do it now! Ask a staff

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: If you have not installed Node, do it now!   Ask a staff

Node.js

If you have not installed Node, do it now! http://nodejs.org

Ask a staff member if you need help!

6.470 Day 4

Page 2: If you have not installed Node, do it now!   Ask a staff

ParsnipQuick Node.js demo

Page 3: If you have not installed Node, do it now!   Ask a staff

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications

Page 4: If you have not installed Node, do it now!   Ask a staff

JavaScript

Page 5: If you have not installed Node, do it now!   Ask a staff

JavaScript Everywhere!

Page 6: If you have not installed Node, do it now!   Ask a staff

TODAY IS!

JAVASCRIPT

Page 7: If you have not installed Node, do it now!   Ask a staff

Node.js Example

Write your own server// app.js !var http = require(‘http'); !helloServer = http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World'); }) !helloServer.listen(8000);

Running the server$> node app.js

Page 8: If you have not installed Node, do it now!   Ask a staff

You!

Page 9: If you have not installed Node, do it now!   Ask a staff

Node.js Server

You!

Page 10: If you have not installed Node, do it now!   Ask a staff

Node.js Server

You!

request a page

Page 11: If you have not installed Node, do it now!   Ask a staff

Node.js Server

You!

request a page HERE IS SOME HTML

<html> <head> <title> … etc </html>

Page 12: If you have not installed Node, do it now!   Ask a staff

Be Careful!

Client Side JavaScript is NOT Server Side JavaScript

Page 13: If you have not installed Node, do it now!   Ask a staff

Node.js Server

You!

request a page

HERE IS SOME HTML

<html> <head> <title> … </html>

Page 14: If you have not installed Node, do it now!   Ask a staff

Node.js Server

You!

request a page

HERE IS SOME HTML

<html> <head> <title> … <script> var i = 1; i ++; </script> </html>

Page 15: If you have not installed Node, do it now!   Ask a staff

Why use Node? Part 1.Write Less Code Node Package Manager (NPM)

Page 16: If you have not installed Node, do it now!   Ask a staff

Node Package ManagerBunch of fancy libraries for you to use !Try it! Make sure it’s installed! Windows: Open Node Command Prompt Mac: Open Terminal Type npm. You should see a bunch of commands !Now try npm install -g express Windows: You may need to run as administrator (reopen Node Command Prompt except right click run as admin) Mac, you may need to do sudo npm install -g express

Page 17: If you have not installed Node, do it now!   Ask a staff

Package.jsonA set of ‘instructions’ that assist your app !{ "name": "helloworldapp", "description": "this is my new app", "version": "0.0.1", "private": true, "dependencies": { "express": ">= 3.4.0", "hbs": “*" } }

Page 18: If you have not installed Node, do it now!   Ask a staff

Package.jsonnpm install will look in package.json to make sure you have what you need. !

Whenever you have a potentially new/different package.json, always npm install

Page 19: If you have not installed Node, do it now!   Ask a staff

Package.jsonnpm install will look in package.json to make sure you have what you need.

Page 20: If you have not installed Node, do it now!   Ask a staff

Why use Node? Part 2.JavaScript & all its quirks

Page 21: If you have not installed Node, do it now!   Ask a staff

JavaScript is Asynchronous

//PHP !echo “hello”; sleep(2); echo “world”; !

//JavaScript !setTimeout(function() { console.log(“world”) }, 2000); !console.log(“hello”);

Page 22: If you have not installed Node, do it now!   Ask a staff

Why use Node? Part 3.Sitting on a comfortable cloud. A Google cloud

Page 23: If you have not installed Node, do it now!   Ask a staff

A comfy layer on Google’s JavaScript Engine - Reliable and Supported

Page 24: If you have not installed Node, do it now!   Ask a staff

Express.js

Page 25: If you have not installed Node, do it now!   Ask a staff

A Web Framework Built On Node.js

Express.js

Page 26: If you have not installed Node, do it now!   Ask a staff

Let’s Try it Out

Express.js

Type express helloexpress

Page 27: If you have not installed Node, do it now!   Ask a staff

Express.js

Node.js Server

You!

request a page Here is your HTML

Page 28: If you have not installed Node, do it now!   Ask a staff

Express.js

Node.js Server

You!

request a page Here is your HTML

Express.js

Page 29: If you have not installed Node, do it now!   Ask a staff

Express.js

Node.js Server

You!

request a page Here is your HTML

Express.jsRoutesapp.js Views etc…

Page 30: If you have not installed Node, do it now!   Ask a staff

Express.js

Node.js Server

You!

request a page Here is your HTML

Express.jsRoutesapp.js Views etc…

I got this, guys

Page 31: If you have not installed Node, do it now!   Ask a staff

Demo: A Simple Web Counter

Express.js

Page 32: If you have not installed Node, do it now!   Ask a staff

What happens when I restart the server?

Express.js

Page 33: If you have not installed Node, do it now!   Ask a staff

DatabaseMySQL

Page 34: If you have not installed Node, do it now!   Ask a staff

DatabaseMySQL MongoDB

Page 35: If you have not installed Node, do it now!   Ask a staff

DatabaseMySQL FirebaseMongoDB

Page 36: If you have not installed Node, do it now!   Ask a staff

DatabaseMySQL FirebaseMongoDB

npm install mysql

Page 37: If you have not installed Node, do it now!   Ask a staff

DatabaseMySQL FirebaseMongoDB

npm install mysql npm install mongodb

Page 38: If you have not installed Node, do it now!   Ask a staff

DatabaseMySQL FirebaseMongoDB

npm install mysql npm install mongodb npm install firebase

Page 39: If you have not installed Node, do it now!   Ask a staff

Firebase

Page 40: If you have not installed Node, do it now!   Ask a staff

Data is stored as standard JSON

Firebase

Page 41: If you have not installed Node, do it now!   Ask a staff

Data is stored as standard JSON

Firebase

{ movingbox1: ‘books’, movingbox2: ‘clothes’ }

Page 42: If you have not installed Node, do it now!   Ask a staff

Data is stored as standard JSON

Firebase

{ movingbox1: ‘books’, movingbox2: ‘clothes’ movingbox3: { shoebox1: ‘sandals’, shoebox2: ‘boots’ }, }

Page 43: If you have not installed Node, do it now!   Ask a staff

Demo: A better web counter

Firebase

Page 44: If you have not installed Node, do it now!   Ask a staff

Holy, that was a lot.

Page 45: If you have not installed Node, do it now!   Ask a staff

Node.js is Tricky

Page 46: If you have not installed Node, do it now!   Ask a staff

But not impossible