diff --git a/README.md b/README.md index 0bd360c..fde0350 100644 --- a/README.md +++ b/README.md @@ -283,3 +283,44 @@ 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. 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: + +``` + +var Swaggerize = require('swaggerize-express'); +delete Swaggerize.defaultExpressOptions['views']; +delete Swaggerize.defaultExpressOptions['view cache']; +delete Swaggerize.defaultExpressOptions['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') +})); + +// launch =================================================================== +app.listen(port); +console.log('The magic happens on port ' + port); +``` diff --git a/lib/index.js b/lib/index.js index fc7bac4..ac0220c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -44,30 +44,22 @@ 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]); + 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; @@ -83,6 +75,19 @@ function mount(app, options) { }; } +swaggerize.defaultExpressOptions = { + '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 + }; + /** * Loads the api from a path, with support for yaml.. * @param apiPath