Browse Source

Hack to fix #49 until we can move to express.

Michelle Bu 10 years ago
parent
commit
6cfbe60031
1 changed files with 20 additions and 0 deletions
  1. 20 0
      lib/server.js

+ 20 - 0
lib/server.js

@@ -192,6 +192,26 @@ PeerServer.prototype._initializeHTTP = function() {
   this._app.use(restify.queryParser());
   this._app.use(util.allowCrossDomain);
 
+  /** Hack from https://github.com/mcavage/node-restify/issues/284, until we switch to express */
+  function unknownMethodHandler(req, res) {
+    if (req.method.toLowerCase() === 'options') {
+      var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version'];
+
+      if (res.methods.indexOf('OPTIONS') === -1) res.methods.push('OPTIONS');
+
+      res.header('Access-Control-Allow-Credentials', true);
+      res.header('Access-Control-Allow-Headers', allowHeaders.join(', '));
+      res.header('Access-Control-Allow-Methods', res.methods.join(', '));
+      res.header('Access-Control-Allow-Origin', req.headers.origin);
+
+      return res.send(204);
+    } else {
+      return res.send(new restify.MethodNotAllowedError());
+    }
+  }
+
+  this._app.on('MethodNotAllowed', unknownMethodHandler);
+
   // Retrieve guaranteed random ID.
   this._app.get(this._options.path + ':key/id', function(req, res, next) {
     res.contentType = 'text/html';