Home > Software design >  In NodeJS, is res.sendStatus(status).send(message) Invalid?
In NodeJS, is res.sendStatus(status).send(message) Invalid?

Time:06-11

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)

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

CodePudding user response:

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.

  • Related