From a650557a89d5a58cf3fcb75224782674631c7b38 Mon Sep 17 00:00:00 2001 From: Chris David Date: Wed, 2 Nov 2016 20:43:02 -0700 Subject: [PATCH 1/4] add hooks to remove view configuration settings made on parent express application. --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ lib/index.js | 36 +++++++++++++++++++++--------------- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0bd360c..2634a5a 100644 --- a/README.md +++ b/README.md @@ -283,3 +283,43 @@ function authorize(req, res, next) { //... } ``` + +### Running alongside a node development webserver + +For production, it is recommended to run the API separate from the +webserver. And as such, this middleware assumes that it is the only +thing running inside the express app. + +However, if you would like to run your API alongside a webserver as a +convenience during development, it is possible. In this scenario, you +will want to do two things: + +1. Remove the express parent app view configuration from the settings +that are configured on the parent app during mount. A convenience +function `expressParentAppRemoveViewSettings` is already prepared to +do exactly this. This will allow any other template rendering system +you may have already configured intact. + +2. Stand up the API server relatively late in your `server.js`. This +will give this middleware a fair chance at being the last thing that +configures required settings on the parent express application. + +Example: + +``` + +... the bulk of your other server stuff goes here ... + +// API ====================================================================== +var Swaggerize = require('swaggerize-express'); +Swaggerize.expressParentAppRemoveViewSettings(); // convenience function to remove view configuration settings. + +app.use(Swaggerize({ + api: path.resolve('.//config/swagger.json'), + handlers: path.resolve('.//handlers') +})); + +// launch =================================================================== +app.listen(port); +console.log('The magic happens on port ' + port); +``` diff --git a/lib/index.js b/lib/index.js index fc7bac4..3290959 100644 --- a/lib/index.js +++ b/lib/index.js @@ -44,26 +44,13 @@ function swaggerize(options) { function mount(app, options) { return function onmount(parent) { - var settings; - parent._router.stack.pop(); //If a mountpath was provided, override basePath in api. options.api.basePath = app.mountpath !== '/' ? app.mountpath : options.api.basePath; - Object.keys(settings = { - 'x-powered-by': false, - 'trust proxy': false, - 'jsonp callback name': null, - 'json replacer': null, - 'json spaces': 0, - 'case sensitive routing': false, - 'strict routing': false, - 'views': null, - 'view cache': false, - 'view engine': false - }).forEach(function (option) { - parent.set(option, settings[option]); + Object.keys(swaggerize.expressParentAppSettings).forEach(function (option) { + parent.set(option, swaggerize.expressParentAppSettings[option]); }); Object.keys(options.express).forEach(function (option) { @@ -83,6 +70,25 @@ function mount(app, options) { }; } +swaggerize.expressParentAppSettings = { + 'x-powered-by': false, + 'trust proxy': false, + 'jsonp callback name': null, + 'json replacer': null, + 'json spaces': 0, + 'case sensitive routing': false, + 'strict routing': false, + 'views': null, + 'view cache': false, + 'view engine': false + }; + +swaggerize.expressParentAppRemoveViewSettings = function() { + delete swaggerize.expressParentAppSettings['views']; + delete swaggerize.expressParentAppSettings['view cache']; + delete swaggerize.expressParentAppSettings['view engine']; +} + /** * Loads the api from a path, with support for yaml.. * @param apiPath From cb8fa914facdc3323c3648d5ecc7e65fa9ad3a0b Mon Sep 17 00:00:00 2001 From: Chris David Date: Thu, 3 Nov 2016 08:50:25 -0700 Subject: [PATCH 2/4] simplify the way express options may be overidden. --- README.md | 17 +++++++++-------- lib/index.js | 12 +++--------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2634a5a..bb65817 100644 --- a/README.md +++ b/README.md @@ -295,10 +295,9 @@ convenience during development, it is possible. In this scenario, you will want to do two things: 1. Remove the express parent app view configuration from the settings -that are configured on the parent app during mount. A convenience -function `expressParentAppRemoveViewSettings` is already prepared to -do exactly this. This will allow any other template rendering system -you may have already configured intact. +that are configured on the parent app during mount. This will allow +any other template rendering system you may have already configured +intact. 2. Stand up the API server relatively late in your `server.js`. This will give this middleware a fair chance at being the last thing that @@ -308,12 +307,14 @@ Example: ``` -... the bulk of your other server stuff goes here ... - -// API ====================================================================== var Swaggerize = require('swaggerize-express'); -Swaggerize.expressParentAppRemoveViewSettings(); // convenience function to remove view configuration settings. +delete Swaggerize.expressOptions['views']; +delete Swaggerize.expressOptions['view cache']; +delete Swaggerize.expressOptions['view engine']; + +... the bulk of your other server stuff goes here ... +// Stand up API ============================================================= app.use(Swaggerize({ api: path.resolve('.//config/swagger.json'), handlers: path.resolve('.//handlers') diff --git a/lib/index.js b/lib/index.js index 3290959..6718e24 100644 --- a/lib/index.js +++ b/lib/index.js @@ -49,8 +49,8 @@ function mount(app, options) { //If a mountpath was provided, override basePath in api. options.api.basePath = app.mountpath !== '/' ? app.mountpath : options.api.basePath; - Object.keys(swaggerize.expressParentAppSettings).forEach(function (option) { - parent.set(option, swaggerize.expressParentAppSettings[option]); + Object.keys(swaggerize.expressOptions).forEach(function (option) { + parent.set(option, swaggerize.expressOptions[option]); }); Object.keys(options.express).forEach(function (option) { @@ -70,7 +70,7 @@ function mount(app, options) { }; } -swaggerize.expressParentAppSettings = { +swaggerize.expressOptions = { 'x-powered-by': false, 'trust proxy': false, 'jsonp callback name': null, @@ -83,12 +83,6 @@ swaggerize.expressParentAppSettings = { 'view engine': false }; -swaggerize.expressParentAppRemoveViewSettings = function() { - delete swaggerize.expressParentAppSettings['views']; - delete swaggerize.expressParentAppSettings['view cache']; - delete swaggerize.expressParentAppSettings['view engine']; -} - /** * Loads the api from a path, with support for yaml.. * @param apiPath From 367418c8770f10f3b3321fbde1d236d7f291a2ab Mon Sep 17 00:00:00 2001 From: Chris David Date: Sun, 6 Nov 2016 20:37:25 -0700 Subject: [PATCH 3/4] ensure each option is set on express app just once, even if duplicated between defaults and passed in options. --- lib/index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6718e24..ac0220c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -49,12 +49,17 @@ function mount(app, options) { //If a mountpath was provided, override basePath in api. options.api.basePath = app.mountpath !== '/' ? app.mountpath : options.api.basePath; - Object.keys(swaggerize.expressOptions).forEach(function (option) { - parent.set(option, swaggerize.expressOptions[option]); + var expressOptions = {}; + Object.keys(swaggerize.defaultExpressOptions).forEach(function (option) { + expressOptions[option] = swaggerize.defaultExpressOptions[option]; }); Object.keys(options.express).forEach(function (option) { - parent.set(option, options.express[option]); + expressOptions[option] = options.express[option]; + }); + + Object.keys(expressOptions).forEach(function (option) { + parent.set(option, expressOptions[option]); }); parent.mountpath = options.api.basePath; @@ -70,7 +75,7 @@ function mount(app, options) { }; } -swaggerize.expressOptions = { +swaggerize.defaultExpressOptions = { 'x-powered-by': false, 'trust proxy': false, 'jsonp callback name': null, From 1c5ca49286ac8f3115715a8451f14c29d2afe52c Mon Sep 17 00:00:00 2001 From: Chris David Date: Thu, 10 Nov 2016 19:16:59 -0700 Subject: [PATCH 4/4] update README with defaultExpressOptions variable name. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bb65817..fde0350 100644 --- a/README.md +++ b/README.md @@ -308,9 +308,9 @@ Example: ``` var Swaggerize = require('swaggerize-express'); -delete Swaggerize.expressOptions['views']; -delete Swaggerize.expressOptions['view cache']; -delete Swaggerize.expressOptions['view engine']; +delete Swaggerize.defaultExpressOptions['views']; +delete Swaggerize.defaultExpressOptions['view cache']; +delete Swaggerize.defaultExpressOptions['view engine']; ... the bulk of your other server stuff goes here ...