Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store matched routes in request #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ Router.prototype.handle = function handle(req, res, callback) {
return next(layerError || err)
}

// store matched routes
if (layer.path) {
req.matchedRoutes = (req.matchedRoutes || []).concat(layer.matchedPath.path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to create unnecessary arrays. Can you instead only create a new array the first time, then push to it after?

req. matchedRoutes = req.matchedRoutes || []
req.matchedRoutes.push(layer.matchedPath.path)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

if (route) {
return layer.handle_request(req, res, next)
}
Expand Down Expand Up @@ -341,7 +346,7 @@ Router.prototype.process_params = function process_params(layer, called, req, re
var params = this.params

// captured parameters from the layer, keys and values
var keys = layer.keys
var keys = layer.matchedPath ? layer.matchedPath.keys : []

// fast track
if (!keys || keys.length === 0) {
Expand Down
36 changes: 27 additions & 9 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,32 @@ var hasOwnProperty = Object.prototype.hasOwnProperty

module.exports = Layer

function Layer(path, options, fn) {
function Layer(paths, options, fn) {
if (!(this instanceof Layer)) {
return new Layer(path, options, fn)
return new Layer(paths, options, fn)
}

debug('new %s', path)
debug('new %s', paths)
var opts = options || {}

this.handle = fn
this.name = fn.name || '<anonymous>'
this.params = undefined
this.path = undefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a line for this.matchedPath = undefined, see comment below for reasoning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.regexp = pathRegexp(path, this.keys = [], opts)

if (path === '/' && opts.end === false) {
this.regexp.fast_slash = true
if (paths === '/' && opts.end === false) {
this.fastSlash = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V8 can do optimizations if the shape of an object remains the same. This addition will have only some Layer's with a fastSlash property. To optimize this, you should define it as undefined above, or false here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

this.paths = !Array.isArray(paths) ? [paths] : paths
this.paths = this.paths.map(function (path) {
var pathObj = {
path: path,
keys: []
}
pathObj.regexp = pathRegexp(path, pathObj.keys, opts)
return pathObj
})
}

/**
Expand Down Expand Up @@ -113,14 +122,23 @@ Layer.prototype.match = function match(path) {
return false
}

if (this.regexp.fast_slash) {
if (this.fastSlash) {
// fast path non-ending match for / (everything matches)
this.params = {}
this.path = ''
this.matchedPath = this.paths[0]
return true
}

var m = this.regexp.exec(path)
var checkPath, m;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the express modules are working toward compliance with standard. Which includes the rule that variable declarations are each on a single line. We have doing it piecemeal because it would cause too many conflicts otherwise, so since this is a new line, can you make the change just here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


for (var i = 0; i < this.paths.length; i++) {
checkPath = this.paths[i]
if (m = checkPath.regexp.exec(path)) {
this.matchedPath = checkPath
break
}
}

if (!m) {
this.params = undefined
Expand All @@ -137,7 +155,7 @@ Layer.prototype.match = function match(path) {
var params = this.params

for (var i = 1; i < m.length; i++) {
var key = keys[i - 1]
var key = this.matchedPath.keys[i - 1]
var prop = key.name
var val = decode_param(m[i])

Expand Down
25 changes: 25 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,31 @@ describe('Router', function () {
})
})

describe('req.matchedRoutes', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as comment above, can you make the formatting through here function () {? As per standard.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it('should store matchedRoutes in request', function(done) {
var router = new Router()
var barRouter = new Router()
var bazRouter = new Router()
var server = createServer(router)
var matchedRoutes

router.use(['/foo/:id', '/foe'], barRouter)
barRouter.use(['/bar'], bazRouter)
bazRouter.get(['/bez', '/baz/:subId'], function(req, res, next) {
matchedRoutes = req.matchedRoutes
next()
})
router.use(saw)

request(server)
.get('/foo/10/bar/baz/30')
.expect(200, 'saw GET /foo/10/bar/baz/30', function(err, res) {
assert.deepEqual(matchedRoutes, ['/foo/:id', '/bar', '/baz/:subId'])
done(err)
})
})
})

function helloWorld(req, res) {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
Expand Down