Node.js module for delivering optimized, minified, mangled, gzipped, and CDN-hosted assets in Express (currently by Amazon S3 and Amazon CloudFront).
## Usage
Server
var express = require('express')
, path = require('path')
, app = express.createServer()
, semver = require('semver');
var options = {
publicDir : path.join(__dirname, 'public')
, viewsDir : path.join(__dirname, 'views')
, domain : 'cdn.your-domain.com'
, bucket : 'bucket-name'
, key : 'amazon-s3-key'
, secret : 'amazon-s3-secret'
, hostname : 'localhost'
, port : 1337
, ssl : false
, production : true
};
var CDN = require('express-cdn')(app, options);
app.configure(function() {
app.set('view engine', 'jade');
app.set('view options', { layout: false, pretty: true });
app.enable('view cache');
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'public')));
});
if (semver.lt(express.version, '3.0.0')) {
app.locals({ CDN: CDN() });
} else {
app.dynamicHelpers({ CDN: CDN });
}
app.get('/', function(req, res, next) {
res.render('basic');
return;
});
console.log("Server started: http://localhost:1337");
app.listen(1337);
View Engine
Jade
// #1 - Load an image
!= CDN('/img/sprite.png')
// #2 - Load an image with a custom tag attribute
!= CDN('/img/sprite.png', { alt: 'Sprite' })
// #3 - Load a script
!= CDN('/js/script.js')
// #4 - Load a script with a custom tag attribute
!= CDN('/js/script.js', { 'data-message': 'Hello' })
// #5 - Load and concat two scripts
!= CDN([ '/js/plugins.js', '/js/script.js' ])
// #6 - Load and concat two scripts with custom tag attributes
!= CDN([ '/js/plugins.js', '/js/script.js' ], { 'data-message': 'Hello' })
// #7 - Load a stylesheet
!= CDN('/css/style.css')
// #8 - Load and concat two stylesheets
!= CDN([ '/css/style.css', '/css/extra.css' ])
EJS
<!-- #1 - Load an image -->
<%- CDN('/img/sprite.png') %>
<!-- #2 - Load an image with a custom tag attribute -->
<%- CDN('/img/sprite.png', { alt: 'Sprite' }) %>
<!-- #3 - Load a script -->
<%- CDN('/js/script.js') %>
<!-- #4 - Load a script with a custom tag attribute -->
<%- CDN('/js/script.js', { 'data-message': 'Hello' }) %>
<!-- #5 - Load and concat two scripts -->
<%- CDN([ '/js/plugins.js', '/js/script.js' ]) %>
<!-- #6 - Load and concat two scripts with custom tag attributes -->
<%- CDN([ '/js/plugins.js', '/js/script.js' ], { 'data-message': 'Hello' }) %>
<!-- #7 - Load a stylesheet -->
<%- CDN('/css/style.css') %>
<!-- #8 - Load and concat two stylesheets -->
<%- CDN([ '/css/style.css', '/css/extra.css' ]) %>
Automatically Rendered HTML
Development Mode
<img src="/img/sprite.png?v=1341214029" />
<img src="/img/sprite.png?v=1341214029" alt="Sprite" />
<script src="/js/script.js?v=1341214029" type="text/javascript"></script>
<script src="/js/script.js?v=1341214029" type="text/javascript" data-message="Hello"></script>
<script src="/js/plugins.js?v=1341214029" type="text/javascript"></script>
<script src="/js/script.js?v=1341214029" type="text/javascript"></script>
<script src="/js/plugins.js?v=1341214029" type="text/javascript" data-message="Hello"></script>
<script src="/js/script.js?v=1341214029" type="text/javascript" data-message="Hello"></script>
<link href="/css/style.css?v=1341214029" rel="stylesheet" type="text/css" />
<link href="/css/style.css?v=1341214029" rel="stylesheet" type="text/css" />
<link href="/css/extra.css?v=1341214029" rel="stylesheet" type="text/css" />
Production Mode
The protocol will automatically change to "https" or "http" depending on the SSL option.
The module will automatically upload and detect new/modified assets based off timestamp,
as it utilizes the timestamp for version control! There is built-in magic to detect if
individual assets were changed when concatenating multiple assets together (it adds the
timestamps together and checks if the combined asset timestamp on S3 exists!).
<img src="https://cdn.your-site.com/img/sprite.1341382571.png" />
<img src="https://cdn.your-site.com/img/sprite.1341382571.png" alt="Sprite" />
<script src="https://cdn.your-site.com/js/script.1341382571.js" type="text/javascript"></script>
<script src="https://cdn.your-site.com/js/script.1341382571.js" type="text/javascript" data-message="Hello"></script>
<script src="https://cdn.your-site.com/plugins%2Bscript.1341382571.js" type="text/javascript"></script>
<script src="https://cdn.your-site.com/plugins%2Bscript.1341382571.js" type="text/javascript" data-message="Hello"></script>
<link href="https://cdn.your-site.com/css/style.1341382571.css" rel="stylesheet" type="text/css" />
<link href="https://cdn.your-site.com/style%2Bextra.1341382571.css" rel="stylesheet" type="text/css" />