Comparing version 0.5.2 to 0.5.3
45
index.js
@@ -7,3 +7,3 @@ var http = require('http'); | ||
var METHODS = ['get', 'post', 'put', 'del', 'head', 'options']; | ||
var HTTP_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'UPGRADE', 'OPTIONS']; | ||
var HTTP_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS']; | ||
@@ -33,3 +33,2 @@ var noop = function() {}; | ||
this.server = server; | ||
this.last = {}; | ||
@@ -42,3 +41,8 @@ if (server) { | ||
this._servers = []; | ||
this._end = {}; | ||
this.on('request', function(request, response) { | ||
self._find(request, response); | ||
}); | ||
if (options && options.hang) { | ||
@@ -49,11 +53,4 @@ return; | ||
HTTP_METHODS.forEach(function(method) { | ||
var type = method === 'UPGRADE' ? 'upgrade' : 'request'; | ||
self._methods[method] = []; | ||
self.last[method] = function(request, response) { | ||
if (type === 'upgrade' && !self.listeners('upgrade').length) { | ||
response.destroy(); // to support legacy interface we need to check if anyone else is listening | ||
return; | ||
} | ||
self._end[method] = function(request, response) { | ||
response.writeHead(404); | ||
@@ -63,6 +60,5 @@ response.end(); | ||
}); | ||
}); | ||
METHODS.concat('upgrade').forEach(function(method) { | ||
METHODS.concat('delete').forEach(function(method) { | ||
var httpMethod = method.replace('del', 'delete').toUpperCase(); | ||
@@ -81,3 +77,3 @@ | ||
if (typeof pattern === 'function') { | ||
this.last[httpMethod] = pattern; | ||
this._end[httpMethod] = pattern; | ||
return; | ||
@@ -114,2 +110,7 @@ } | ||
Router.prototype.upgrade = function(fn) { | ||
this.on('upgrade', fn); | ||
return this; | ||
}; | ||
Router.prototype.all = function() { | ||
@@ -126,3 +127,3 @@ var self = this; | ||
Router.prototype.route = function(request, response) { | ||
this._find(request.method, request, response); | ||
this._find(request, response); | ||
}; | ||
@@ -152,8 +153,11 @@ Router.prototype.listen = function(port, callback) { | ||
server.on('request', function(request, response) { | ||
self._find(request.method, request, response); | ||
self.emit('request', request, response); | ||
}); | ||
server.on('upgrade', function(request, connection, head) { | ||
self._find('UPGRADE', request, connection, head); | ||
self.emit('upgrade', request, console, head); | ||
if (!self.listeners('upgrade').length) { | ||
connection.destroy(); | ||
return; | ||
} | ||
self.emit('upgrade', request, connection, head); | ||
}); | ||
@@ -195,5 +199,6 @@ | ||
Router.prototype._find = function(method, request, response) { | ||
Router.prototype._find = function(request, response) { | ||
var method = request.method; | ||
var routes = this._methods[method]; | ||
var last = this.last[method] || noop; | ||
var end = this._end[method] || noop; | ||
var index = 0; | ||
@@ -208,3 +213,3 @@ | ||
if (index >= routes.length) { | ||
last(request, response); | ||
end(request, response); | ||
return; | ||
@@ -211,0 +216,0 @@ } |
{ | ||
"name":"router", | ||
"version":"0.5.2", | ||
"version":"0.5.3", | ||
"description":"A lean and mean web router", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
243
9016