coffee-middleware

coffee-script middleware for connect.

sudo npm install coffee-middleware
Option Description Default
force Always re-compile coffee-script files on each request. false
once Only check for need to recompile once after each server restart. Useful for reducing disk i/o on production. false
debug Output any debugging messages to the console. false
src Source directory containing the .coffee files. Required.
dest Desitnation directory to output the compiled .js files. <src>
prefix Path which should be stripped from the public pathname.
var coffeeMiddleware = require('coffee-middleware');

var server = connect.createServer(
    coffeeMiddleware({
        src: __dirname + '/public',
        compress: true
    }),
    connect.staticProvider(__dirname + '/public')
);
var coffeeMiddleware = require('coffee-middleware');

var app = express.createServer();

app.configure(function () {
    // Other configuration here...

    app.use(coffeeMiddleware({
        src: __dirname + '/public',
        compress: true
    }));

    app.use(express.static(__dirname + '/public'));
});

When using a different src and dest you can use the prefix option to make the directory structure cleaner.

Requests for static assets (like javascripts) made to the express server use a pathname to look up the file. So if the request is for http://localhost/js/main.js the pathname will be /js/main.js.

The middleware uses that path to determine where to look for coffee-script files. In the original example it looks for the .coffee file at /public/js/main.coffee and compiles it to /public/js/main.js.

If you are using a different src and dest options it causes for more complex directories structures unless you use the prefix option. For example, without the prefix, and with a src of /src/coffee and a dest of /public it would look for the .coffee file at /src/coffee/js/main.coffee and compile it to /public/js/main.js. To make it cleaner you can use the prefix option:

var coffeeMiddleware = require('coffee-middleware');

var app = express.createServer();

app.configure(function () {
    // Other configuration here...

    app.use(coffeeMiddleware({
        dest: __dirname + '/public/js',
        src: __dirname + '/src/coffee',
        prefix: '/js',
        compress: true
    }));

    app.use(express.static(__dirname + '/public'));
});

Using the prefix it changes the pathname from /js/main.js to /main.coffee. With that prefix removed from the pathname it makes things cleaner. With the prefix removed it would look for the .coffee file at /src/coffee/main.coffee and compile it to /public/js/main.js.