Processing.js 1.0 Released

by admin on 19/11/2010

The team behind Processing.js finally released their 1.0 milestone. This release now is nearly feature complete compared to the original Processing. Very few Exceptions remain.

Read the release notes on the Processingjs blog.

No Comments

HUMMINGBIRG – Realtime Analytics

by admin on 6/11/2010

Michael Nutt created an amazing tool for realtime user tracking on websites. It features a high performance tracking server based on nodejs and mongodb as well as a fancy dashboard using lots of HTML5 magic. Combined with an easy way to add your own custom metrics it could become the silver bullet for near realtime monitoring of campaigns and conversions.

And the best thing about it: It is developed as opensource so go ahead and help out via github.

No Comments

Tunnelblick – A nice OpenVPN GUI for OSX

by admin on 6/11/2010

From the Application Developer:

Tunnelblick is a ready-to-use Graphic User Interface (GUI) for OpenVPN on Mac OS X. It provides easy-to-use control of OpenVPN server and/or client connections.
It runs on OS X Tiger (10.4), Leopard (10.5), and Snow Leopard (10.6). It comes as a ready-to-use Universal application with all necessary binaries and drivers (including OpenVPN and tun/tap) included. No additional installation is necessary — just add your configuration and encryption information.
Tunnelblick is free software licensed under the GNU General Public License (GPL) Version 2.

I for one truely like Tunnelblick as it is a lot more hassle free than Apples VPN Gui/Implementation.

Get it from Google Code.

No Comments

Adding Rails-Style Controllers to the Express Framework for node.js

by admin on 3/11/2010

UPDATE: now available on github

When playing around with the expressjs web framework for node.js one of the first things you may ask yourself is ‘Where the … are my controllers’. Funny thing: express does not provide them.

I for one truly love to separate different parts of my code, so adding dedicated controller classes was the first thing i did. It is not at all a fancy Implementation that mimics big frameworks like Ruby on Rails or Django, but rather a small script that provides what I need at the moment. Below I will list the steps necessary to have some form of controller in express.

The first step is to create an express app of course. This is quite convenient using the build in ‘express’ executable:

express --sessions application-name

This command much like the ‘rails’ command created the barebone for our new express application and added cookie/session support.

The next step is to create the basic controller file. I creatively named it base_controller.js and put it in the application root folder. This base_controller.js should accomplish two tasks: First it needs to define custom routes for each controller class of the application and secondly it needs to provide a base class for all application controllers.

Without much further ado i came up with the following structure for base_controller.js:

BaseController = function(request, result) {
	this.request = request;
	this.result  = result;

	this.render = function(template, options) {
		return this.result.render(template, options);
	};

	this.send = function(content) {
		return this.result.send(content);
	};

	this.extend = function(child) {
	    for(var p in child) this[p] = child[p];
		return this;
	};

};

// really simple routing
exports.init_routes = function(app) {
	var fs = require('fs');
	// get all js files in controllers subfolder
	fs.readdir(__dirname+'/controllers', function(err, files) {
		files.forEach(function(file) {

			if( /.js$/.test(file) ) {
				mdl = require('./controllers/'+file);

				// add the standard route
				app.get('/' + file.replace(/\.js$/, '') + '/:action?/:id?', function(request, result) {
					var controller = new mdl.controller(request, result);

						// build action parameter
						if( !request.params.action ) {
							request.params.action = "indexAction";
						} else {
							request.params.action += 'Action';
						}
						// try to call the action
						if( typeof controller[request.params.action] == 'function' ) {
							controller[request.params.action]();
						} else {
							result.send(request.params.action + ' is not a controller action');
						}

					delete controller;
				});
			}

		});
	});
};

The init_routes function sets up the application routes and should be called once at application startup. It first loads a list of all files of the subfolder ‘controllers’. For each of these files it creates a route consiting of the files name, some optional action and an optional id value. Much like the default routes known from Ruby on Rails. If a request matches any of these routes, the script initialises the controller with the request and result objects and tries to call the given controller action, falling back to ‘index’ if none was passed. To separate actions and normal functions in controllers, the word ‘Action’ is appended to every action passed.

So for example the route ‘/test/dosomething’ would call the function ‘dosomethingAction’ of the controller file test.js.

To make writing controllers as clutter free as possible an extend function (inspired by the prototype.js implementation of Object.extend) is also available in the base_controller. Its usage can be seen in the final example.

After defining the base controller, the next step is to initialize it. This can be done by adding this line to the app.js file generated by express:

require('./base_controller.js').init_routes(app);

This completes the controller setup and we can now create a controllers subfolder in our express application and start adding code to it. Defining a controller is really simple using the extend function of base_controller.js. For example a the controller ‘controllers/test.js’ could contain the following content:

ExampleController = function(request, result) {
	BaseController.call(this, request, result);

	this.y = 0;

	this.indexAction = function() {
		this.render('index', {
			locals: {
				title: 'Express'
			}
		});
	};

	this.testAction = function() {
		this.y += 1;
		this.send(this.y+'');
	};
};

exports.controller = ExampleController;

So basically we simply extend the base controller with our actions. This may not be the most OOP way of doing things, but i truly like the concise and distraction free way of defining controllers.

If everything went well, we kann start our application with node app.js and view the results at http://localhost:3000/test (implicitly calling the indexAction) and http://localhost:3000/test/what.

There are many obvious extensions to the base_controller.js file. The first that comes to mind are helpers and shorthands for typical rendering / sending calls. Another thing should be typical filters like before_filter / after_filter in Ruby on Rails. As i progress further, another article covering these will be posted.

2 Comments