36
Something about Node The basics

Something about node basics

Embed Size (px)

DESCRIPTION

http://github.com/sideshowcoder/javascript-workshop

Citation preview

Page 1: Something about node basics

Something about Node

The basics

Page 2: Something about node basics

What is node.js?

Page 3: Something about node basics

Google’s JavaScript VM V8

Page 4: Something about node basics

POSIX

Page 5: Something about node basics

libuv

Page 6: Something about node basics

Server side JavaScript

Page 7: Something about node basics

Why node.js?

Page 8: Something about node basics

(native) Async I/O

Page 9: Something about node basics

V8 = Speed!

Page 10: Something about node basics

Easy to get started with…

Page 11: Something about node basics

var http = require("http") !var html = "<html>" + "<title>Hello Node</title>" + "<body>Hello Node</body>" + "</html>" !var handler = function(req, res) { res.writeHead(200, { "Content-Type": "text/html" }) res.end(html) } !http.createServer(handler).listen(3000, "127.0.0.1")

Page 12: Something about node basics

It’s all about the ecosystem!

Page 13: Something about node basics

NPM

Page 14: Something about node basics

– James Halliday (http://substack.net/how_I_write_modules)

“If some component is reusable enough to be a module then the maintenance gains are really worth the overhead of making a new project

with separate tests and docs.”

Page 15: Something about node basics

mkdir -p my_thing cd my_thing npm init

Page 16: Something about node basics

package.json

Page 17: Something about node basics

{ "name": "my_thing", "version": "0.0.0", "description": "detect what is my thing", "main": "index.js", "scripts": { "test": "node ./test.js" }, "keywords": [ "my", "thing" ], "author": "sideshowcoder", "license": "BSD-2-Clause" }

Page 18: Something about node basics

index.js

Page 19: Something about node basics

var myThing = function(thing, cb) { if(thing.match(/snowboard*/)) { cb(null, thing + ", is my thing!") } else { cb(thing + ", is not really my thing!") } } !module.exports = myThing

Page 20: Something about node basics

var myThing = require("./") !var handler = function(err, res) { if(err) { console.log(err) return } console.log(res) } !myThing("snowboardng into a beertent", handler) myThing("walking into a beertent", handler) !

Page 21: Something about node basics

Install and use modules

Page 22: Something about node basics

npm install --save minimist

Page 23: Something about node basics

var myThing = require("../my_thing") var argv = require("minimist")(process.argv.slice(2)) !myThing(argv.i, function(err, res) { if(err) return console.error(err) console.log(res) }) !

Page 24: Something about node basics

Build cat in node!

Page 25: Something about node basics

Why node for client side engineering?

Page 26: Something about node basics

Browserify

Page 27: Something about node basics

You can use almost* everything here for webapps

*The browser is not an OS sorry :(

Page 28: Something about node basics

Pitfalls & “Best practices”

Page 29: Something about node basics

Name your functions

Page 30: Something about node basics

myThing(argv.i, function(err, res) { if(err) return console.error(err) console.log(res) }) !

myThing("snowboardng into a beertent", handler) !!

… because!

Page 31: Something about node basics

Beware of the V of evil

doThisFirst(function(err) { if(err) throw err nowDoThis(function() { if(err) throw err andFinallyDoThis() { if(err) throw err done() }) }) })

Page 32: Something about node basics

JavaScript is more OO than you think!

Page 33: Something about node basics

Exceptions are not control flow!

Page 34: Something about node basics

function wait(cb) { setTimeout(cb, 100); } try { wait(function() { throw new Error("Out of time") }) } catch(e) { console.log(e) }

Page 35: Something about node basics

Don’t call the callback more than once!

Page 36: Something about node basics

function going(err, cb) { if(err) cb(err) cb() } !going("wild", function() { console.log("going wild") }) !!