You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.3 KiB

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