Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between response.status() vs. response.sendStatus() in express

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?

like image 925
retrieveTheTruth Avatar asked Jul 27 '16 18:07

retrieveTheTruth


People also ask

What is difference between Res send and RES status send?

json() method will then call res. send() as well under the hood. There is no actual difference between res. send and res.

Does Res status Send response?

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.

What is difference between Res send and res end?

res. send() is used to send the response to the client where res. end() is used to end the response you are sending.

How do I send a status code in response Express?

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.


1 Answers

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

like image 104
freakish Avatar answered Sep 21 '22 21:09

freakish