var Watcher = require('./watcher')
var middleware = require('./middleware')
var http = require('http')
var connect = require('connect')

exports.serve = serve
function serve (builder, options) {
  options = options || {}

  console.log('Serving on http://' + options.host + ':' + options.port + '\n')

  var watcher = options.watcher || new Watcher(builder, {verbose: true})

  var app = connect().use(middleware(watcher))

  var server = http.createServer(app)

  // We register these so the 'exit' handler removing temp dirs is called
  function cleanupAndExit() {
    builder.cleanup().catch(function(err) {
      console.error('Cleanup error:')
      console.error(err && err.stack ? err.stack : err)
    }).finally(function() {
      process.exit(1)
    })
  }

  process.on('SIGINT', cleanupAndExit)
  process.on('SIGTERM', cleanupAndExit)

  watcher.on('change', function(results) {
    console.log('Built - ' + Math.round(results.totalTime / 1e6) + ' ms @ ' + new Date().toString())
  })

  watcher.on('error', function(err) {
    console.log('Built with error:')
    // Should also show file and line/col if present; see cli.js
    if (err.file) {
      console.log('File: ' + err.file)
    }
    console.log(err.stack)
    console.log('')
  })

  server.listen(parseInt(options.port, 10), options.host)
}

@@ -213,16 +213,34 @@
 ns %7C%7C %7B%7D
+%0A  var server = %7B%7D
 %0A%0A  cons
@@ -319,16 +319,23 @@
 %0A%0A  var 
+server.
 watcher 
@@ -400,16 +400,23 @@
 %0A%0A  var 
+server.
 app = co
@@ -438,16 +438,23 @@
 dleware(
+server.
 watcher)
@@ -468,16 +468,21 @@
 r server
+.http
  = http.
@@ -494,16 +494,23 @@
 eServer(
+server.
 app)%0A%0A  
@@ -889,24 +889,31 @@
 AndExit)%0A%0A  
+server.
 watcher.on('
@@ -1050,16 +1050,23 @@
   %7D)%0A%0A  
+server.
 watcher.
@@ -1322,16 +1322,21 @@
  server.
+http.
 listen(p
@@ -1376,10 +1376,26 @@
 s.host)%0A
+  return server%0A
 %7D%0A

Return a server POJO to allow for easier wrappers on Broccoli Server