Prepare your NodeJS Project Using Forever, Nodeunit and ExpressJS

Prior to creating Jenkins jobs and Capistrano recipes you should prepare NodeJS dependencies and scripts. This article does not provide in-depth details about NodeJS.

The assumptions are that nodeunit is used for unit testing, forever for process management, and ExpressJS for creating a web server.

Step 1: Create a package.json file:

NodeJS dependencies and utility scripts can be configured through the package.json file. Below is an example of such a file:

{

 "config": {

    "unsafe-perm": true

  },

  "scripts": {

    "test": "node_modules/.bin/nodeunit  tests.js",

    "start": "./node_modules/forever/bin/foreverd start app.js",

    "stop": "./node_modules/forever/bin/foreverd stop app.js"

  },

  "dependencies": {

    "express": "*",

    "forever": "*",

    "nodeunit": "^0.9.0"

  }

}

More details can be found here: https://www.npmjs.org/doc/json.html and here: http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/

Step 2: Add some tests.js content (based on https://github.com/caolan/nodeunit):

exports.testSomething = function(test){

    test.expect(1);

    test.ok(true, "this assertion should pass");

    test.done();

};

Step 3: Add a dummy app.js file (based on: http://expressjs.com/guide.html#intro):

var express = require('express');

var app = express();


app.get('/', function(req, res){

  res.send('Hello World');

});
var server = app.listen(3000, function() {

    console.log('Listening on port %d', server.address().port);

});