rc@73: /** rc@73: * Module dependencies. rc@73: */ rc@73: rc@73: var pathRegexp = require('path-to-regexp'); rc@73: var debug = require('debug')('express:router:layer'); rc@73: rc@73: /** rc@73: * Expose `Layer`. rc@73: */ rc@73: rc@73: module.exports = Layer; rc@73: rc@73: function Layer(path, options, fn) { rc@73: if (!(this instanceof Layer)) { rc@73: return new Layer(path, options, fn); rc@73: } rc@73: rc@73: debug('new %s', path); rc@73: options = options || {}; rc@73: this.regexp = pathRegexp(path, this.keys = [], options); rc@73: this.handle = fn; rc@73: } rc@73: rc@73: /** rc@73: * Check if this route matches `path`, if so rc@73: * populate `.params`. rc@73: * rc@73: * @param {String} path rc@73: * @return {Boolean} rc@73: * @api private rc@73: */ rc@73: rc@73: Layer.prototype.match = function(path){ rc@73: var keys = this.keys; rc@73: var params = this.params = {}; rc@73: var m = this.regexp.exec(path); rc@73: var n = 0; rc@73: var key; rc@73: var val; rc@73: rc@73: if (!m) return false; rc@73: rc@73: this.path = m[0]; rc@73: rc@73: for (var i = 1, len = m.length; i < len; ++i) { rc@73: key = keys[i - 1]; rc@73: rc@73: try { rc@73: val = 'string' == typeof m[i] rc@73: ? decodeURIComponent(m[i]) rc@73: : m[i]; rc@73: } catch(e) { rc@73: var err = new Error("Failed to decode param '" + m[i] + "'"); rc@73: err.status = 400; rc@73: throw err; rc@73: } rc@73: rc@73: if (key) { rc@73: params[key.name] = val; rc@73: } else { rc@73: params[n++] = val; rc@73: } rc@73: } rc@73: rc@73: return true; rc@73: };