res.status() vs. res.statusCode

Code Worm picture Code Worm · Jun 16, 2018 · Viewed 16k times · Source

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"
});

Answer

t.niese picture t.niese · Jun 16, 2018

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.