Home

Awesome

AutoStatic

Automatically serve static files, with version control (etag supported), compression and CDN support. This makes it possible to skip all the annoying packaging process when deploying your application.

You may also want to try a inline static management module.

Usage

var express = require('express');
var autostatic = require('autostatic');

var app = express();

var as = autostatic({
  root: 'http://img.example.com',
  dir: __dirname + '/public'
});

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

app.locals({
  static: as.helper(),
});

In template:

<script src="${static('/js/abcd.js')}"></script>

this will output as:

<script src="http://img.example.com/js/abcd.min.js?1234567-8900"></script>

The abcd.min.js file is generated by this module automatically. This is an asynchronous process, so it will serve the original /js/abcd.js first, and cache control is handled by express.static middleware. Once the minified version of this file is ready, minified file with etag as suffix (/js/abcd.min.js?122456-123) will be served.

You can set up a unique domain for your assets (img.example.com), in Nginx or Apache, with your public files directory as root (in Nginx) or DocumentRoot (in Apache).

Or, set up an upload method to deploy the compressed file to CDN:

var as = autostatic({
  root: 'http://img1.xxcdn.com',
  upload: function(path, contents) {
    // your upload method
  }
});

Concatenating files

You can use autostatic to serve multiple static files.

<script src="${static('/js/abcd.js', '/js/efgh.js')}"></script>

will output:

<script src="/autostatic??/js/abcd.js,/js/efgh.js"></script>

The path /autostatic can be configured:

autostatic.set({
  route: '/serve_assets'
});

Bitdeli Badge