Rinser/node_modules/express/lib/router/route.js

79 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-07-20 13:42:07 +00:00
/**
* Module dependencies.
*/
2015-08-14 19:58:05 +00:00
var utils = require('../utils');
2015-07-20 13:42:07 +00:00
/**
2015-08-14 19:58:05 +00:00
* Expose `Route`.
2015-07-20 13:42:07 +00:00
*/
module.exports = Route;
/**
2015-08-14 19:58:05 +00:00
* Initialize `Route` with the given HTTP `method`, `path`,
* and an array of `callbacks` and `options`.
*
* Options:
*
* - `sensitive` enable case-sensitive routes
* - `strict` enable strict matching for trailing slashes
2015-07-20 13:42:07 +00:00
*
2015-08-14 19:58:05 +00:00
* @param {String} method
2015-07-20 13:42:07 +00:00
* @param {String} path
2015-08-14 19:58:05 +00:00
* @param {Array} callbacks
* @param {Object} options.
* @api private
2015-07-20 13:42:07 +00:00
*/
2015-08-14 19:58:05 +00:00
function Route(method, path, callbacks, options) {
options = options || {};
2015-07-20 13:42:07 +00:00
this.path = path;
2015-08-14 19:58:05 +00:00
this.method = method;
this.callbacks = callbacks;
this.regexp = utils.pathRegexp(path
, this.keys = []
, options.sensitive
, options.strict);
2015-07-20 13:42:07 +00:00
}
/**
2015-08-14 19:58:05 +00:00
* Check if this route matches `path`, if so
* populate `.params`.
*
* @param {String} path
* @return {Boolean}
* @api private
2015-07-20 13:42:07 +00:00
*/
2015-08-14 19:58:05 +00:00
Route.prototype.match = function(path){
var keys = this.keys
, params = this.params = []
, m = this.regexp.exec(path);
2015-07-20 13:42:07 +00:00
2015-08-14 19:58:05 +00:00
if (!m) return false;
2015-07-20 13:42:07 +00:00
2015-08-14 19:58:05 +00:00
for (var i = 1, len = m.length; i < len; ++i) {
var key = keys[i - 1];
2015-07-20 13:42:07 +00:00
2015-08-14 19:58:05 +00:00
try {
var val = 'string' == typeof m[i]
? decodeURIComponent(m[i])
: m[i];
} catch(e) {
var err = new Error("Failed to decode param '" + m[i] + "'");
err.status = 400;
throw err;
2015-07-20 13:42:07 +00:00
}
2015-08-14 19:58:05 +00:00
if (key) {
params[key.name] = val;
2015-07-20 13:42:07 +00:00
} else {
2015-08-14 19:58:05 +00:00
params.push(val);
2015-07-20 13:42:07 +00:00
}
}
2015-08-14 19:58:05 +00:00
return true;
2015-07-20 13:42:07 +00:00
};