Skip to content
Myr

layer.js

This file

Path

lib/router/layer.js

61 lines · 1.2 KiB · JavaScript

Last changed by

Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5

History of this file

Take it

Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5

lines 1–61why, one step back
1var utils = require('../utils')
2 , debug = require('debug')('express:router:layer')34function Layer(path, options, fn) {5 if (!(this instanceof Layer)) {6 return new Layer(path, options, fn);7 }89 debug('new %s', path);10 options = options || {};11 this.path = path;12 this.params = {};13 this.regexp = utils.pathRegexp(path14 , this.keys = []15 , options.sensitive16 , options.strict17 , options.end);18 this.handle = fn;19}2021/**22 * Check if this route matches `path`, if so23 * populate `.params`.24 *25 * @param {String} path26 * @return {Boolean}27 * @api private28 */2930Layer.prototype.match = function(path){31 var keys = this.keys32 , params = this.params = {}33 , m = this.regexp.exec(path)34 , n = 0;3536 if (!m) return false;3738 for (var i = 1, len = m.length; i < len; ++i) {39 var key = keys[i - 1];4041 try {42 var val = 'string' == typeof m[i]43 ? decodeURIComponent(m[i])44 : m[i];45 } catch(e) {46 var err = new Error("Failed to decode param '" + m[i] + "'");47 err.status = 400;48 throw err;49 }5051 if (key) {52 params[key.name] = val;53 } else {54 params[n++] = val;55 }56 }5758 return true;59};6061module.exports = Layer;