From 928cb3c2102448d93cb00c93db76b2d8a9e0d59f Mon Sep 17 00:00:00 2001 From: Joseph Sutton Date: Sat, 14 Jan 2017 17:24:30 -0800 Subject: [PATCH] (add) options to static assets --- legacy/http.framework.express.js | 6 ++--- legacy/service.middleware/defaultRoutes.js | 31 +++++++++++++--------- lib/http.framework.express.js | 6 ++--- lib/service.middleware/defaultRoutes.js | 31 +++++++++++++--------- package.json | 2 +- 5 files changed, 44 insertions(+), 32 deletions(-) diff --git a/legacy/http.framework.express.js b/legacy/http.framework.express.js index b2cd39f..1bcfedd 100755 --- a/legacy/http.framework.express.js +++ b/legacy/http.framework.express.js @@ -189,11 +189,11 @@ HttpFrameworkExpress.prototype.addMethodFunction = function (method, middlewareL this._app[method](routeStr, handler); }; -HttpFrameworkExpress.prototype.addStaticDir = function (staticDir, staticRoute) { +HttpFrameworkExpress.prototype.addStaticDir = function (staticDir, staticRoute, staticOptions) { if (staticRoute) { - return this._app.use(staticRoute, express.static(staticDir)); + return this._app.use(staticRoute, express.static(staticDir, staticOptions)); } else { - return this._app.use(express.static(staticDir)); + return this._app.use(express.static(staticDir, staticOptions)); } }; diff --git a/legacy/service.middleware/defaultRoutes.js b/legacy/service.middleware/defaultRoutes.js index ce8c2e9..dbb1827 100644 --- a/legacy/service.middleware/defaultRoutes.js +++ b/legacy/service.middleware/defaultRoutes.js @@ -136,22 +136,29 @@ var DefaultRoutes = function (_ServiceMiddleware) { }, { key: '_addStaticRoute', value: function _addStaticRoute(service, staticContent, route) { + var staticOptions = {}; if (!_.isArray(staticContent) && _.isObject(staticContent)) { var staticRoute = staticContent; - if (staticRoute.hasOwnProperty('from') && staticRoute.hasOwnProperty('to')) { + if (staticRoute.hasOwnProperty('from')) { staticContent = staticRoute.from; + } + if (staticRoute.hasOwnProperty('to')) { route = staticRoute.to; - } else { - if (staticRoute.hasOwnProperty('root')) { - service.directory.service = staticRoute.root; - } - if (staticRoute.hasOwnProperty('cache')) { - // TODO - } - if (staticRoute.hasOwnProperty('list')) { - staticContent = staticRoute.list; - } + } + if (staticRoute.hasOwnProperty('root')) { + service.directory.service = staticRoute.root; + } + if (staticRoute.hasOwnProperty('cache')) {} + // TODO + + // this oversides "from" + if (staticRoute.hasOwnProperty('list')) { + staticContent = staticRoute.list; + } + + if (staticRoute.hasOwnProperty('options') && _.isObject(staticRoute.options)) { + staticOptions = staticRoute.options; } } @@ -177,7 +184,7 @@ var DefaultRoutes = function (_ServiceMiddleware) { // logger.log("Adding Static Dir Content -", staticContent); logger.log('Static Dir Route:', staticContent, '->', route || '/'); - this._httpFramework.addStaticDir(staticContent, route); + this._httpFramework.addStaticDir(staticContent, route, staticOptions); return true; } else { // logger.log("Adding Static File -", staticContent); diff --git a/lib/http.framework.express.js b/lib/http.framework.express.js index 799bd44..11f9746 100755 --- a/lib/http.framework.express.js +++ b/lib/http.framework.express.js @@ -194,12 +194,12 @@ HttpFrameworkExpress.prototype.addMethodFunction = function (method, middlewareL this._app[ method ](routeStr, handler); }; -HttpFrameworkExpress.prototype.addStaticDir = function (staticDir, staticRoute) { +HttpFrameworkExpress.prototype.addStaticDir = function (staticDir, staticRoute, staticOptions) { if (staticRoute) { - return this._app.use(staticRoute, express.static(staticDir)); + return this._app.use(staticRoute, express.static(staticDir, staticOptions)); } else { - return this._app.use(express.static(staticDir)); + return this._app.use(express.static(staticDir, staticOptions)); } }; diff --git a/lib/service.middleware/defaultRoutes.js b/lib/service.middleware/defaultRoutes.js index 999e05e..827bb1a 100644 --- a/lib/service.middleware/defaultRoutes.js +++ b/lib/service.middleware/defaultRoutes.js @@ -94,25 +94,30 @@ class DefaultRoutes extends ServiceMiddleware { * @private */ _addStaticRoute (service, staticContent, route) { + var staticOptions = {}; if (!_.isArray(staticContent) && _.isObject(staticContent)) { var staticRoute = staticContent; - if (staticRoute.hasOwnProperty('from') && - staticRoute.hasOwnProperty('to')) { + if (staticRoute.hasOwnProperty('from')) { staticContent = staticRoute.from; + } + if (staticRoute.hasOwnProperty('to')) { route = staticRoute.to; } - else { - if (staticRoute.hasOwnProperty('root')) { - service.directory.service = staticRoute.root; - } - if (staticRoute.hasOwnProperty('cache')) { - // TODO - } - if (staticRoute.hasOwnProperty('list')) { - staticContent = staticRoute.list; - } + if (staticRoute.hasOwnProperty('root')) { + service.directory.service = staticRoute.root; + } + if (staticRoute.hasOwnProperty('cache')) { + // TODO + } + // this oversides "from" + if (staticRoute.hasOwnProperty('list')) { + staticContent = staticRoute.list; + } + + if (staticRoute.hasOwnProperty('options') && _.isObject(staticRoute.options)) { + staticOptions = staticRoute.options; } } @@ -139,7 +144,7 @@ class DefaultRoutes extends ServiceMiddleware { // logger.log("Adding Static Dir Content -", staticContent); logger.log('Static Dir Route:', staticContent, '->', route || '/'); - this._httpFramework.addStaticDir(staticContent, route); + this._httpFramework.addStaticDir(staticContent, route, staticOptions); return true; } else { diff --git a/package.json b/package.json index 52c1274..c0d6d90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hyper.io", - "version": "0.7.1", + "version": "0.7.2", "description": "MicroServices so fast they've gone plaid!", "homepage": "https://github.com/jstty/hyper.io#readme", "keywords": [