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

This commit is contained in:
Michelle Bu 2014-08-29 02:06:20 -04:00
parent d3f1f6868c
commit 6cfbe60031

View File

@ -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';