layer.js
This file
Path
lib/router/layer.js
80 lines · 1.3 KiB · JavaScript
Last changed by
Reduce try-catch de-op area in param matchingDouglas Christopher Wilsonbefore adoption10618ce
History of this fileTake it
Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
lines 1–3why, one step back1/**2 * Module dependencies.3 */lintJonathan Ongbefore adoption110f471
line 4why, one step back4use path-to-regexpJonathan Ongbefore adoption3321055
lines 5–6why, one step back5var pathRegexp = require('path-to-regexp');6var debug = require('debug')('express:router:layer');Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
line 7why, one step back7Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
lines 8–13why, one step back8/**9 * Expose `Layer`.10 */1112module.exports = Layer;13Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
lines 14–20why, one step back14function Layer(path, options, fn) {15 if (!(this instanceof Layer)) {16 return new Layer(path, options, fn);17 }1819 debug('new %s', path);20 options = options || {};use path-to-regexpJonathan Ongbefore adoption3321055
line 21why, one step back21 this.regexp = pathRegexp(path, this.keys = [], options);Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
lines 22–34why, one step back22 this.handle = fn;23}2425/**26 * Check if this route matches `path`, if so27 * populate `.params`.28 *29 * @param {String} path30 * @return {Boolean}31 * @api private32 */3334Layer.prototype.match = function(path){Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
lines 35–38why, one step back35 var keys = this.keys;36 var params = this.params = {};37 var m = this.regexp.exec(path);38 var n = 0;some jshint cleanupJonathan Ongbefore adoption5572897
lines 39–40why, one step back39 var key;40 var val;Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
lines 41–42why, one step back4142 if (!m) return false;Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1
lines 43–44why, one step back4344 this.path = m[0];Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
lines 45–46why, one step back4546 for (var i = 1, len = m.length; i < len; ++i) {some jshint cleanupJonathan Ongbefore adoption5572897
line 47why, one step back47 key = keys[i - 1];Reduce try-catch de-op area in param matchingDouglas Christopher Wilsonbefore adoption10618ce
line 48why, one step back48 val = decode_param(m[i]);Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
lines 49–58why, one step back4950 if (key) {51 params[key.name] = val;52 } else {53 params[n++] = val;54 }55 }5657 return true;58};Reduce try-catch de-op area in param matchingDouglas Christopher Wilsonbefore adoption10618ce
lines 59–80why, one step back5960/**61 * Decode param value.62 *63 * @param {string} val64 * @return {string}65 * @api private66 */6768function decode_param(val){69 if (typeof val !== 'string') {70 return val;71 }7273 try {74 return decodeURIComponent(val);75 } catch (e) {76 var err = new TypeError("Failed to decode param '" + val + "'");77 err.status = 400;78 throw err;79 }80}