strongloop/loopback — pull request #757 by bajtos (2014-11-06 15:46:53+00:00)
Title: RFC: phased router [do not merge]

A prototype implementation of of a phased router - see #739.

**Usage**

```js
// called inside "routes" phase
app.use('/foo', function(req, res, next) { / * ... */ });

// called amongst the first middleware
app.phase('init').use(compress());

// called after all routes were handled
app.phase('files').use(loopback.static('./public'));

// install a "before" handler
app.phase('init').before(serveFavicon(loopback.faviconFile));

// create a route invoked before "regular" routes
// note that `.after` and `.before` cannot be used for routes yet
app.phase('init').post('/foo', function(req, res) { /* ... */ });
```

**Todo**

- API for registering new phases and specifying their order
- Add more built-in phases (probably in loopback to keep the phased router less opinionated)
- Consider renaming `error` phase to `end` or `final`
- Move server-specific code from `lib/application.js` to a standalone file
- Contribute express enhancement allowing people to configure their own Router implementation to be used as the default router, without the need to copy & past `app.lazyrouter` function.
- Fix `undefined phase` in `PhaseList.run` - see [lib/phase-list.js](https://github.com/strongloop/loopback-phase/blob/60d83bd99f2442571e744219aaaa86001516d7e7/lib/phase-list.js#L152-L153)
- Write a unit-test to verify that router properties like `caseSensitive` are correctly forwarded by `DecoratedRouter`
- Improve loopback-phase to support generic `Phase` implementations (any object with `id` and `run` can be used as a phase)
- Implement customization hooks in `PhaseList.run`
- Implement `PhaseList.get(name)` that throws a helpful error when the phase was not found

/to @raymondfeng @ritch please review, ideally by EOD. Tomorrow my time, I'll start moving the code to a new module `strong-router` and work on the missing items outlined above.