layer.js
This file
Path
lib/router/layer.js
181 lines · 3.2 KiB · JavaScript
Last changed by
Use "%o" in path debug to tell types apartDouglas Christopher Wilsonbefore adoption8b6dc6c
History of this fileTake it
The walk stopped after 200 commits: lines older than 2ac2509 point at it.
4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 1–37why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */89'use strict';1011/**12 * Module dependencies.13 * @private14 */1516var pathRegexp = require('path-to-regexp');17var debug = require('debug')('express:router:layer');1819/**20 * Module variables.21 * @private22 */2324var hasOwnProperty = Object.prototype.hasOwnProperty;2526/**27 * Module exports.28 * @public29 */3031module.exports = Layer;3233function Layer(path, options, fn) {34 if (!(this instanceof Layer)) {35 return new Layer(path, options, fn);36 }37Use "%o" in path debug to tell types apartDouglas Christopher Wilsonbefore adoption8b6dc6c
line 38why, one step back38 debug('new %o', path)4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 39–46why, one step back39 var opts = options || {};4041 this.handle = fn;42 this.name = fn.name || '<anonymous>';43 this.params = undefined;44 this.path = undefined;45 this.regexp = pathRegexp(path, this.keys = [], opts);46perf: add fast match path for "*" routeDouglas Christopher Wilsonbefore adoption081b811
lines 47–49why, one step back47 // set fast path flags48 this.regexp.fast_star = path === '*'49 this.regexp.fast_slash = path === '/' && opts.end === false4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 50–110why, one step back50}5152/**53 * Handle the error for the layer.54 *55 * @param {Error} error56 * @param {Request} req57 * @param {Response} res58 * @param {function} next59 * @api private60 */6162Layer.prototype.handle_error = function handle_error(error, req, res, next) {63 var fn = this.handle;6465 if (fn.length !== 4) {66 // not a standard error handler67 return next(error);68 }6970 try {71 fn(error, req, res, next);72 } catch (err) {73 next(err);74 }75};7677/**78 * Handle the request for the layer.79 *80 * @param {Request} req81 * @param {Response} res82 * @param {function} next83 * @api private84 */8586Layer.prototype.handle_request = function handle(req, res, next) {87 var fn = this.handle;8889 if (fn.length > 3) {90 // not a standard request handler91 return next();92 }9394 try {95 fn(req, res, next);96 } catch (err) {97 next(err);98 }99};100101/**102 * Check if this route matches `path`, if so103 * populate `.params`.104 *105 * @param {String} path106 * @return {Boolean}107 * @api private108 */109110Layer.prototype.match = function match(path) {lint: consolidate layer match failure pathDouglas Christopher Wilsonbefore adoption7bc5f1a
line 111why, one step back111 var match4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
line 112why, one step back112lint: consolidate layer match failure pathDouglas Christopher Wilsonbefore adoption7bc5f1a
line 113why, one step back113 if (path != null) {perf: add fast match path for "*" routeDouglas Christopher Wilsonbefore adoption081b811
line 114why, one step back114 // fast path non-ending match for / (any path matches)lint: consolidate layer match failure pathDouglas Christopher Wilsonbefore adoption7bc5f1a
lines 115–117why, one step back115 if (this.regexp.fast_slash) {116 this.params = {}117 this.path = ''perf: add fast match path for "*" routeDouglas Christopher Wilsonbefore adoption081b811
lines 118–124why, one step back118 return true119 }120121 // fast path for * (everything matched in a param)122 if (this.regexp.fast_star) {123 this.params = {'0': decode_param(path)}124 this.path = pathlint: consolidate layer match failure pathDouglas Christopher Wilsonbefore adoption7bc5f1a
lines 125–129why, one step back125 return true126 }127128 // match the path129 match = this.regexp.exec(path)4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 130–131why, one step back130 }131lint: consolidate layer match failure pathDouglas Christopher Wilsonbefore adoption7bc5f1a
line 132why, one step back132 if (!match) {4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 133–139why, one step back133 this.params = undefined;134 this.path = undefined;135 return false;136 }137138 // store values139 this.params = {};lint: consolidate layer match failure pathDouglas Christopher Wilsonbefore adoption7bc5f1a
line 140why, one step back140 this.path = match[0]4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 141–144why, one step back141142 var keys = this.keys;143 var params = this.params;144lint: consolidate layer match failure pathDouglas Christopher Wilsonbefore adoption7bc5f1a
line 145why, one step back145 for (var i = 1; i < match.length; i++) {4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 146–147why, one step back146 var key = keys[i - 1];147 var prop = key.name;lint: consolidate layer match failure pathDouglas Christopher Wilsonbefore adoption7bc5f1a
line 148why, one step back148 var val = decode_param(match[i])4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 149–181why, one step back149150 if (val !== undefined || !(hasOwnProperty.call(params, prop))) {151 params[prop] = val;152 }153 }154155 return true;156};157158/**159 * Decode param value.160 *161 * @param {string} val162 * @return {string}163 * @private164 */165166function decode_param(val) {167 if (typeof val !== 'string' || val.length === 0) {168 return val;169 }170171 try {172 return decodeURIComponent(val);173 } catch (err) {174 if (err instanceof URIError) {175 err.message = 'Failed to decode param \'' + val + '\'';176 err.status = err.statusCode = 400;177 }178179 throw err;180 }181}