Skip to content
Myr

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 file

Take it

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

lines 1–3why, one step back
1/**
2 * Module dependencies.3 */

lintJonathan Ongbefore adoption110f471

line 4why, one step back
4

use path-to-regexpJonathan Ongbefore adoption3321055

lines 5–6why, one step back
5var 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 back
7

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

lines 8–13why, one step back
8/**
9 * Expose `Layer`.10 */1112module.exports = Layer;13

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

lines 14–20why, one step back
14function 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 back
21 this.regexp = pathRegexp(path, this.keys = [], options);

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

lines 22–34why, one step back
22 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 back
35 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 back
39 var key;
40 var val;

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

lines 41–42why, one step back
41
42 if (!m) return false;

Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1

lines 43–44why, one step back
43
44 this.path = m[0];

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

lines 45–46why, one step back
45
46 for (var i = 1, len = m.length; i < len; ++i) {

some jshint cleanupJonathan Ongbefore adoption5572897

line 47why, one step back
47 key = keys[i - 1];

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

lines 48–49why, one step back
48
49 try {

some jshint cleanupJonathan Ongbefore adoption5572897

line 50why, one step back
50 val = 'string' == typeof m[i]

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

lines 51–67why, one step back
51 ? decodeURIComponent(m[i])
52 : m[i];53 } catch(e) {54 var err = new Error("Failed to decode param '" + m[i] + "'");55 err.status = 400;56 throw err;57 }5859 if (key) {60 params[key.name] = val;61 } else {62 params[n++] = val;63 }64 }6566 return true;67};