sjmeverett / express-decorators

ES2015 decorators for express
45 stars 9 forks source link

How to add middleware for all controller actions? #5

Closed breezewish closed 7 years ago

breezewish commented 8 years ago

We have a middleware to be shared among multiple controllers. We also want the middleware being called in every action.

@web.use requires a member function as the middleware, which is not suitable. @web.middleware assigns middleware to a single action, which is not suitable as well.

bali182 commented 8 years ago

Just stumbled upon this library, and checked out the issues. You could delegate to the middleware or make it a field:

@controller('/foo')
class MyController {

  @use
  willUseThis(request, response, next) {
     yourMiddleWareFn(request, response, next)
  }

  @use alsoWillUseThis = yourMiddleWareFn
}

Didn't test it but you could give it a go :)

sjmeverett commented 7 years ago

Hi @breeswish, sorry I missed this.

You can use 'global' middleware as normal in express. E.g., for the previous version of this library:

let app = express();
app.use(someSortOfMiddleware); // <-- this line
let controller = new MyController();
controller.register(app);

or for the most recent version:

let app = new express();
app.use(someSortOfMiddleware); // <-- this line
let controller = new MyController();
web.register(app, controller);

That answer your question?