Introduction to Node.js


Published on

A quick talk I did at London Titanium meet up introducting Node.js

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Introduction to Node.js

  1. 1. Introduction to Node.js
  2. 2. WhoDeveloper for Kainos (Java, Scala, Rails, JavaScript, HTML,CSS)Student at University of BathWrite a lot about JavaScript on Twitter
  3. 3. Origins of NodeServer side JavaScript done rightruns on Chromes V8 Engine (its quick)Evented I/O - runs single non-blocking thread with eventloopthis is great as JS was designed to be run in a single threadenvironment (browser)
  4. 4. Node right nowV0.8 standardised the API (non breaking)currently V0.8.4, much more stable than < 0.8Install via installers, from source or via package manager likeBrew.
  5. 5. NPMPackage Manager for Node (think rubygems / bundler forNode)Full of really useful modules (and some less useful ones)12,627 packages as of yesterday (browse at
  6. 6. Event Driven JavaScriptLearn to think Asynchronously "Once node has completed atask, the callback for it is fired. But there can only be onecallback firing at the same time. Until that callback hasfinished executing, all other callbacks have to wait in line. Inaddition to that, there is no guarantee on the order in whichthe callbacks will fire." From: Async.js
  7. 7. Callbacks Lots and lots of callbacksvar net = require(net);var server = net.createServer(function (socket) { socket.write(Echo serverrn); socket.pipe(socket);});server.listen(1337,; How to avoid:
  8. 8. Naming your Callbacks Did you know you can name them?var net = require(net);var server = net.createServer(function writeResponse(socket) { socket.write(Echo serverrn); socket.pipe(socket);});server.listen(1337,;
  9. 9. Avoiding Callbacks: Modules"Write small modules that each do one thing, and assemblethem into other modules that do a bigger thing. You cant getinto callback hell if you dont go there." Isaac Schlueter -Node.js core contributor @izswrite your code in a file like normal://file socket-module.jsfunction writeResponse(socket) {socket.write(Echo serverrn);socket.pipe(socket);});
  10. 10. module.exports = {resp: writeResponse}(this follows the CommonJS module structure)require and use itvar net = require(net);var socketModule = require(socket-module);var server = net.createServer(socketModule.resp);server.listen(1337,;
  11. 11. Databases with NodeRedis & the Redis NPM Package is awesomeAdapters for all common DB solutions
  12. 12. Web FrameworksMost popular is Express JS - www.expressjs.comLoads out there - Google "node js framework"Geddy, Flatiron, RailwayJSTools like this are slowly but surely maturing
  13. 13. Expressvar app = express.createServer();app.get(/, function(req, res){ res.send(Hello World);});app.listen(3000); routing, views (Jade), etc very extensible
  14. 14. About to hit V3
  15. 15. Unit TestingNodeUnit more. Find one that suits (I love Mocha)
  16. 16. In the wildJSBin by @rem - - pure awesomenessTweetDig by @mheap - - 2.5m tweets perday
  17. 17. To Sum UpNode is still very young, although standards and conventionsare beginning to be defined.Lack of resources is slowly becoming less of an issue.V0.8 is huge improvement on prior versions.Node is seriously quick if used properly.
  18. 18. Further Resources@Peepcode screencasts www.peepcode.comCodeSchool Node course www.codeschool.comAsync JavaScript book from Trevor to Node blog
  19. 19. Any Questions?Slides on Github: for JavaScript tutorials(including Node)@Jack_Franklin if you can put up with even more of merambling