Inversifyjs: Inversify-Express-Utils Middleware for all controllers

Created on 17 Apr 2018  路  2Comments  路  Source: inversify/InversifyJS

Moved from https://github.com/inversify/inversify-express-example/issues/327

For example i wanna to make loggerMiddleware but connect this middleware to each controller is intolerable. Haw can i do it for all request??

question

Most helpful comment

@dcavanagh What if I want to rebind something in a global middleware?

All 2 comments

@Bon4ik inversify-express-utils is just express under the hood. If you want to register a global middle-ware you can do it in the config step

function myMiddleWare(request: express.Request, response: express.Response, next: express.NextFunction) {
    console.log(`HTTP ${request.method} ${request.url}`);
    next();
}

let server = new InversifyExpressServer(container);

server.setConfig((app) => {
  app.use(myMiddleWare);
  app.use(bodyParser.json());
});

let serverInstance = server.build();
serverInstance.listen(3000);

@dcavanagh What if I want to rebind something in a global middleware?

Was this page helpful?
0 / 5 - 0 ratings