Skip to content

Instantly share code, notes, and snippets.

@jlank
Created June 12, 2013 18:44
Show Gist options
  • Save jlank/5767973 to your computer and use it in GitHub Desktop.
Save jlank/5767973 to your computer and use it in GitHub Desktop.
Automatic HTTP OPTIONS for Expressjs routes... place after all other routes. Doesn't fully conform to the protocol spec per: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html w/r/t the `Max-Forwards` header. Would also like to add helper to automagically render a response body per: http://zacstewart.com/2012/04/14/http-options-method.html ..…
app.options('*', function (req, res) {
var path = req.originalUrl;
var allow = [];
for (var opt in app.routes) app.routes[opt].forEach(findRoutes);
function findRoutes (route) {
if (path.match(route.regexp)) allow.push(opt.toUpperCase());
}
if (allow.length === 1) return res.send(404); // means its only OPTIONS
res.header('Allow', allow.join(','));
res.send(200);
});
@jlank
Copy link
Author

jlank commented Jun 12, 2013

https://developers.helloreverb.com/swagger/ for the response body maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment