 middleware = (options) ->
 
   # Start the Python mobwrite daemon.
+  debug = options?.debug?
   logger = options?.logger
   daemonPort = options?.port or DEFAULT_DAEMON_PORT
   daemonHost = options?.host or DEFAULT_DAEMON_HOST
-  root = options?.root or "__mobwrite__"
+  root = options?.root or "mobwrite"
   serve({logger: logger, port: daemonPort, host: daemonHost})
 
   # Make a pattern that we can match inbound requests for mobwrite middleware.
-  rootPattern = new RegExp("#{root}/([^\/]+)")
+  rootPattern = new RegExp("#{root}/([^\\/\\?]+)")
+
+  # Set up default Javascript configs. These can always be overridden in the
+  # HTML anyway, but setting sane defaults helps.
+  clientOptions =
+    debug: debug
+    syncGateway: "/#{root}/sync"
+    syncInterval: options?.minSyncInterval or DEFAULT_MIN_SYNC_INTERVAL
+    minSyncInterval: options?.minSyncInterval or DEFAULT_MIN_SYNC_INTERVAL
+    maxSyncInterval: options?.maxSyncInterval or DEFAULT_MAX_SYNC_INTERVAL
+  clientJavascript = """
+    ;(function(){
+      #{if debug then MOBWRITE_DEBUG_JAVASCRIPT else MOBWRITE_MINIFIED_JAVASCRIPT}
+      var options = JSON.parse(#{JSON.stringify(JSON.stringify(clientOptions))})
+      for (var key in options) {
+        if (options.hasOwnProperty(key)) {
+          mobwrite[key] = options[key]
+        }
+      }
+    }).call(this);
+    """
 
   # Create a connect middleware (e.g. for use in ExpressJS).
   # TODO: verify that this works with plain http.createServer()

  return (req, res, next) ->
    bodyParser req, res, ->

      # Respond to any requests to the sample q.py/q.php/q.jsp endpoints
      # by grabbing the "q" or "p" parameters and interacting with the Daemon.
      # TODO: implement as "special" URLs, and offer JS from here
      # /__mobwrite__/sync (POST)
      # /__mobwrite__/client.js (GET)
      # /__mobwrite__/forms.js (GET)
      if MOBWRITE_SAMPLE_ENDPOINT_PATTERN.test(req.url) or rootPattern.test(req.url)
        clientNeedsJsonp = req.query.p?
        daemonRequest = req.body?.q or req.query.p or "\n"
        daemonResponse = ""

              </table>
          </form>
          <script>
              mobwrite.share('mobwrite-form');
          </script>
      </body>
