airbnb/rendr — pull request #84 by spikebrehm (2013-07-02 14:02:35+00:00)
Title: Refactor Server into OOP; move Express boilerplate to Server

This greatly cleans up the `server/server.js` module, and reduces the amount of boilerplate needed to get a Rendr app up and running. It's meant to go hand-in-hand with the related changes in [`airbnb/rendr-app-template`](https://github.com/airbnb/rendr-app-template/compare/oop-server).

### Creating a server

Using the `rendr.createServer(expressApp, options)` method will attach a singleton `Server` instance at `rendr.server`, which can be used by other parts of the codebase to find i.e. `server.dataAdapter`, `server.viewEngine`, `server.router`, etc. Apps can pass in custom modules to acts as these objects in `options`.

### Managing the Rendr middleware stack

Before this, the application had to [manage its own Rendr middleware and add the Rendr routes manually](https://github.com/airbnb/rendr-app-template/blob/b6fea832bafbd79a4cd4aa730bbfa6dfb10cf855/server/server.js#L79-L115). Now, that is moved into `Server`, and the `server.stack` property is the array of middleware functions to be used before all Rendr routes. Apps can append to this stack (`server.stack.push(middlewareFn)`) or replace it entirely for more custom use cases (`server.stack = [middlewareFn1, middlewareFn2]`).

### Error handlers

We also move over the `errorHandler` and `notFoundHandler` middleware functions from `rendr-app-template` over to `rendr` in order to provide a sane default, while allowing apps to override these by passing into `options`.

/cc @ajacksified @hurrymaplelad @technicolorenvy @jacoblwe20 @logsdon