node package manager


connect-assets-cdn Build Status

A helper module for connect-assets to upload your files to a CDN (like Amazon S3)


npm install connect-assets-cdn

Usage (Cake Task)

To create a Cake task to upload new assets, follow this example.



var uploader = require("./src/s3AssetUpload");

uploader.upload(console.log, function(err, uploaded) {
    if(err) {
        return console.log("Error uploading: " + err.message);



express = require 'express'
assets = require "connect-assets"
jsPaths = require "connect-assets-jspaths"

{AssetsCDN} = require "connect-assets-cdn"

# Either create a config file like here or set this to 
# an object with amazon key, secret and bucket values
# Also should have the assetsRoot; e.g. //
config = require "./config"

primeCSS = (assets) ->
    # We only have one file to pre-compile for css
    # You might need to add more for your situation;
    # whatever you reference in your views should be 
    # referenced like this here
    assets.instance.options.helperContext.css "prod"

upload = (log, done) ->

    # Set up a fake express server so we can load connect-assets
    app = express()

    opts = 
        # TODO: Set your assetsRoot
        servePath: config.assetsRoot

    app.use assets opts

    # Prime the CSS files
    primeCSS assets
    # Prime the JS files
    jsPaths assets
    # TODO: Set these from your config
    {key, secret, bucket} =

    # Create our cdn manager and upload
    cdn = new AssetsCDN {assets, key, secret, bucket, log}

    cdn.upload done

module.exports = {upload}


task 's3', 'Upload builtAssets', ->
    # Set production NODE_ENV
    currentEnv = process.env
    currentEnv.NODE_ENV = "production"

    server = spawn 'node', ['s3.js'],
      env: currentEnv

    server.stdout.pipe process.stdout
    server.stderr.pipe process.stderr

Usage (Before Starting Server)

# You can also upload new assets before starting the server...

assets = require 'connect-assets'
{AssetsCDN} = require 'connect-assets-cdn'

# Snip ...

app.use assets
	servePath: '//'

# You should probably only upload for production
if process.env.NODE_ENV == "production"

	cdn = new AssetsCDN 
		assets: assets
		key: "S3_Key"
		secret: "S3_Secret"
		bucket: "S3_Bucket"

	cdn.upload (err, uploadedPaths) ->
		throw err if err

		app.listen 3000, (err) ->
			console.log "Server started"


  • Better uploader tests


Made possible thanks to connect-assets, knox.


Created by Jacob Gable. MIT License; no attribution required.