Hi all.
When I have the following case, routing controllers fails and try to execute both functions:
@JsonController()
export class UserController {
@Get("/users/me")
getMe() {
const id = 1;
return userRepository.findById(id);
}
@Get("/users/:id")
getOne(@Param("id") id: number) {
return userRepository.findById(id);
}
}
As far as I read it should execute first occurency not both.
Thanks!
your both route should not same . it should be like this
@JsonController()
export class UserController {
@Get("/users/me")
getMe() {
const id = 1;
return userRepository.findById(id);
}
@Get("/users/detail/:id")
getOne(@Param("id") id: number) {
return userRepository.findById(id);
}
}
your both route should not same . it should be like this
@JsonController()
export class UserController {@Get("/users/me") getMe() { const id = 1; return userRepository.findById(id); } @Get("/users/detail/:id") getOne(@Param("id") id: number) { return userRepository.findById(id); }}
Well, I know I can do this way. But this doesn't solve the issue. It should not execute both handlers. Now it does a conflict between routes.
id is a number so you can use regex to be stricter on the matching.
@Get("/users/:id([0-9]+)")
I have the same error. I use routing-controller in an existing express program. when I remove a middleware, it won't throw again.
// catch 404 and forward to error handler
// app.use(function (req: Request, res: Response, next: NextFunction) {
// res.status(404);
// res.send({
// code: 404,
// message: "Not Found"
// });
//});
I have the same error. I use
routing-controllerin an existing express program. when I remove a middleware, it won't throw again.// catch 404 and forward to error handler // app.use(function (req: Request, res: Response, next: NextFunction) { // res.status(404); // res.send({ // code: 404, // message: "Not Found" // }); //});
I had the same issue. This was my workaround in the end so that i could keep the custom 404 middleware:
app.use(function (req: Request, res: Response, next: NextFunction) {
if (!res.finished) {
res.status(404).json(
{
status: 404,
message: `Cannot ${req.method} ${req.url}`,
}
);
}
res.end();
});
It basically checks if headers were already sent ond only if not sends the 404 infos.
But I'm still curious about the PROPER way to do this. The official way of doing things is the one that @doghappy mentioned, see http://expressjs.com/en/starter/faq.html but it does not work, at least in the context with routing-controllers...
This has been fixed.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
I have the same error. I use
routing-controllerin an existing express program. when I remove a middleware, it won't throw again.