layer.js
This file
Path
lib/router/layer.js
67 lines · 1.2 KiB · JavaScript
Last changed by
Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768/** * Module dependencies. */
var pathRegexp = require('path-to-regexp');var debug = require('debug')('express:router:layer');
/** * Expose `Layer`. */
module.exports = Layer;
function Layer(path, options, fn) { if (!(this instanceof Layer)) { return new Layer(path, options, fn); }
debug('new %s', path); options = options || {}; this.regexp = pathRegexp(path, this.keys = [], options); this.handle = fn;}
/** * Check if this route matches `path`, if so * populate `.params`. * * @param {String} path * @return {Boolean} * @api private */
Layer.prototype.match = function(path){ var keys = this.keys; var params = this.params = {}; var m = this.regexp.exec(path); var n = 0; var key; var val;
if (!m) return false;
this.path = m[0];
for (var i = 1, len = m.length; i < len; ++i) { key = keys[i - 1];
try { val = 'string' == typeof m[i] ? decodeURIComponent(m[i]) : m[i]; } catch(e) { var err = new Error("Failed to decode param '" + m[i] + "'"); err.status = 400; throw err; }
if (key) { params[key.name] = val; } else { params[n++] = val; } }
return true;};