Skip to content

Instantly share code, notes, and snippets.

@jackfranklin
Created July 30, 2012 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackfranklin/3e059e4121708489fb0e to your computer and use it in GitHub Desktop.
Save jackfranklin/3e059e4121708489fb0e to your computer and use it in GitHub Desktop.
Node.js talk

Introduction to Node.js

Who

  • Developer for Kainos (Java, Scala, Rails, JavaScript, HTML, CSS)
  • Student at University of Bath
  • Write a lot about JavaScript at www.javascriptplayground.com
  • @Jack_Franklin on Twitter

Origins of Node

  • Server side JavaScript done right
  • runs on Chrome's V8 Engine (it's quick)
  • Evented I/O - runs single non-blocking thread with event loop
  • this is great as JS was designed to be run in a single thread environment (browser)

Node right now

  • V0.8 standardised the API (non breaking)
  • currently V0.8.4, much more stable than < 0.8
  • Install via installers, from source or via package manager like Brew.

NPM

  • Package Manager for Node (think rubygems / bundler for Node)
  • Full of really useful modules (and some less useful ones)
  • 12,627 packages as of yesterday (browse at http://search.npmjs.org/)

Event Driven JavaScript

## Callbacks

  • Lots and lots of callbacks
var net = require('net');

var server = net.createServer(function (socket) {
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});

server.listen(1337, '127.0.0.1');

Naming your Callbacks

  • Did you know you can name them?
var net = require('net');
var server = net.createServer(function writeResponse(socket) {
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});
server.listen(1337, '127.0.0.1');

Avoiding Callbacks: Modules

  • "Write small modules that each do one thing, and assemble them into other modules that do a bigger thing. You can't get into callback hell if you don't go there." Isaac Schlueter - Node.js core contributor @izs

  • write your code in a file like normal:

//file socket-module.js
function writeResponse(socket) {
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});
module.exports = {
  resp: writeResponse
}

(this follows the CommonJS module structure)

  • require and use it
var net = require('net');
var socketModule = require('socket-module');
var server = net.createServer(socketModule.resp);
server.listen(1337, '127.0.0.1');

Databases with Node

  • Redis & the Redis NPM Package is awesome
  • Adapters for all common DB solutions

Web Frameworks

  • Most popular is Express JS - www.expressjs.com
  • Loads out there - Google "node js framework"
  • Geddy, Flatiron, RailwayJS
  • Tools like this are slowly but surely maturing

Express

var app = express.createServer();

app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(3000);
  • routing, views (Jade), etc
  • very extensible
  • About to hit V3

Unit Testing

In the wild

To Sum Up

  • Node is still very young, although standards and conventions are 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.

Further Resources

Any Questions?

  • Slides on Github: gist.github.com/jackfranklin
  • www.javascriptplayground.com for JavaScript tutorials (including Node)
  • @Jack_Franklin if you can put up with even more of me rambling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment