routing-controllers
routing-controllers copied to clipboard
fix: import statements should not affect the route matching order
Description
We found in our project that a different order of Controller imports produces different results for a catch-all 404 route which blocks controllers imported earlier in the file.
A canonical sorting of imports is in my opinion common practice and it is not intuitive that the order matters in this case. Shouldn't the matching preference depend only on factors such as longest path and order inside the initializers controllers
array?
Minimal code-snippet showcasing the problem
controllers/NotFoundController.ts
import { Controller, NotFoundError, All } from "routing-controllers";
@Controller()
export class NotFoundController {
@All("/*")
catchAll() {
throw new NotFoundError("This route does not exist");
}
}
controllers/ApplesController.ts
@JsonController("/apples")
export class ApplesController {
@Get("/")
async Apples(
return JSON.stringify(["Granny Smith", "Braeburn"])
)
}
app.ts
import { NotFoundController } from "./controllers/NotFoundController";
import { ApplesController } from "./controllers/ApplesController";
export const app = createExpressServer({
controllers: [
ApplesController,
NotFoundController,
]
});
Expected behavior
GET http://localhost:3000/apples
["Granny Smith", "Braeburn"]
Actual behavior
GET http://localhost:3000/apples
404 error