Something about node basics

553 views

Published on

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

Published in: Technology, Sports
  • Be the first to comment

  • Be the first to like this

Something about node basics

  1. 1. Something about Node The basics
  2. 2. What is node.js?
  3. 3. Google’s JavaScript VM V8
  4. 4. POSIX
  5. 5. libuv
  6. 6. Server side JavaScript
  7. 7. Why node.js?
  8. 8. (native) Async I/O
  9. 9. V8 = Speed!
  10. 10. Easy to get started with…
  11. 11. 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")
  12. 12. It’s all about the ecosystem!
  13. 13. NPM
  14. 14. “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.” – James Halliday (http://substack.net/how_I_write_modules)
  15. 15. mkdir -p my_thing cd my_thing npm init
  16. 16. package.json
  17. 17. { "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" }
  18. 18. index.js
  19. 19. 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
  20. 20. 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) !
  21. 21. Install and use modules
  22. 22. npm install --save minimist
  23. 23. 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) }) !
  24. 24. Build cat in node!
  25. 25. Why node for client side engineering?
  26. 26. Browserify
  27. 27. You can use almost* everything here for webapps *The browser is not an OS sorry :(
  28. 28. Pitfalls & “Best practices”
  29. 29. Name your functions
  30. 30. myThing(argv.i, function(err, res) { if(err) return console.error(err) console.log(res) }) ! … because! myThing("snowboardng into a beertent", handler) ! !
  31. 31. 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() }) }) })
  32. 32. JavaScript is more OO than you think!
  33. 33. Exceptions are not control flow!
  34. 34. function wait(cb) { setTimeout(cb, 100); } try { wait(function() { throw new Error("Out of time") }) } catch(e) { console.log(e) }
  35. 35. Don’t call the callback more than once!
  36. 36. function going(err, cb) { if(err) cb(err) cb() } ! going("wild", function() { console.log("going wild") }) ! !

×