node.js and djangode

node.js and djangode

Theresa O’Connor
Django San Diego

node.js is an event-driven networking engine for JavaScript.

Like Twisted, but for V8, not Python.

Watch the video.

Grab it from github

sudo make install

Give it a spin

var http = require('http');
http.createServer(function (req, res) {
  res.sendHeader(200, {
    'Content-Type': 'text/plain'
  res.sendBody('Hello, world!');
// This bit sure looks familiar…
http.createServer(function (req, res) {
  res.sendHeader(200, {                
    'Content-Type': 'text/plain'       
  res.sendBody('Hello, world!');       
# Just like WSGI
def simple_app(environ, start_response):
    start_response('200 OK',
    return ['Hello, world!\n']

WSGI works because it’s so easy to compose middleware & apps.

// JavaScript lends itself to
// functional composition

function foo(bar) {
    return function() {
        // … do something, then …
        bar.apply(this, arguments);

…and JavaScript programmers are already used to event handling via callbacks.

So this sort of event-driven, server-side JS code is not just fast à la Twisted, but natural.

Sure, but this stuff is all lower-level than frameworks like Django, right?

Simon Willison’s djangode

“Utility functions for node.js that imitate some useful concepts from Django.”

var dj = require('djangode');
  ['^/$', function(req, res) {
    dj.respond(res, '<h1>Homepage</h1>');
  ['^/page/(\\d+)$', function(req, res, page) {
    dj.respond(res, '<h1>Page ' + page + '</h1>');
]), 8000);

Looking for a storage backend? CouchDB via node-couch!

PostgreSQL is in the works too: ry’s node_postgres and creationix’s postgres-js

What about templating?

There are many different JS templating engines.

I’ ve been using John Resig’s simple JS templates.

Made available as dj.tmpl in my djangode fork

dj.tmpl("hello, <%=name%>", {
    name: "Nikolaj"
// produces
"hello, Nikolaj"


But remember, node.js isn’t just for HTTP & HTML.

Like Twisted, it’s for all sorts of networking tasks.

Expect to see many protocols supported in the next year, like Twisted’s various projects

JavaScript is not just for browsers any more.

Get involved!
fork it · talk about it

thank you · cc by-sa 3.0