Permalink
Browse files

Started Route implementation

  • Loading branch information...
tj committed Apr 26, 2011
1 parent fb655f4 commit 8b2268cf386c5f456efc488adce88b1f241fc38f
Showing with 62 additions and 54 deletions.
  1. +12 −52 lib/router/index.js
  2. +50 −2 lib/router/route.js
View
@@ -11,7 +11,8 @@
var utils = require('../utils')
, parse = require('url').parse
- , _methods = require('./methods');
+ , _methods = require('./methods')
+ , Route = require('./route');
/**
* Expose router.
@@ -82,16 +83,9 @@ function router(fn){
if (!path) throw new Error(name + ' route requires a path');
if (!fn) throw new Error(name + ' route ' + path + ' requires a callback');
- var regexp = path instanceof RegExp
- ? path
- : normalizePath(path, keys);
- localRoutes.push({
- fn: fn
- , path: regexp
- , keys: keys
- , orig: path
- , method: name
- });
+
+ var route = new Route(name, path, fn);
+ localRoutes.push(route);
return self;
};
}
@@ -105,7 +99,7 @@ function router(fn){
var i = 0
, keys = route.keys;
- req.params = route.params;
+ req.params = route.callback.params;
// Param preconditions
(function param(err) {
@@ -134,15 +128,15 @@ function router(fn){
// route middleware
i = 0;
(function nextMiddleware(err){
- var fn = route.middleware[i++];
+ var fn = route.callback.middleware[i++];
if ('route' == err) {
pass(req._route_index + 1);
} else if (err) {
next(err);
} else if (fn) {
fn(req, res, nextMiddleware);
} else {
- route.call(self, req, res, function(err){
+ route.callback.call(self, req, res, function(err){
if (err) {
next(err);
} else {
@@ -265,40 +259,6 @@ function optionsFor(path, routes) {
});
}
-/**
- * Normalize the given path string,
- * returning a regular expression.
- *
- * An empty array should be passed,
- * which will contain the placeholder
- * key names. For example "/user/:id" will
- * then contain ["id"].
- *
- * @param {String} path
- * @param {Array} keys
- * @return {RegExp}
- * @api private
- */
-
-function normalizePath(path, keys) {
- path = path
- .concat('/?')
- .replace(/\/\(/g, '(?:/')
- .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
- keys.push(key);
- slash = slash || '';
- return ''
- + (optional ? '' : slash)
- + '(?:'
- + (optional ? slash : '')
- + (format || '') + (capture || '([^/]+?)') + ')'
- + (optional || '');
- })
- .replace(/([\/.])/g, '\\$1')
- .replace(/\*/g, '(.+)');
- return new RegExp('^' + path + '$', 'i');
-}
-
/**
* Attempt to match the given request to
* one of the routes. When successful
@@ -320,9 +280,9 @@ function match(req, routes, i) {
, pathname = url.pathname;
for (var len = routes.length; i < len; ++i) {
var route = routes[i]
- , fn = route.fn
- , path = route.path
- , keys = fn.keys = route.keys;
+ , fn = route.callback
+ , path = route.regexp
+ , keys = route.keys;
if (captures = path.exec(pathname)) {
fn.method = method;
fn.params = [];
@@ -338,7 +298,7 @@ function match(req, routes, i) {
}
}
req._route_index = i;
- return fn;
+ return route;
}
}
}
View
@@ -11,6 +11,54 @@
module.exports = Route;
-function Route() {
-
+/**
+ * Initialize `Route` with the given HTTP `method`, `path`,
+ * and callback `fn`.
+ *
+ * @param {String} method
+ * @param {String} path
+ * @param {Function} fn
+ * @api private
+ */
+
+function Route(method, path, fn) {
+ this.callback = fn;
+ this.path = path;
+ this.regexp = normalize(path, this.keys = []);
+ this.method = method;
+}
+
+/**
+ * Normalize the given path string,
+ * returning a regular expression.
+ *
+ * An empty array should be passed,
+ * which will contain the placeholder
+ * key names. For example "/user/:id" will
+ * then contain ["id"].
+ *
+ * @param {String} path
+ * @param {Array} keys
+ * @return {RegExp}
+ * @api private
+ */
+
+function normalize(path, keys) {
+ if (path instanceof RegExp) return path;
+ path = path
+ .concat('/?')
+ .replace(/\/\(/g, '(?:/')
+ .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
+ keys.push(key);
+ slash = slash || '';
+ return ''
+ + (optional ? '' : slash)
+ + '(?:'
+ + (optional ? slash : '')
+ + (format || '') + (capture || '([^/]+?)') + ')'
+ + (optional || '');
+ })
+ .replace(/([\/.])/g, '\\$1')
+ .replace(/\*/g, '(.+)');
+ return new RegExp('^' + path + '$', 'i');
}

0 comments on commit 8b2268c

Please sign in to comment.