|
|
@@ -19,6 +19,14 @@ function resJson(obj) { |
|
|
|
this.end(JSON.stringify(obj)); |
|
|
|
} |
|
|
|
|
|
|
|
function methodsMatch(route, req) { |
|
|
|
if (route.method === "ALL") |
|
|
|
return true; |
|
|
|
if (route.method === "GET" && req.method === "HEAD") |
|
|
|
return true; |
|
|
|
return route.method === req.method; |
|
|
|
} |
|
|
|
|
|
|
|
class App { |
|
|
|
constructor(options) { |
|
|
|
options = options || {}; |
|
|
@@ -68,16 +76,23 @@ class App { |
|
|
|
|
|
|
|
var route = null; |
|
|
|
|
|
|
|
// With HEAD requests, we don't want to write anything |
|
|
|
if (req.method === "HEAD") { |
|
|
|
res.write = function() {}; |
|
|
|
var end = res.end; |
|
|
|
res.end = function() { end.call(res); } |
|
|
|
} |
|
|
|
|
|
|
|
// If the route is in the hash map, use that |
|
|
|
var r = this._routeMap[url.pathname]; |
|
|
|
if (r && (r.method === "ALL" || r.method === req.method)) { |
|
|
|
if (r && (methodsMatch(r, req))) { |
|
|
|
route = r; |
|
|
|
|
|
|
|
// Search through the routes list and look for matching routes |
|
|
|
} else { |
|
|
|
for (var i in this._routes) { |
|
|
|
var r = this._routes[i]; |
|
|
|
if (r.method !== "ALL" && r.method !== req.method) |
|
|
|
if (!methodsMatch(r, req)) |
|
|
|
continue; |
|
|
|
|
|
|
|
if (r.pattern.test(req.urlobj.pathname)) { |