source: Dev/trunk/src/node_modules/express/lib/router/route.js

Last change on this file was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

File size: 1.3 KB
Line 
1
2/**
3 * Module dependencies.
4 */
5
6var utils = require('../utils');
7
8/**
9 * Expose `Route`.
10 */
11
12module.exports = Route;
13
14/**
15 * Initialize `Route` with the given HTTP `method`, `path`,
16 * and an array of `callbacks` and `options`.
17 *
18 * Options:
19 *
20 *   - `sensitive`    enable case-sensitive routes
21 *   - `strict`       enable strict matching for trailing slashes
22 *
23 * @param {String} method
24 * @param {String} path
25 * @param {Array} callbacks
26 * @param {Object} options.
27 * @api private
28 */
29
30function Route(method, path, callbacks, options) {
31  options = options || {};
32  this.path = path;
33  this.method = method;
34  this.callbacks = callbacks;
35  this.regexp = utils.pathRegexp(path
36    , this.keys = []
37    , options.sensitive
38    , options.strict);
39}
40
41/**
42 * Check if this route matches `path`, if so
43 * populate `.params`.
44 *
45 * @param {String} path
46 * @return {Boolean}
47 * @api private
48 */
49
50Route.prototype.match = function(path){
51  var keys = this.keys
52    , params = this.params = []
53    , m = this.regexp.exec(path);
54
55  if (!m) return false;
56
57  for (var i = 1, len = m.length; i < len; ++i) {
58    var key = keys[i - 1];
59
60    var val = 'string' == typeof m[i]
61      ? decodeURIComponent(m[i])
62      : m[i];
63
64    if (key) {
65      params[key.name] = val;
66    } else {
67      params.push(val);
68    }
69  }
70
71  return true;
72};
Note: See TracBrowser for help on using the repository browser.