Skip to content
Myr

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

2 files+62 added−54 removed

modified:lib/router/index.js+12 added-52 removed
9 unmodified lines
10 */1112var utils = require('../utils')13 , parse = require('url').parse14 , _methods = require('./methods');14 , _methods = require('./methods')15 , Route = require('./route');1617/**18 * Expose router.19 */
62 unmodified lines
82 fn.middleware = middleware;8384 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 });8687 var route = new Route(name, path, fn);88 localRoutes.push(route);89 return self;90 };91 }92
5 unmodified lines
98 if (route = match(req, routes, i)) {99 var i = 0100 , keys = route.keys;101108 req.params = route.params;102 req.params = route.callback.params;103104 // Param preconditions105 (function param(err) {106 try {
20 unmodified lines
127 } 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 lines
259 });260}261262/**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 */282283function 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}301302/**263 * Attempt to match the given request to264 * one of the routes. When successful265 * a route function is returned.266 *
12 unmodified lines
279 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 lines
297 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