Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

res.status() vs. res.statusCode

I would like to know if these two different approach is it identical in expressjs?

 res.statusCode = 500;
 return res.json({
  status: "error"
 });

or

return res.status(500).json({
  status: "error"
});
like image 646
Code Worm Avatar asked Jun 16 '18 13:06

Code Worm


People also ask

What does res Status () do?

The res. status() function set the HTTP status for the response. It is a chainable alias of Node's response.

What is the difference between Res send and RES status?

The res object basically refers to the response that'll be sent out as part of this API call. The res. send function sets the content type to text/Html which means that the client will now treat it as text. It then returns the response to the client.

What is the difference between RES end and Res send?

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

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.


1 Answers

expressjs - Response

the res object is an enhanced version of Node’s own response object and supports all built-in fields and methods.

res.status(code)

Sets the HTTP status for the response. It is a chainable alias of Node’s response.statusCode.

So the result is the same. expressjs just added a chainable version of statusCode.

like image 189
t.niese Avatar answered Nov 16 '22 00:11

t.niese