layer.js
This file
Path
lib/router/layer.js
170 lines · 3.0 KiB · JavaScript
Last changed by
deps: path-to-regexp@0.1.6Blake Embreybefore adoption60e2008
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: add license commentsDouglas Christopher Wilsonbefore adoption95ad276
lines 1–8why, 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 */8perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f
lines 9–10why, one step back9'use strict';10docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 11–18why, one step back11/**12 * Module dependencies.13 */1415var pathRegexp = require('path-to-regexp');16var debug = require('debug')('express:router:layer');1718/**Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2
lines 19–24why, one step back19 * Module variables.20 */2122var hasOwnProperty = Object.prototype.hasOwnProperty;2324/**docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 25–107why, one step back25 * Expose `Layer`.26 */2728module.exports = Layer;2930function Layer(path, options, fn) {31 if (!(this instanceof Layer)) {32 return new Layer(path, options, fn);33 }3435 debug('new %s', path);36 options = options || {};3738 this.handle = fn;39 this.name = fn.name || '<anonymous>';40 this.params = undefined;41 this.path = undefined;42 this.regexp = pathRegexp(path, this.keys = [], options);4344 if (path === '/' && options.end === false) {45 this.regexp.fast_slash = true;46 }47}4849/**50 * Handle the error for the layer.51 *52 * @param {Error} error53 * @param {Request} req54 * @param {Response} res55 * @param {function} next56 * @api private57 */5859Layer.prototype.handle_error = function handle_error(error, req, res, next) {60 var fn = this.handle;6162 if (fn.length !== 4) {63 // not a standard error handler64 return next(error);65 }6667 try {68 fn(error, req, res, next);69 } catch (err) {70 next(err);71 }72};7374/**75 * Handle the request for the layer.76 *77 * @param {Request} req78 * @param {Response} res79 * @param {function} next80 * @api private81 */8283Layer.prototype.handle_request = function handle(req, res, next) {84 var fn = this.handle;8586 if (fn.length > 3) {87 // not a standard request handler88 return next();89 }9091 try {92 fn(req, res, next);93 } catch (err) {94 next(err);95 }96};9798/**99 * Check if this route matches `path`, if so100 * populate `.params`.101 *102 * @param {String} path103 * @return {Boolean}104 * @api private105 */106107Layer.prototype.match = function match(path) {Fix handling of invalid empty URLsDouglas Christopher Wilsonbefore adoption68290ee
lines 108–114why, one step back108 if (path == null) {109 // no path, nothing matches110 this.params = undefined;111 this.path = undefined;112 return false;113 }114docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 115–136why, one step back115 if (this.regexp.fast_slash) {116 // fast path non-ending match for / (everything matches)117 this.params = {};118 this.path = '';119 return true;120 }121122 var m = this.regexp.exec(path);123124 if (!m) {125 this.params = undefined;126 this.path = undefined;127 return false;128 }129130 // store values131 this.params = {};132 this.path = m[0];133134 var keys = this.keys;135 var params = this.params;136deps: path-to-regexp@0.1.6Blake Embreybefore adoption60e2008
lines 137–140why, one step back137 for (var i = 1; i < m.length; i++) {138 var key = keys[i - 1];139 var prop = key.name;140 var val = decode_param(m[i]);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 141why, one step back141Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2
lines 142–143why, one step back142 if (val !== undefined || !(hasOwnProperty.call(params, prop))) {143 params[prop] = val;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 144–170why, one step back144 }145 }146147 return true;148};149150/**151 * Decode param value.152 *153 * @param {string} val154 * @return {string}155 * @api private156 */157158function decode_param(val){159 if (typeof val !== 'string') {160 return val;161 }162163 try {164 return decodeURIComponent(val);165 } catch (e) {166 var err = new TypeError("Failed to decode param '" + val + "'");167 err.status = 400;168 throw err;169 }170}