Created
June 12, 2013 18:44
-
-
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 ..…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://developers.helloreverb.com/swagger/ for the response body maybe?