Skip to content

Commit

Permalink
Use router middleware instead of global middleware when we can find m…
Browse files Browse the repository at this point in the history
…atching route.
  • Loading branch information
lsm committed Nov 26, 2015
1 parent e095585 commit d7166ee
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/web/framework/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,33 @@ ExpressAdapter.prototype.useMiddleware = function(url, middleware, service) {
url = [url]
}

var app = this.getApp()
var route = service.router.getRoutes()
var _middleware = middleware(app)

// make sure we mount middleware under the correct base url
url.forEach(function(link, idx) {
if ('^' !== link[0]) {
url[idx] = path.join(service.baseUrl, link)
url.forEach(function(link) {
var method = link.method
var routeMounted = false

if ('default' === method && route) {
Object.keys(route).forEach(function(routeName) {
var _route = route[routeName]
// It's a router based middleware if we have exactly same url defined in route
if (_route.path === link.path) {
debug('[%s] attache router middleware directly at path "%s"', service.name, link.path)
app[_route.method](link.path, _middleware)
routeMounted = true
}
})
}
})

debug('[%s] use middleware directly at url "%s"', service.name, url)

var app = this.getApp()
app.use(url, middleware(app))
if (false === routeMounted) {
method = method === 'default' ? 'use' : method
debug('[%s] use middleware directly at path "%s" with method "%s"', service.name, link.path, method)
app[method](link.path, _middleware)
}
})
}

/**
Expand Down

0 comments on commit d7166ee

Please sign in to comment.