var path = require('path')
var fs = require('fs')
var Router = require('koa-router')
var router = new Router()

var RELOAD_FILE = path.join(__dirname, './reload-client.js')

module.exports = function reload (httpServer, koaApp, verboseLogging) {
  var reloadCode = fs.readFileSync(RELOAD_FILE, 'utf8')

  var conn

  var WebSocketServer = require('ws').Server
  var wss = new WebSocketServer({ server: httpServer })

  wss.on('connection', (ws) => {
    // Take the current web socket connection and save it to webSocketConnetion so we can use it later in the returned reload function for manually firing reload events.
    conn = ws

    if (verboseLogging) {
      console.log('Reload client connected to server')
    }
  })

  if (verboseLogging) {
    reloadCode = reloadCode.replace('verboseLogging = false', 'verboseLogging = true')
  }

  router.get('/reload/reload.js', function(ctx, next){
    ctx.type = 'text/javascript'
    ctx.body = reloadCode
  })

  koaApp.use(router.routes())
  koaApp.use(router.allowedMethods())

  // Return an object, so that the user can manually reload the server by calling the returned function reload. Using the web socket connection from above, we provide a function called reload which passes the command 'reload' to the function sendMessage. sendMessage sends the message 'reload' over the socket (if the socket is connected) to the client. The client then recieves the messages checks to see if the message is reload and then reloads the page.
  return {
    'server': reload,
    'connection': conn,
    'reload': function () {
      this.sendMessage('reload')
    },
    'sendMessage': function (command) {
      if (conn) {
        conn.send(command, function (error) {
          if (error) {
            console.error(error)
          }
        })
      } else {
        if (verboseLogging) {
          console.log('Cannot send "' + command + '" to client: still not connected')
        }
      }
    }
  }
}

@@ -82,35 +82,8 @@
 r')%0A
-var router = new Router()%0A%0A
 var 
@@ -138,16 +138,42 @@
 ent.js')
+%0Avar router = new Router()
 %0A%0Amodule
@@ -215,16 +215,8 @@
 ver,
- koaApp,
  ver
@@ -862,24 +862,16 @@
 ad.js', 
-function
 (ctx, ne
@@ -873,16 +873,20 @@
 x, next)
+ =%3E 
 %7B%0A    ct
@@ -948,76 +948,8 @@
 %7D)%0A%0A
-  koaApp.use(router.routes())%0A  koaApp.use(router.allowedMethods())%0A
 %0A  /
@@ -1414,16 +1414,44 @@
 eturn %7B%0A
+    'Reloadrouter': router,%0A
     'ser

change router