Browse Source

better handling of methods

master
mortie 7 years ago
parent
commit
423af95c8e
2 changed files with 18 additions and 3 deletions
  1. 17
    2
      index.js
  2. 1
    1
      package.json

+ 17
- 2
index.js View File

@@ -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)) {

+ 1
- 1
package.json View File

@@ -1,6 +1,6 @@
{
"name": "webframe",
"version": "0.3.3",
"version": "0.3.4",
"description": "Web server.",
"main": "index.js",
"scripts": {

Loading…
Cancel
Save