Permalink
Browse files

Merge branch 'refactor/router'

  • Loading branch information...
2 parents 805b9ac + 53a16e1 commit 351f6abe4c0327783add1a65ac41feaffd21d60e @tj tj committed Apr 26, 2011
Showing with 482 additions and 313 deletions.
  1. +3 −1 lib/express.js
  2. +53 −28 lib/http.js
  3. +15 −3 lib/https.js
  4. +61 −95 lib/router/index.js
  5. +31 −0 lib/router/methods.js
  6. +64 −0 lib/router/route.js
  7. +7 −1 lib/view/view.js
  8. +3 −185 test/express.test.js
  9. +245 −0 test/router.test.js
View
@@ -11,7 +11,8 @@
var connect = require('connect')
, HTTPSServer = require('./https')
- , HTTPServer = require('./http');
+ , HTTPServer = require('./http')
+ , Route = require('./router/route')
/**
* Re-export connect auto-loaders.
@@ -51,6 +52,7 @@ exports.createServer = function(options){
exports.HTTPServer = HTTPServer;
exports.HTTPSServer = HTTPSServer;
+exports.Route = Route;
/**
* View extensions.
View
@@ -18,13 +18,25 @@ var qs = require('qs')
, utils = connect.utils;
/**
+ * Expose `HTTPServer`.
+ */
+
+exports = module.exports = HTTPServer;
+
+/**
+ * Server proto.
+ */
+
+var app = HTTPServer.prototype;
+
+/**
* Initialize a new `HTTPServer` with optional `middleware`.
*
* @param {Array} middleware
* @api public
*/
-var Server = exports = module.exports = function HTTPServer(middleware){
+function HTTPServer(middleware){
connect.HTTPServer.call(this, []);
this.init(middleware);
};
@@ -33,7 +45,7 @@ var Server = exports = module.exports = function HTTPServer(middleware){
* Inherit from `connect.HTTPServer`.
*/
-Server.prototype.__proto__ = connect.HTTPServer.prototype;
+app.__proto__ = connect.HTTPServer.prototype;
/**
* Initialize the server.
@@ -42,22 +54,17 @@ Server.prototype.__proto__ = connect.HTTPServer.prototype;
* @api private
*/
-Server.prototype.init = function(middleware){
+app.init = function(middleware){
var self = this;
this.cache = {};
- this.match = {};
- this.lookup = {};
this.settings = {};
this.redirects = {};
this.isCallbacks = {};
this._locals = {};
this.dynamicViewHelpers = {};
this.errorHandlers = [];
- // default "home" to /
this.set('home', '/');
-
- // set "env" to NODE_ENV, defaulting to "development"
this.set('env', process.env.NODE_ENV || 'development');
// expose objects to each other
@@ -107,13 +114,31 @@ Server.prototype.init = function(middleware){
this.on('listening', this.registerErrorHandlers.bind(this));
// route lookup methods
+ this.remove = function(url){
+ return self.remove.all(url);
+ };
+
+ this.match = function(url){
+ return self.match.all(url);
+ };
+
+ this.lookup = function(url){
+ return self.lookup.all(url);
+ };
+
methods.forEach(function(method){
self.match[method] = function(url){
return self.router.match(url, 'all' == method
? null
: method);
};
+ self.remove[method] = function(url){
+ return self.router.remove(url, 'all' == method
+ ? null
+ : method);
+ };
+
self.lookup[method] = function(path){
return self.router.lookup(path, 'all' == method
? null
@@ -126,7 +151,7 @@ Server.prototype.init = function(middleware){
* When using the vhost() middleware register error handlers.
*/
-Server.prototype.onvhost = function(){
+app.onvhost = function(){
this.registerErrorHandlers();
};
@@ -137,7 +162,7 @@ Server.prototype.onvhost = function(){
* @api public
*/
-Server.prototype.registerErrorHandlers = function(){
+app.registerErrorHandlers = function(){
this.errorHandlers.forEach(function(fn){
this.use(function(err, req, res, next){
fn.apply(this, arguments);
@@ -156,7 +181,7 @@ Server.prototype.registerErrorHandlers = function(){
* @api public
*/
-Server.prototype.use = function(route, middleware){
+app.use = function(route, middleware){
var app, home, handle;
if ('string' != typeof route) {
@@ -214,7 +239,7 @@ Server.prototype.use = function(route, middleware){
* @api public
*/
-Server.prototype.mounted = function(fn){
+app.mounted = function(fn){
this.__mounted = fn;
return this;
};
@@ -226,7 +251,7 @@ Server.prototype.mounted = function(fn){
* @api public
*/
-Server.prototype.register = function(){
+app.register = function(){
view.register.apply(this, arguments);
return this;
};
@@ -240,8 +265,8 @@ Server.prototype.register = function(){
* @api public
*/
-Server.prototype.helpers =
-Server.prototype.locals = function(obj){
+app.helpers =
+app.locals = function(obj){
utils.merge(this._locals, obj);
return this;
};
@@ -255,7 +280,7 @@ Server.prototype.locals = function(obj){
* @api public
*/
-Server.prototype.dynamicHelpers = function(obj){
+app.dynamicHelpers = function(obj){
utils.merge(this.dynamicViewHelpers, obj);
return this;
};
@@ -303,7 +328,7 @@ Server.prototype.dynamicHelpers = function(obj){
* @api public
*/
-Server.prototype.param = function(name, fn){
+app.param = function(name, fn){
if (Array.isArray(name)) {
name.forEach(function(name){
this.param(name, fn);
@@ -324,7 +349,7 @@ Server.prototype.param = function(name, fn){
* @api public
*/
-Server.prototype.error = function(fn){
+app.error = function(fn){
this.errorHandlers.push(fn);
return this;
};
@@ -338,7 +363,7 @@ Server.prototype.error = function(fn){
* @api public
*/
-Server.prototype.is = function(type, fn){
+app.is = function(type, fn){
if (!fn) return this.isCallbacks[type];
this.isCallbacks[type] = fn;
return this;
@@ -354,7 +379,7 @@ Server.prototype.is = function(type, fn){
* @api public
*/
-Server.prototype.set = function(setting, val){
+app.set = function(setting, val){
if (val === undefined) {
if (this.settings.hasOwnProperty(setting)) {
return this.settings[setting];
@@ -375,7 +400,7 @@ Server.prototype.set = function(setting, val){
* @api public
*/
-Server.prototype.enabled = function(setting){
+app.enabled = function(setting){
return !!this.set(setting);
};
@@ -387,7 +412,7 @@ Server.prototype.enabled = function(setting){
* @api public
*/
-Server.prototype.disabled = function(setting){
+app.disabled = function(setting){
return !this.set(setting);
};
@@ -399,7 +424,7 @@ Server.prototype.disabled = function(setting){
* @api public
*/
-Server.prototype.enable = function(setting){
+app.enable = function(setting){
return this.set(setting, true);
};
@@ -411,7 +436,7 @@ Server.prototype.enable = function(setting){
* @api public
*/
-Server.prototype.disable = function(setting){
+app.disable = function(setting){
return this.set(setting, false);
};
@@ -424,7 +449,7 @@ Server.prototype.disable = function(setting){
* @api public
*/
-Server.prototype.redirect = function(key, url){
+app.redirect = function(key, url){
this.redirects[key] = url;
return this;
};
@@ -438,7 +463,7 @@ Server.prototype.redirect = function(key, url){
* @api public
*/
-Server.prototype.configure = function(env, fn){
+app.configure = function(env, fn){
if ('function' == typeof env) {
fn = env, env = 'all';
}
@@ -451,7 +476,7 @@ Server.prototype.configure = function(env, fn){
// Generate routing methods
function generateMethod(method){
- Server.prototype[method] = function(path){
+ app[method] = function(path){
var self = this;
// Lookup
@@ -475,4 +500,4 @@ methods.forEach(generateMethod);
// Alias delete as "del"
-Server.prototype.del = Server.prototype.delete;
+app.del = app.delete;
View
@@ -14,6 +14,18 @@ var connect = require('connect')
, https = require('https');
/**
+ * Expose `HTTPSServer`.
+ */
+
+exports = module.exports = HTTPSServer;
+
+/**
+ * Server proto.
+ */
+
+var app = HTTPSServer.prototype;
+
+/**
* Initialize a new `HTTPSServer` with the
* given `options`, and optional `middleware`.
*
@@ -22,7 +34,7 @@ var connect = require('connect')
* @api public
*/
-var Server = exports = module.exports = function HTTPSServer(options, middleware){
+function HTTPSServer(options, middleware){
connect.HTTPSServer.call(this, options, []);
this.init(middleware);
};
@@ -31,10 +43,10 @@ var Server = exports = module.exports = function HTTPSServer(options, middleware
* Inherit from `connect.HTTPSServer`.
*/
-Server.prototype.__proto__ = connect.HTTPSServer.prototype;
+app.__proto__ = connect.HTTPSServer.prototype;
// mixin HTTPServer methods
Object.keys(HTTPServer.prototype).forEach(function(method){
- Server.prototype[method] = HTTPServer.prototype[method];
+ app[method] = HTTPServer.prototype[method];
});
Oops, something went wrong.

0 comments on commit 351f6ab

Please sign in to comment.