Skip to content

Commit

Permalink
Merge pull request #11 from AtiX/feature/10_controlUrlOfStaticFiles
Browse files Browse the repository at this point in the history
Feature/10 control url of static files
  • Loading branch information
AtiX authored Oct 12, 2016
2 parents 9ff9d84 + 4efb1be commit af8d997
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<module>/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.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 31 additions & 10 deletions src/availableBricks/staticAssets.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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}'"
23 changes: 23 additions & 0 deletions test/availableBricks/staticAssets.coffee
Original file line number Diff line number Diff line change
@@ -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
9 changes: 0 additions & 9 deletions test/dummyTest.coffee

This file was deleted.

0 comments on commit af8d997

Please sign in to comment.