#refreshdublin

Introduction to Node.js



http://hop.ie/go/node

Source files:
http://hop.ie/downloads/node-examples.zip

Plan

  • What is Node.js?
  • Frameworks
  • Create an app with Express.js
  • Example: Tweets
  • Pints

Node.js

  • JavaScript based software system
  • Non-blocking
  • Event driven
  • Launched in 2009

Blocking VS Non-blocking

Doctor's surgery VS Fast-food outlet

Data I/O

L1 3 cycles
L2 14 cycles
RAM 250 cycles
Disk 41,000,000
Network 240,000,000

Source: Ryan Dahl 2008 / Jeff Kunkle 2012

JavaScript

What sort of

apps?

Modules

Currently: ~30k modules available

A full Node app

var fs = require('fs');
var contents = fs.readFile(
  './hello.txt',
  'utf8', 
  function(err,contents){
    console.log(contents);
  }
);
console.log("Hello from Node!\n");

Download: http://hop.ie/downloads/node-examples.zip

Hello World

  • Create new file: app.js
var http = require('http');
var server = http.createServer(
function (request, response) {
  response.writeHead(
    200,
    {'Content-Type': 'text/plain'}
  );
  response.end('Hello World!\n');
});
server.listen(1337, '127.0.0.1');

Frameworks

  • Express.js
  • Meteor / Socketstream
  • Sails
  • Hood.ie

Creating an app

Using Express.js

Example

Tweets

hop.ie/tweets

Source code: github.com/donovanh/tweets

Hosting

  • Self hosting
  • Appfog
  • Heroku
  • NodeJitsu

Summary

The good

  • Fast and scaleable
  • Great community
  • All JavaScript
  • Streams, Socket.io etc

The bad

  • CPU-intensive apps
  • Learning curve
  • Not browser JavaScript

Next steps

  1. Install Node.js
  2. Make something
  3. ????
  4. Debug

Resources

Thanks

Get in touch


http://hop.ie/go/node