running locomotive.js with node-dev

Right now I’m spending a lot of hours getting to know node.js and it’s an amazing new world. However, I tripped over a little stepping stone on the way to enlightenment. Currently I’m experimenting with the locomotive.js MVC framework and I wanted to run it with node-dev, a little command line application, that automatically restarts the node server whenever a file in the application has been changed.

As it turns out I can’t just tell node-dev to run “lcm server”, but that I have to create a file looking like this:

var locomotive = require('locomotive'),
 		env = process.env.NODE_ENV || 'development',
		port = process.env.PORT || 3000,
		address = '';

locomotive.boot(__dirname, env, function(err, server) {
	if (err) { throw err; }
	server.listen(port, address, function() {
	  var addr = this.address();
	  console.log('listening on %s:%d', addr.address, addr.port);

and save it as for example server.js. Then it’s possible to run node-dev server.js and comfortably write a locomotive app, without having to restart it all the time.

Edit 2013-03-05:

The method described above surely also works with other node modules like nodemon, forever, supervisor or similar things.


2 thoughts on “running locomotive.js with node-dev”

Leave a Reply

Your email address will not be published. Required fields are marked *