Description
Description
I'm integrating routing controllers in an existing application to have a better controller structure.
I have lot of controllers, so I don't want to migrate all of them for now.
You fixed an issue where two controllers in routing-controllers were called here: #220
But it is still calling express controller after a annotated controller from routing-controllers:
@Get('/api/users')
getAll()
{
return userReporitory.findAll();
}
app.get('/**', async (_, res) => {
res.render('page.ejs'); // render main app html page for html5 navigation
});
Both are called. It was not the case before, and it works when I put the second controller in a routing-controller class.
I found this workaround:
useExpressServer(app, {...});
// If an api endpoint has already been called, stop routing chain
app.all('/**', (req, res, next) => {
if (!res.headersSent) {
next();
}
});
// other express routers
app.get('/**', async (_, res) => {
res.render('page.ejs'); // render main app html page for html5 navigation
});
Proposed solution
When I started integrating this library, I found we can do either app = createExpressServer
and useExpressServer(app)
.
But I expected we can do something like app.use(createExpressRouter({ controllers: [...] }))
.
So that we can gradually migrate routers one by one:
app.use(staticsRouter());
// Inside myApiRouter(), I can use either routing-controller, like return createExpressRouter(...)
// or normal express like const router = Router(); ... ; return router;
app.use(myApiRouter());
app.use(pwaRouter());
app.use(seoRouter());
app.use(pagesRouter());
And this way we have an independant router than can be used among other express routers.