Node.js and Paperboy for local JavaScript development

Node.js + Paperboy make for a slick replacement to Apache as an http server for local JavaScript development. This has come in handy a lot lately when working on JavaScript experiments or prototypes. Often times it is easiest to use a CDN to include whatever base libraries are needed (jQuery, Dojo, etc). Annoyingly, browsers won’t download CDN content when working on a JavaScript/HTML project served off the local disk (URL like file:///C:/PROJECTS/node/local_server/demo.htm). CDN content is blocked in this situation because the browser sets a different security level for local files. For the CDN to work, the page must be accessed through a URL such as http://localhost:8000/demo.htm.

NodeJS

A plug-in for Node.js called Paperboy acts as a local file server. Paperboy needs to be extracted to the /node_modules/paperboy directory where your node binary lives. You do need to write a quick script to activate paperboy. This is actually a pretty fun way to try Node for the first time. What’s great about this is it works anywhere Node works, so Windows, Mac, Linux users can all benefit from this.

The local http server script:

"use strict";

// base requirements
var http = require('http');
var path = require('path');
var paperboy = require('paperboy');

// configuration options
var PORT = 8000;
// the directory is relative to where node.exe is being started from
var DIRECTORY = path.join(path.dirname(__filename), 'local_dev');

// static server
http.createServer(function(req, res) {
  var ip = req.connection.remoteAddress;
  paperboy
    .deliver(DIRECTORY, req, res)
    .addHeader('Expires', 300)
    .after(function(code) {
      log(code, req.url, ip);
    })
    .error(function(code, msg) {
      res.writeHead(code, {'Content-Type': 'text/plain'});
      res.end("Error " + code);
      log(code, req.url, ip, msg);
    })
    .otherwise(function(err) {
      res.writeHead(404, {'Content-Type': 'text/plain'});
      res.end("Error 404: File not found");
      log(404, req.url, ip, err);
    });
}).listen(PORT);

// logging method
function log(code, url, ip, err) {
  var str = code + ' - ' + url + ' - ' + ip;
  if (err)
    str += ' - ' + err;
  console.log(str);
}

Save this script as local_server.js and locate it in the same folder where the node binary lives.  Set the desired PORT and DIRECTORY variables.  Run the binary and pass in which script you want it to load and you are up and running.

$ ./node.exe local_server.js

More on prototypes:

Prototyping is a really smart idea when it comes to building software. This is true even for minor increments that will ultimately be integrated into a larger software system. Prototyping gives you something to show the customer a lot sooner. That way feedback can be incorporated earlier which leads to less re-work.

When it comes to doing JavaScript features, the thing I love about doing a simple proof of concept outside the larger is system is that changes are immediately available in the browser. There is nothing worse than the pain of waiting for a build or app server restart! The local node server technique has made me more effective as a developer. You don’t have to use Node + Paperboy to do JavaScript prototypes, and a CDN is not required either, but together they save time. Plus this gives you an excuse to try out something new. Personally, I like having a few Node.js projects hanging out in my home directory.

This entry was posted in Application Development, Code and tagged , . Bookmark the permalink.

Comments are closed.