Started Route implementation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent fb655f4 · Browse files · 2 files·+62 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+62 added−54 removed
- other:+62 added−54 removed
Provenance
- Commit
8b2268c- Tree
06d3b59- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
fb655f4
2 files changed+62 added−54 removed
modified:lib/router/index.js+12 added-52 removed
9 unmodified lines10 */11 12 var utils = require('../utils')13 , parse = require('url').parse14- , _methods = require('./methods');14+ , _methods = require('./methods')15+ , Route = require('./route');16 17 /**18 * Expose router.19 */62 unmodified lines82 fn.middleware = middleware;83 84 if (!path) throw new Error(name + ' route requires a path');85 if (!fn) throw new Error(name + ' route ' + path + ' requires a callback');85- var regexp = path instanceof RegExp86- ? path87- : normalizePath(path, keys);88- localRoutes.push({89- fn: fn90- , path: regexp91- , keys: keys92- , orig: path93- , method: name94- });86+87+ var route = new Route(name, path, fn);88+ localRoutes.push(route);89 return self;90 };91 }92 5 unmodified lines98 if (route = match(req, routes, i)) {99 var i = 0100 , keys = route.keys;101 108- req.params = route.params;102+ req.params = route.callback.params;103 104 // Param preconditions105 (function param(err) {106 try {20 unmodified lines127 } else if (!key) {128 // route middleware129 i = 0;130 (function nextMiddleware(err){137- var fn = route.middleware[i++];131+ var fn = route.callback.middleware[i++];132 if ('route' == err) {133 pass(req._route_index + 1);134 } else if (err) {135 next(err);136 } else if (fn) {137 fn(req, res, nextMiddleware);138 } else {145- route.call(self, req, res, function(err){139+ route.callback.call(self, req, res, function(err){140 if (err) {141 next(err);142 } else {143 pass(req._route_index + 1);115 unmodified lines259 });260 }261 262 /**269- * Normalize the given path string,270- * returning a regular expression.271- *272- * An empty array should be passed,273- * which will contain the placeholder274- * key names. For example "/user/:id" will275- * then contain ["id"].276- *277- * @param {String} path278- * @param {Array} keys279- * @return {RegExp}280- * @api private281- */282-283-function normalizePath(path, keys) {284- path = path285- .concat('/?')286- .replace(/\/\(/g, '(?:/')287- .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){288- keys.push(key);289- slash = slash || '';290- return ''291- + (optional ? '' : slash)292- + '(?:'293- + (optional ? slash : '')294- + (format || '') + (capture || '([^/]+?)') + ')'295- + (optional || '');296- })297- .replace(/([\/.])/g, '\\$1')298- .replace(/\*/g, '(.+)');299- return new RegExp('^' + path + '$', 'i');300-}301-302-/**263 * Attempt to match the given request to264 * one of the routes. When successful265 * a route function is returned.266 *12 unmodified lines279 var url = parse(req.url)280 , pathname = url.pathname;281 for (var len = routes.length; i < len; ++i) {282 var route = routes[i]323- , fn = route.fn324- , path = route.path325- , keys = fn.keys = route.keys;283+ , fn = route.callback284+ , path = route.regexp285+ , keys = route.keys;286 if (captures = path.exec(pathname)) {287 fn.method = method;288 fn.params = [];289 for (var j = 1, len = captures.length; j < len; ++j) {7 unmodified lines297 fn.params.push(val);298 }299 }300 req._route_index = i;341- return fn;301+ return route;302 }303 }304 }305 }modified:lib/router/route.js+50 added-2 removed
9 unmodified lines1011121314151415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263649 unmodified lines */
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');}No newline at end of fileNo newline at end of file