What is the difference between response.status()
and response.sendStatus()
in Express.
I notice that one is used generally for post, get, and other middleware, while the later is used in delete requests. Why is this?
json() method will then call res. send() as well under the hood. There is no actual difference between res. send and res.
The res. sendStatus() function is used to set the response HTTP status code to statusCode and send its string representation as the response body. Parameter: The statusCode parameter describes the HTTP status code.
res. send() is used to send the response to the client where res. end() is used to end the response you are sending.
To send the status code to the client-side, you can method chain using the . send() method. The status code 404 tells the client side that the data requested is not found.
status()
sets a HTTP status on the response (as a Javascript object on the server side).
sendStatus()
sets the status and sends it to the client.
The usage doesn't depend on the HTTP method of the request. In most cases you would use sendStatus
anyway since it's unlikely that the status changes once the request is processed (especially since status code is the first line in a raw HTTP response).
Read more in the docs:
https://expressjs.com/en/4x/api.html#res.sendStatus
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With