5 var pathRegexp = require('path-to-regexp');
6 var debug = require('debug')('express:router:layer');
12 var hasOwnProperty = Object.prototype.hasOwnProperty;
18 module.exports = Layer;
20 function Layer(path, options, fn) {
21 if (!(this instanceof Layer)) {
22 return new Layer(path, options, fn);
25 debug('new %s', path);
26 options = options || {};
29 this.name = fn.name || '<anonymous>';
30 this.params = undefined;
31 this.path = undefined;
32 this.regexp = pathRegexp(path, this.keys = [], options);
34 if (path === '/' && options.end === false) {
35 this.regexp.fast_slash = true;
40 * Handle the error for the layer.
42 * @param {Error} error
43 * @param {Request} req
44 * @param {Response} res
45 * @param {function} next
49 Layer.prototype.handle_error = function handle_error(error, req, res, next) {
52 if (fn.length !== 4) {
53 // not a standard error handler
58 fn(error, req, res, next);
65 * Handle the request for the layer.
67 * @param {Request} req
68 * @param {Response} res
69 * @param {function} next
73 Layer.prototype.handle_request = function handle(req, res, next) {
77 // not a standard request handler
89 * Check if this route matches `path`, if so
92 * @param {String} path
97 Layer.prototype.match = function match(path) {
99 // no path, nothing matches
100 this.params = undefined;
101 this.path = undefined;
105 if (this.regexp.fast_slash) {
106 // fast path non-ending match for / (everything matches)
112 var m = this.regexp.exec(path);
115 this.params = undefined;
116 this.path = undefined;
124 var keys = this.keys;
125 var params = this.params;
131 for (var i = 1, len = m.length; i < len; ++i) {
136 val = decode_param(m[i]);
138 if (val !== undefined || !(hasOwnProperty.call(params, prop))) {
147 * Decode param value.
149 * @param {string} val
154 function decode_param(val){
155 if (typeof val !== 'string') {
160 return decodeURIComponent(val);
162 var err = new TypeError("Failed to decode param '" + val + "'");