routing-controllers
routing-controllers copied to clipboard
question: Can I use DI in an per-action Middleware?
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?