strongloop/loopback — pull request #133 by bajtos (2014-01-13 16:27:23+00:00)
Title: Implement app.installMiddleware

The implementation is mostly a copy of [loopback-workspace/templates/app.js](https://github.com/strongloop/loopback-workspace/blob/e7214ee150e0e5f467e55fa89e8fc9ad93e0d75b/templates/app.js).

What was omitted:

- loopback-explorer. It should be configured from `boot/explorer.js`

```js
try {
var explorer = require('loopback-explorer');
app.on('middleware:handlers', function() {
app.use('/explorer', explorer(app));
});
app.on('start', function() {
console.log('explorer available at etc.');
});
} catch(e){
console.log('run npm install loopback-explorer etc.');
}
```

- status report at the root URL, which is is an ordinary express route and should never go to `app.installMiddleware`. I don't have a solution for loading express routes yet, I suppose we should auto-load all files from `routes/` directory. Anyhow, (custom) express routes are out of scope of this pull request.

```js
// this is the code in app.js
app.get('/', loopback.status())
```

- authentication & swagger remotes.

```js
var swaggerRemote = app.remotes().exports.swagger;
if (swaggerRemote) swaggerRemote.requireToken = false;

app.enableAuth();
```

This code does not belong to `installMiddleware`, so I am keeping it in app.js template for now.

See API docs for description and examples on how to use the new method.

This is how the new app.js template should look like:

```js
// require()s, cluster setup, etc.

app.boot(__dirname);
app.installMiddleware();

// TODO - move to strong-remoting, loopback-explorer or boot/auth.js
var swaggerRemote = app.remotes().exports.swagger;
if (swaggerRemote) swaggerRemote.requireToken = false;

// TODO - move to boot/auth.js
app.enableAuth();

app.start = function() {
// skipped here
}

if(require.main === module) {
app.start();
}
```
/to @ritch @sam-github please review
/cc @raymondfeng @Schoonology