Adam Laycock


Featured Posts

Moving to Gatsby

4th December 2018

Ive moved my site over to Gatsby!

Everything an Option in the Theme

18th August 2016

My aproach to WordPress theme development.

RADIUS With Windows Server 2012R2

22nd July 2016

RADIUS authentication for wireless networks using NAP in Windows Server 2012R2.


10th November 2015

  • Motorway

Motorway provides flow control for your node applications through Junctions and Actions.

Motorway was created to streamline my application launchers. I found that I normally ended up with a main.js that looked like this:

Database = require('db/database')


app = Express()

RouterA = require('routes/routerA')

app.use('/', RouterA)


//and so on

Which got messy and ambiguos. At no point does the code imply that Database.init() has to happen before requiring routers it just does in this case. I would also then be duplicating most of this in my tests without app.listen(3000) so I could test app instead of running it.

This left a bit too much code outside my test coverage for my liking so I needed to make it re-usable which quickly lead to:

myApp = require('./lib/myApp')


Nicer but still nothing in-code that implies the right order. Cue Motorway which solves all this:

Motorway = require('motorway')

Database = require('./database')
Express = require('express')

app = null
mway = new Motorway

mway.addJunction('configure', ['init'])
mway.addJunction('launch', ['configure'])

mway.addAction('init', function(){
    _runner = this

mway.addAction('init', function(){
    app = Express()

mway.addAction('configure', function(){
    RouterA = require('./routers/routera')

    app.use('/', RouterA)

mway.addAction('launch', function(){
    _runner = this
    app.listen(3000, function(){

module.exports = {"Motorway": mway, "Application": app}

You can even store your junctions in their own files, so that file can become:

module.exports = {
    name: 'configure',
    runAfter: ['init'],
    actions: [

Making your code even neater.

When it comes to running Motorway in an applications tests it works very much the same, except it drops the launch junction.

In my tests (Mocha/Chai.expect) I have a test that makes sure my application inits okay which looks like this:

launch = mway.junctions.findOne({name: 'launch'})

mway.addJunction 'test', launch.runAfter

mway.addAction 'test', ->

mway.dropJunction 'launch'
mway.start 'init'

Which creates a new junction to finish the async test with the same run conditions as the original launch junction and it then drops the launch junction. Calling mway.start('init') now runs exactly the same code in the same order as normal but this time ends in a passed test not a listening server.

Motorway now forms a large part of my latest project and I’m wedging it into older ones that could really do with the help.

The source is over on github along with the issue tracker etc…

Hopefully Motorway will help you as much as it helped me.

Other Posts

Caching Apple downloads on a Windows server

15th July 2015

Following my recent post I look at how to implement Apple caching on a Windows server.

LESS in a WordPress theme

12th January 2016

Using LESS to build the CSS based on theme options is a great way to improve yout WordPress Themes.

Adam Laycock
Recent Articles

All content is my own unless otherwise stated.

My content is licensed under the CC-BY-NC-SA 4.0 license

cc by-nc-sa