sjmeverett / express-decorators

ES2015 decorators for express
45 stars 9 forks source link

Multiple middleware needs to be specified in an unconventional order #2

Closed Cinamonas closed 8 years ago

Cinamonas commented 8 years ago

Given the following code:

@web.get('/')
@web.middleware(a)
@web.middleware(b)
@web.middleware(c)
indexAction(req, res, next) { /* <…> */ }

Middlewares will be executed in such order: c → b → a. Which is not what you'd expect.

As a quick workaround, I changed the order in which I attach middlewares, but I guess it's something that could be addressed at library level.

sjmeverett commented 8 years ago

Thanks for this.

I'm reluctant to change this however for various reasons. First up is backwards compatibility - I don't want to break it for existing users. Secondly, the order depends on your transpiler; the spec hasn't completely congealed on this, and for example, babel recently changed the order it does it in.

Lastly, although this is totally up to interpretation, it's not necessarily backwards if you think about it as successively wrapping the handler in function calls, kind of like get('/', a(b(c(fn)))). In this case it would execute inside out.