Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In NodeJS, is res.sendStatus(status).send(message) Invalid?

Whenever I return my HTTP response with : res.sendStatus(status).send(message)

I get this error:

Exception from a finished function: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I couldn’t understand where I was sending a response twice, but the error stopped when I removed .send(message)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So is this not a valid response? I’ve resorted to logged the messages instead of using .send()

>Solution :

status() sets a HTTP status on the response (as a Javascript object on the server side).

sendStatus() sets the status and also sends it to the client which is similar to res.status(200).send('OK')

You are trying to send the response twice, thus giving this error.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading