routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

question: Can I use DI in an per-action Middleware?

Open Liioooo opened this issue 3 years ago • 0 comments

I would like to use DI in a per-action middleware like in global middleware like this.

export class GoogleLoginMiddleware implements ExpressMiddlewareInterface {

  constructor(private translationService: TranslationService) {}

  use(request: Request, response: Response, next: (err?: any) => any): any {
    // do something with this service
    console.log(this.translationService.availableLanguages);
    next();
  }
}

But the problem is that I don't get the Service but the ContainerInstance.

What I did to work around this is this:

export class GoogleLoginMiddleware implements ExpressMiddlewareInterface {

  private translationService = Container.get(TranslationService);

  use(request: Request, response: Response, next: (err?: any) => any): any {
    // do something with this service
    console.log(this.translationService.availableLanguages);
    next();
  }
}

Did I miss something or is this just not possible to do?

Liioooo avatar Dec 29 '20 16:12 Liioooo