Skip to content

Commit

Permalink
Fixed regression
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Mar 19, 2018
1 parent d426aaf commit 0e76e75
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -162,7 +162,7 @@ Router.prototype._insert = function _insert (method, path, kind, params, handler
prefix.slice(len),
currentNode.children,
currentNode.kind,
new Node.Handlers(currentNode.handlers),
Node.buildHandlers(currentNode.handlers),
currentNode.regex
)
if (currentNode.wildcardChild !== null) {
Expand Down
16 changes: 9 additions & 7 deletions node.js
Expand Up @@ -18,7 +18,7 @@ function Node (prefix, children, kind, handlers, regex) {
this.children = children || {}
this.numberOfChildren = Object.keys(this.children).length
this.kind = kind || this.types.STATIC
this.handlers = handlers || new Handlers()
this.handlers = buildHandlers(handlers)
this.regex = regex || null
this.wildcardChild = null
this.parametricBrother = null
Expand Down Expand Up @@ -84,7 +84,7 @@ Node.prototype.reset = function (prefix) {
this.prefix = prefix
this.children = {}
this.kind = this.types.STATIC
this.handlers = new Handlers()
this.handlers = buildHandlers()
this.numberOfChildren = 0
this.regex = null
this.wildcardChild = null
Expand Down Expand Up @@ -168,14 +168,16 @@ Node.prototype.prettyPrint = function (prefix, tail) {
return tree
}

function Handlers (handlers) {
handlers = handlers || {}

function buildHandlers (handlers) {
var code = ''
for (var i in http.METHODS) {
var m = http.METHODS[i]
this[m] = handlers[m] || null
code += `this['${m}'] = handlers['${m}'] || null
`
}
const Fn = new Function('handlers', code) // eslint-disable-line
return new Fn(handlers || {})
}

module.exports = Node
module.exports.Handlers = Handlers
module.exports.buildHandlers = buildHandlers

0 comments on commit 0e76e75

Please sign in to comment.