Useful Platitudes

Notes on programming by Daniel Mendel

Dumbwaiter v0.0.1

I’ve recently found myself doing a lot of experiments in HTML that are only composed of client-side code. The trick is that to properly test them, you have to serve the pages through a host of some kind – anything that you open directly using the file: protocol runs into security measures that prevent the page access to certain types of assets and requests.

In the past I’ve either launched Chrome with --allow-file-access-from-files and used the file: protocol, or served the files through a local Apache or Nginx server. Since I’m lazy, I decided these options were too much work and that instead I wanted a simple CLI to spin up small servers for any local directory.

This morning I wrote dumbwaiter, a tiny, 11 line npm module for doing just that by using the connect.static middleware.

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env node

var connect  = require('connect')
  , path    = require('path')
  , dirPath  = path.resolve( process.argv[2] )
  , PORT     = process.argv[3] || 3000
  , server   = connect()

server.use( connect.static( dirPath ) )
server.listen( PORT )
console.log('serving ' + dirPath + ' on port ' + PORT + ' ...')

Install with npm install -g dumbwaiter, then run as dumbwaiter [rootDir] [port].