sjmeverett/express-decorators

How to add middleware for all controller actions?

Closed this issue · 2 comments

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.

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 :)

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?