From 2c2301780a715f07d553ee51876001e681f10392 Mon Sep 17 00:00:00 2001 From: Arthur Silber Date: Tue, 11 Oct 2016 18:29:29 +0200 Subject: [PATCH 1/3] Allows for control of resulting url of static files --- README.md | 7 +++++ src/availableBricks/staticAssets.coffee | 41 +++++++++++++++++++------ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1222c6f..ad445ec 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,13 @@ resulting out of different required versions. - `developmentMode` defines whether to apply production settings or not. Defaults to true. - `bundleName` defines how to name the code bundle, defaults to `client.js` +** staticAssets ** + +- `moduleAssetPaths` defines subfolders of modules whose content should be served statically. Per default, it is set to +`['public']`, serving all files in `/public` under the root URL `/`. Instead of specifying a string path, +you can also use an object `{ urlRoot: '/moduleAssets', path: 'assets' }` to control the resulting URL. +- `staticNodeModulesPaths` same as above, but specifies what node modules should be served statically, e.g. `['font-awesome']`. + ## Development Although the ideas and most of the code of ServerBricks is used in multiple production apps, the module itself is fairly new and still needs some work and polishing - feel free to file issues and create pull requests. diff --git a/src/availableBricks/staticAssets.coffee b/src/availableBricks/staticAssets.coffee index e344fb4..9fe857c 100644 --- a/src/availableBricks/staticAssets.coffee +++ b/src/availableBricks/staticAssets.coffee @@ -9,28 +9,49 @@ module.exports = class StaticAssets extends Brick @staticPaths = config.moduleAssetPaths || ['public'] @staticNodeModulePaths = config.nodeModuleAssetPaths || [] + @staticPaths = @_ensureUrlAndPath @staticPaths + @staticNodeModulePaths = @_ensureUrlAndPath @staticNodeModulePaths + + # Ensures that we know the url and path for each entry. For strings, the global + # url root '/' is assumed + _ensureUrlAndPath: (inputArray) -> + output = [] + + for entry in inputArray + if typeof entry is 'string' + # Assume root mount point + output.push { + urlRoot: '/' + path: entry + } + else + # Assume object with correct keys + output.push entry + + return output + prepareInitialization: (@expressApp, @log) => @log.debug '[ServerBricks] initializes staticAssets brick' - for nodeModulePath in @staticNodeModulePaths - pathToUse = path.join 'node_modules', nodeModulePath - @expressApp.use express.static(pathToUse) - @log.debug "[ServerBricks] Serve static asset node-module path '#{pathToUse}'" + for mountPoint in @staticNodeModulePaths + pathToUse = path.join 'node_modules', mountPoint.path + @expressApp.use mountPoint.urlRoot, express.static(pathToUse) + @log.debug "[ServerBricks] Serve static asset node-module path '#{mountPoint.urlRoot}' -> #{pathToUse}'" # called on each module initializeModule: (moduleFolder) => p = Promise.resolve() p = p.then => servePromises = [] - for staticPath in @staticPaths - servePromises.push @_serveStaticallyIfExists(moduleFolder, staticPath) + for mountPoint in @staticPaths + servePromises.push @_serveStaticallyIfExists(moduleFolder, mountPoint) return Promise.all(servePromises) return p - _serveStaticallyIfExists: (moduleFolder, staticPath) -> - pubFolder = path.join moduleFolder, staticPath + _serveStaticallyIfExists: (moduleFolder, mountPoint) -> + pubFolder = path.join moduleFolder, mountPoint.path return directoryUtils.directoryExists(pubFolder) .then (doesExist) => if doesExist - @expressApp.use express.static(pubFolder) - @log.debug "[ServerBricks] Serve static asset module path '#{pubFolder}'" + @expressApp.use mountPoint.urlRoot, express.static(pubFolder) + @log.debug "[ServerBricks] Serve static asset module path '#{mountPoint.urlRoot}' -> #{pubFolder}'" From 350af54ba5dbddf3fc9760b8a98c42bc205c5009 Mon Sep 17 00:00:00 2001 From: Arthur Silber Date: Tue, 11 Oct 2016 18:38:52 +0200 Subject: [PATCH 2/3] Adds missing express dependency --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index a51e3ce..9cb84a5 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "bluebird": "3.4.3", "browserify-middleware": "7.0.0", "coffeeify": "2.0.1", + "express": "^4.14.0", "mongoose": "4.5.10", "node-sass-middleware": "0.9.8", "pug": "2.0.0-beta6" From 4efb1be706806a29788436430a8d65151a823f0a Mon Sep 17 00:00:00 2001 From: Arthur Silber Date: Tue, 11 Oct 2016 18:39:20 +0200 Subject: [PATCH 3/3] Adds test for staticAssets input handling --- test/availableBricks/staticAssets.coffee | 23 +++++++++++++++++++++++ test/dummyTest.coffee | 9 --------- 2 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 test/availableBricks/staticAssets.coffee delete mode 100644 test/dummyTest.coffee diff --git a/test/availableBricks/staticAssets.coffee b/test/availableBricks/staticAssets.coffee new file mode 100644 index 0000000..3a294a7 --- /dev/null +++ b/test/availableBricks/staticAssets.coffee @@ -0,0 +1,23 @@ +chai = require 'chai' +expect = chai.expect + +StaticAssets = require '../../src/availableBricks/staticAssets' + +describe 'StaticAssets', -> + it 'should handle strings and object as input data', -> + testInput = [ + 'myStringPath' + { urlRoot: '/myUrlPathRoot', path: 'myUrlPathPath' } + ] + + expectedOutput = [ + { urlRoot: '/', path: 'myStringPath' } + { urlRoot: '/myUrlPathRoot', path: 'myUrlPathPath' } + ] + + staticAssetsBrick = new StaticAssets() + + actualOutput = staticAssetsBrick._ensureUrlAndPath testInput + expect(actualOutput).to.eql expectedOutput + return + return diff --git a/test/dummyTest.coffee b/test/dummyTest.coffee deleted file mode 100644 index 6bb117d..0000000 --- a/test/dummyTest.coffee +++ /dev/null @@ -1,9 +0,0 @@ -# This is a dummy test file. -# Once you have own tests, feel free to remove this file. - -chai = require 'chai' -expect = chai.expect - -describe 'Dummy Test', -> - it 'should perform a dummy test', -> - expect(1).to.eql(1)