You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How do we influence the router ordering such that the API controller serves API requests, and the MainController serves all others for HTML5 routing support?
The controllers are all aggregated and registered internally by inversify, so as far as I can tell, there is no defined way to influence the order in which the routes are registered which is potentially important. A similar issue exists within methods where I want one function to handle something like a / route that maybe redirects to /someDefaultId, but have the * route exist to handle everything else. The ordering of which routes are registered on a method by method basis isn't defined in the inversify API, so I'm not sure how to influence that either.
Thanks!
The text was updated successfully, but these errors were encountered:
Hi @dcherman thanks for asking. The problem is that you cannot use "" as a child of "/". The routes defined by @controller('/') are joined to the routes defined by methods like @httpGet('*') so it was trying "/". There are two solutions:
A) Using inversify-express-utils
I'm not sure but I think the problem is that you cannot define the "*" as an action you can try the following:
@controller("*")classNotFoundController{
@httpGet("/")publicasyncindex(){returnPromise.resolve("not found!");}}
@controller("/")classHomeControllee{
@httpGet("/")publicindex(){returnPromise.resolve("home!");}}constcontainer=newContainer();constserver=newInversifyExpressServer(container);constapp=server.build();app.listen(3000,()=>{console.log("Listening on port 3000...");});
b) Using express.js
constserver=newInversifyExpressServer(container);// Info: https://github.com/inversify/inversify-express-utils/blob/752a68310e3ea515c0c47a757413fe38ee1f00f4/src/server.ts#L71-L82server.setErrorConfig((app)=>{// Catch and log all exceptionsapp.use(exceptionLoggerMiddleware);});// Info: https://github.com/inversify/inversify-express-utils/blob/752a68310e3ea515c0c47a757413fe38ee1f00f4/src/server.ts#L58-L69server.setConfig((app)=>{app.use(helmet());});// Loads constrollers and initializes routingconstapp=server.build();// The route for /app.get("/",(req: express.Request,res: express.Response)=>{res.redirect(homePath);});// The last route should be a global handler for 404 errorsapp.get("*",(req: express.Request,res: express.Response)=>{res.redirect(notFoundPath);});
This should solve your problem so I'm closing the issue. Please feel free to ask again if you need additional help.
Given two controllers:
How do we influence the router ordering such that the API controller serves API requests, and the MainController serves all others for HTML5 routing support?
The controllers are all aggregated and registered internally by inversify, so as far as I can tell, there is no defined way to influence the order in which the routes are registered which is potentially important. A similar issue exists within methods where I want one function to handle something like a
/
route that maybe redirects to/someDefaultId
, but have the*
route exist to handle everything else. The ordering of which routes are registered on a method by method basis isn't defined in the inversify API, so I'm not sure how to influence that either.Thanks!
The text was updated successfully, but these errors were encountered: