Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hooks to remove view configuration settings made on parent express application #115

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('./<API_NAME>/config/swagger.json'),
handlers: path.resolve('./<API_NAME>/handlers')
}));

// launch ===================================================================
app.listen(port);
console.log('The magic happens on port ' + port);
```
37 changes: 21 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down