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"
});
The res. status() function set the HTTP status for the response. It is a chainable alias of Node's response.
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.
res. send() is used to send the response to the client where res. end() is used to end the response you are sending.
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.
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
.
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