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

this.end(JSON.stringify(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 { class App {
constructor(options) { constructor(options) {
options = options || {}; options = options || {};


var route = null; 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 // If the route is in the hash map, use that
var r = this._routeMap[url.pathname]; var r = this._routeMap[url.pathname];
if (r && (r.method === "ALL" || r.method === req.method)) {
if (r && (methodsMatch(r, req))) {
route = r; route = r;


// Search through the routes list and look for matching routes // Search through the routes list and look for matching routes
} else { } else {
for (var i in this._routes) { for (var i in this._routes) {
var r = this._routes[i]; var r = this._routes[i];
if (r.method !== "ALL" && r.method !== req.method)
if (!methodsMatch(r, req))
continue; continue;


if (r.pattern.test(req.urlobj.pathname)) { if (r.pattern.test(req.urlobj.pathname)) {

+ 1
- 1
package.json View File

{ {
"name": "webframe", "name": "webframe",
"version": "0.3.3",
"version": "0.3.4",
"description": "Web server.", "description": "Web server.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

Loading…
Cancel
Save