gist: 2868086, 2012-06-04 12:32:41Z
// Assume errorPage, jsonBody, formBody exist within scope.
function methods(routes, handleHttpForms) {
if (handleHttpForms) {
return createHttpFormsRequestHandler(routes)
}
return requestHandler.bind(this)
}
function requestHandler(req, res) {
var method = req.method,
f = routes[method]
if (f) {
return f.apply(this, arguments)
}
errorPage(req, res, 405)
}
function createHttpFormsRequestHandler(routes) {
return httpFormsRequestHandler
function httpFormsRequestHandler(req, res) {
if (req.method !== "POST") {
return requestHandler.apply(this, arguments)
}
var args = arguments,
self = this
contentTypes(req, {
"application/json": jsonBody,
"default": formBody
})(req, res, extractMethod)
function extractMethod(body) {
var method = body._method,
f = routes[method]
if (f) {
return f.apply(self, args)
}
errorPage(req, res, 405)
}
}
}