Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client #491

Closed
florin-nedelcu opened this issue Jun 10, 2019 · 7 comments · Fixed by #568 or #536
Closed
Assignees
Labels
status: done/released Issue has been completed, no further action is needed. type: fix Issues describing a broken feature.
Milestone

Comments

@florin-nedelcu
Copy link

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!

@developersuresh
Copy link

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);
}

}

@florin-nedelcu
Copy link
Author

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.

@gruckion
Copy link

gruckion commented Jul 26, 2019

id is a number so you can use regex to be stricter on the matching.

@Get("/users/:id([0-9]+)")

@doghappy
Copy link

doghappy commented Nov 20, 2019

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"
//    });
//});

@dornfeder
Copy link

dornfeder commented Jan 10, 2020

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 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...

@jotamorais jotamorais self-assigned this May 29, 2020
@jotamorais jotamorais added this to the 0.9.x release milestone May 29, 2020
@NoNameProvided NoNameProvided changed the title Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client fix: [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Aug 9, 2020
@NoNameProvided NoNameProvided added status: done/released Issue has been completed, no further action is needed. type: fix Issues describing a broken feature. labels Aug 9, 2020
@NoNameProvided
Copy link
Member

This has been fixed.

@typestack typestack locked as resolved and limited conversation to collaborators Aug 9, 2020
@NoNameProvided
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: done/released Issue has been completed, no further action is needed. type: fix Issues describing a broken feature.
Development

Successfully merging a pull request may close this issue.

7 participants