3 * Express - router - Route
4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
12 module.exports = Route;
15 * Initialize `Route` with the given HTTP `method`, `path`,
16 * and callback `fn` and `options`.
20 * - `sensitive` enable case-sensitive routes
21 * - `strict` enable strict matching for trailing slashes
22 * - `middleware` array of middleware
24 * @param {String} method
25 * @param {String} path
26 * @param {Function} fn
27 * @param {Object} options.
31 function Route(method, path, fn, options) {
32 options = options || {};
36 this.middleware = options.middleware;
37 this.regexp = normalize(path
44 * Check if this route matches `path` and return captures made.
46 * @param {String} path
51 Route.prototype.match = function(path){
52 return this.regexp.exec(path);
56 * Normalize the given path string,
57 * returning a regular expression.
59 * An empty array should be passed,
60 * which will contain the placeholder
61 * key names. For example "/user/:id" will
62 * then contain ["id"].
64 * @param {String|RegExp} path
66 * @param {Boolean} sensitive
67 * @param {Boolean} strict
72 function normalize(path, keys, sensitive, strict) {
73 if (path instanceof RegExp) return path;
75 .concat(strict ? '' : '/?')
76 .replace(/\/\(/g, '(?:/')
77 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
78 keys.push({ name: key, optional: !! optional });
81 + (optional ? '' : slash)
83 + (optional ? slash : '')
84 + (format || '') + (capture || '([^/]+?)') + ')'
87 .replace(/([\/.])/g, '\\$1')
88 .replace(/\*/g, '(.+)');
89 return new RegExp('^' + path + '$', sensitive ? '' : 'i');