Skip to content

Commit a08a2b6

Browse files
ajfranzoiagabegorelick
authored andcommitted
Store matched routes in request
1 parent dfd856c commit a08a2b6

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ Router.prototype.handle = function handle(req, res, callback) {
292292
return next(layerError || err)
293293
}
294294

295+
if (layer.path) {
296+
req.matchedRoutes = req.matchedRoutes || []
297+
req.matchedRoutes.push(layer.matchedPath.path)
298+
}
299+
295300
if (route) {
296301
return layer.handle_request(req, res, next)
297302
}
@@ -347,7 +352,7 @@ Router.prototype.process_params = function process_params(layer, called, req, re
347352
var params = this.params
348353

349354
// captured parameters from the layer, keys and values
350-
var keys = layer.keys
355+
var keys = layer.matchedPath ? layer.matchedPath.keys : []
351356

352357
// fast track
353358
if (!keys || keys.length === 0) {

lib/layer.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,33 @@ var hasOwnProperty = Object.prototype.hasOwnProperty
2828

2929
module.exports = Layer
3030

31-
function Layer(path, options, fn) {
31+
function Layer(paths, options, fn) {
3232
if (!(this instanceof Layer)) {
33-
return new Layer(path, options, fn)
33+
return new Layer(paths, options, fn)
3434
}
3535

36-
debug('new %o', path)
36+
debug('new %o', paths)
3737
var opts = options || {}
3838

3939
this.handle = fn
4040
this.name = fn.name || '<anonymous>'
4141
this.params = undefined
4242
this.path = undefined
43-
this.regexp = pathRegexp(path, this.keys = [], opts)
4443

4544
// set fast path flags
46-
this.regexp.fast_star = path === '*'
47-
this.regexp.fast_slash = path === '/' && opts.end === false
45+
this.fastStar = paths === '*'
46+
this.fastSlash = paths === '/' && opts.end === false
47+
48+
this.paths = !Array.isArray(paths) ? [paths] : paths
49+
this.paths = this.paths.map(function (path) {
50+
var pathObj = {
51+
path: path,
52+
keys: []
53+
}
54+
pathObj.regexp = pathRegexp(path, pathObj.keys, opts)
55+
56+
return pathObj
57+
})
4858
}
4959

5060
/**
@@ -107,24 +117,33 @@ Layer.prototype.handle_request = function handle(req, res, next) {
107117

108118
Layer.prototype.match = function match(path) {
109119
var match
120+
var checkPath
110121

111122
if (path != null) {
112123
// fast path non-ending match for / (any path matches)
113-
if (this.regexp.fast_slash) {
124+
if (this.fastSlash) {
114125
this.params = {}
115126
this.path = ''
127+
this.matchedPath = this.paths[0]
116128
return true
117129
}
118130

119131
// fast path for * (everything matched in a param)
120-
if (this.regexp.fast_star) {
132+
if (this.fastStar) {
121133
this.params = {'0': decode_param(path)}
122134
this.path = path
135+
this.matchedPath = this.paths[0]
123136
return true
124137
}
125138

126139
// match the path
127-
match = this.regexp.exec(path)
140+
for (var i = 0; i < this.paths.length; i++) {
141+
checkPath = this.paths[i]
142+
if (match = checkPath.regexp.exec(path)) {
143+
this.matchedPath = checkPath
144+
break
145+
}
146+
}
128147
}
129148

130149
if (!match) {
@@ -142,7 +161,7 @@ Layer.prototype.match = function match(path) {
142161
var params = this.params
143162

144163
for (var i = 1; i < match.length; i++) {
145-
var key = keys[i - 1]
164+
var key = this.matchedPath.keys[i - 1]
146165
var prop = key.name
147166
var val = decode_param(match[i])
148167

test/router.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,31 @@ describe('Router', function () {
11291129
.expect(200, 'saw GET /bar', done)
11301130
})
11311131
})
1132+
1133+
describe('req.matchedRoutes', function () {
1134+
it('should store matchedRoutes in request', function (done) {
1135+
var router = new Router()
1136+
var barRouter = new Router()
1137+
var bazRouter = new Router()
1138+
var server = createServer(router)
1139+
var matchedRoutes
1140+
1141+
router.use(['/foo/:id', '/foe'], barRouter)
1142+
barRouter.use(['/bar'], bazRouter)
1143+
bazRouter.get(['/bez', '/baz/:subId'], function (req, res, next) {
1144+
matchedRoutes = req.matchedRoutes
1145+
next()
1146+
})
1147+
router.use(saw)
1148+
1149+
request(server)
1150+
.get('/foo/10/bar/baz/30')
1151+
.expect(200, 'saw GET /foo/10/bar/baz/30', function (err, res) {
1152+
assert.deepEqual(matchedRoutes, ['/foo/:id', '/bar', '/baz/:subId'])
1153+
done(err)
1154+
})
1155+
})
1156+
})
11321157
})
11331158

11341159
function helloWorld(req, res) {

0 commit comments

Comments
 (0)