Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Generic Express.js/Connect middleware handling runtime compilization tasks.
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Build Status

Generic Express.js/Connect middleware handling runtime compilization tasks.


  • Compile source file on requested

  • Cache the compiled data for next request

  • Watch for source file changes

    Once the source file is changed, cache is invalidated.

  • Dependency Supplement interface

    Add extra dependency files to watch

  • Support JSONP request

    Support request with GET /api?callback=?


var compile = require('compile-middleware');
var midware = {
  less: compile({
    filename  : function (req) {            // Source filename resolve
                  // Function obtaining filename 
    src       : '/path/to/source',          // Path to source file
    render    : function (source_path, cb, depend) {
                  // Function render file 
  jade: compile({
    filename  : /(?:\/runtime\/)(.*)\.js/i, // Capture group 1 will be used
    src_ext   : '.jade',                    // Optional, Default ''
    src       : '/path/to/source',
    render    : function (source_path, cb) {
                  // Function rendering file
    headers   : {                           // Optional, HTTP Headers
                  'Cache-Control': 'public, max-age=86400',
                  'Content-Type': 'text/javascript' 


When render function issue an ENOENT error. The middleware will invoke the next() function for other middleware to execute.

Thus, put this middleware before express.static middleware is recommended practice.

Render Function Arguments:

  • source_path

    path to the file to be rendered, file path be made from src, filename expression and src_ext parameter.

  • cb

    callback function, invoke cb(err) on error, cb(null, <data>) on success

  • depend (Optional)

    You can add extra dependency by calling depend("/path/to/extra/depend") or depend(["list/of", "extra/depend", "files"])

    The change on depended file will also invalidate the compiled cache

Either a function or Regular Expression is accepted as filename parameter. When using RegEx, the first capture group will be used as the name of source file. A suffix to filename can be defined by src_ext.

WARNING No not add a g flag to the RegExp, that will broke the filename extraction procedure. It's an V8 Issue

Related Works


(The MIT License)

Something went wrong with that request. Please try again.