Ole Erling appears in NodeJS

Saturday, Dec 4, 2010 3 minute read Tags: umbraco nodejs
Hey, thanks for the interest in this post, but just letting you know that it is over 3 years old, so the content in here may not be accurate.

People would probably agree that I’m not the most normal of people when it comes to developing software. Quite often something takes my fancy, and I have a crack at building with it, whether it is a good idea or not.

Recently there’s been a lot of fuss on Twitter about a Ruby project which has recently gone into v1.0 called Sinatra. It’s got a rather nice syntax if you’re trying to build a quick-fire application, here’s the Hello World example from the site:

require 'sinatra'
get '/hi' do
  "Hello World!"
end

In fact in about 15 minutes I threw together a new site for some quick linking at slace.biz, from which you can jump to /umbraco or get some basic contact info via /me.

Having fun with NodeJS

It’s no secret that I’m a fan of JavaScript, especially if I want to do something that’s a little… strange.

So after playing with Sinatra for a bit I decided “Why can’t I just built it in JavaScript?”. Oh sure, it’s been done before, but reinventing wheels are fun.

This isn’t really a serious attempt, it’s just a bit of fun and a bit of a learning experiment, so I decided that taking the piss would be the best way to go about it. To do this I decided to create my own framework, a framework inspired by a Danish ‘musician’ called Ole Erling.

The source code is available on my bitbucket, if you want to grab it it’s here.

Design of Ole

The design of Ole is to be a fun one (remember: piss-take!) and to work with it you have a fluent API which you can work with. The first thing that Ole must do is enter the room (well, the file):

var ole = requires('./src/ole').enters();

Now that Ole is in the room you can get him to do things, such as listen to HTTP events:

ole.hears('GET', '/', function(req, res) {
  res.end('Hello World!');
});

What I’ve said is that when Ole hears a GET HTTP request on the URL / it will execute a particular function. Ole can hear all four HTTP request modes, GET/ POST/ PUT and DELETE, it’s up to you how you want to implement them.

Once you’re said what Ole can hear you better get him to play it set:

ole.play();

Currently Ole will only play on port 2009 (spot the in-joke there :P) on localhost.

Conclusion

As I’ve said, this is a bit of a joke project that I’m working on, currently I’m hanging out to get a beta invite on heroku.com’s NodeJS support, or the no.be beta project, and when I do expect a site running Ole to go live :D.

Please feed free to get Ole running a set for you too ;).