Layers – Layered Architecture for node.js made easy

June 28, 2011 – 6:37 pm

In my previous post A Layered Node.js Architecture using Express I wrote on the benefits of using a layered architecture and how to implement one in a node.js web app. This post goes one step further by introducing Layers, a module which will help automatically load and neatly setup the layers (and routes) of your web app.

Layers currently only supports Express, but adding support for other frameworks is simple. Feel free to submit a pull request!

The following is a description of how Layers works. For the impatient or code-hungry there is the working Layered Express example from which the following snippets are taken.

Layers performs two basic operations:

  1. Loads all layer javascript files.
  2. Loosely couples the layers and adds routes.

Installing

$ npm install layers

Example app

Simply require it and instantiate with your app, the path to your layers, a function which returns your wiring (more on this in the next section) and, optionally, an options object. i.e.

var Layers = require('layers').Express,
    wiring = require('./layers/wiring');
    new Layers(app, __dirname + '/layers', wiring);

File Layout

In this example the layout of the directories is:

__dirname
   - layers
       - controllers
       - services
       - views

Loading Layers

Each directory nested immediately within the layers directory defines a layer.

Each of these layer directories are recursively scanned for javascript files which are imported using require. The result of the require is inspected for two possibilities:

  1. An object
  2. A function

A Layered Node.js Architecture using Express

March 22, 2011 – 9:49 pm

Using layers in your app is a good way to ensure separation of concerns. This post is not an in-depth description to this architecture pattern but more a quick description of each layer’s purpose and how I’ve implemented them in node using express. For the impatient, you can peruse the code of the working example here.

Software used

View Layer

  1. Accept some data
  2. Apply any required formatting
  3. Render

View instances wrap a template and supplies additional functions to help take the burdon off the templating system, whichever one you use. For example, text transformations, regular expressions, etc are best done outside of the template in a simple testable function. In the following AuthorView example, whilst being contrived, the format function sets the Authors name to upper case.

Controllers

  1. Map a URI
  2. Extract some parameters
  3. Kick off some work (it’s not concerned what nor how it gets done)
  4. Send the result to a View for rendering

This example illustrates the use of Controller.setupGetRoute, a convenience function which takes a route’s path, an action and a hash of Views mapped to the Accept header they respond to. The action property is a function in the current

New Macbook Pro SSD before and after benchmarks

November 2, 2010 – 7:14 pm

Before (5400rpm)

After (OCZ Vertex II 120GB)

To say the least I’m more than happy. The over all score went from 165.24 to 260.85.

Next stop. 8GB of RAM.

To Android Hell and back

September 1, 2010 – 10:22 am

I rooted my HTC Desire over three months ago using the great guide on android.modaco.com. Happily rooted for a month or so, I then took advantage of Paul’s r5 release of a Froyo Rom (now archived) which I installed with ROM Manager. It worked perfectly well and I was blown away by the massive performance boost Froyo delivers.

So, being the inquisitive type, why wouldn’t I try r8 of the same ROM? I used ROM Manager to make a backup and install the ROM, but upon rebooting, something went wrong. I got a new Android splash screen, but it just kept on splashing, with no actual Android goodness at the end!

eep! Lucky I made a backup!

So my phone was borked, but I didn’t panic (yet) and got on to fixing it.

In the end, I’m still not sure what happened, but here’s my solution:

  1. I eventually got the system to boot by choosing “Wipe cache partition” from Recovery mode only to find that the SD Card and USB had been disabled! This is where I panicked as that means no adb, no shell, no nothing!
  2. My first step was to do as I usually do when something goes wrong and

5 hints on holding an old school LAN

July 26, 2010 – 8:51 pm

Old school LANs are a great way to keep in touch with friends, as like – in the same room – not on Facebook, whilst reliving some of the greatest games ever made such as Red Alert 2, Duke Nukem 3D and Carmageddon.

Here’s 5 hints to help you get your LAN up and running and making the most of it.

  1. Anonymous file server and DHCP
    99.9% of time spent at a LAN, especially one where you play old games not designed to run on modern operating systems, is stuffing around trying to get them to work. It is absolutely imperative that you have a dedicated anonymous file server upon which people can share the files required to run the games. This will eliminate unnecessary ”sharing” setup time. It will also prevent file server downtime as it will not need to be rebooted unlike client computers busy installing games.
    Another thing to make it easier is to have a local DNS server. I have a Mac Mini server which comes bundled with Mac OSX Server. Even a novice home networker can setup a local domain. Coupling this with a DHCP server setup