Something about node basics
-
Upload
philipp-fehre -
Category
Technology
-
view
281 -
download
4
description
Transcript of Something about node basics
![Page 1: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/1.jpg)
Something about Node
The basics
![Page 2: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/2.jpg)
What is node.js?
![Page 3: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/3.jpg)
Google’s JavaScript VM V8
![Page 4: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/4.jpg)
POSIX
![Page 5: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/5.jpg)
libuv
![Page 6: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/6.jpg)
Server side JavaScript
![Page 7: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/7.jpg)
Why node.js?
![Page 8: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/8.jpg)
(native) Async I/O
![Page 9: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/9.jpg)
V8 = Speed!
![Page 10: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/10.jpg)
Easy to get started with…
![Page 11: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/12.jpg)
It’s all about the ecosystem!
![Page 13: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/13.jpg)
NPM
![Page 14: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/14.jpg)
– 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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/15.jpg)
mkdir -p my_thing cd my_thing npm init
![Page 16: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/16.jpg)
package.json
![Page 17: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/17.jpg)
{ "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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/18.jpg)
index.js
![Page 19: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/21.jpg)
Install and use modules
![Page 22: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/22.jpg)
npm install --save minimist
![Page 23: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/24.jpg)
Build cat in node!
![Page 25: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/25.jpg)
Why node for client side engineering?
![Page 26: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/26.jpg)
Browserify
![Page 27: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/27.jpg)
You can use almost* everything here for webapps
*The browser is not an OS sorry :(
![Page 28: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/28.jpg)
Pitfalls & “Best practices”
![Page 29: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/29.jpg)
Name your functions
![Page 30: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/32.jpg)
JavaScript is more OO than you think!
![Page 33: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/33.jpg)
Exceptions are not control flow!
![Page 34: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/35.jpg)
Don’t call the callback more than once!
![Page 36: Something about node basics](https://reader033.fdocuments.in/reader033/viewer/2022052522/554f6033b4c905bb178b4692/html5/thumbnails/36.jpg)
function going(err, cb) { if(err) cb(err) cb() } !going("wild", function() { console.log("going wild") }) !!