Technically you aren’t pressured to make use of status codes nonetheless it’s really useful to comply with one of the best practices.
When the consumer doesn’t exist return 404
not 401
. 401 is unauthorized
When consumer enter just isn’t anticipated, that’s validation error(unhealthy request) and return 400
as a substitute of 422
. 422 is used in barely completely different eventualities.
Read extra about it 400 vs 422
More particulars about http status codes
Yes, status codes are essential as a good follow I would favor 404 as a substitute of 401 in your case res.status(404).ship("This username does not exist.");
stackOverflowAnswer
Why can we use the status code?
To make your debug life simple/ higher error dealing with and to log the error in manufacturing to know the severity of the error your utility has in case it crashes.
How to Specify statusCode in Node.js
When to use what status code
By default, Express reply all endpoints with 200 until you didn’t specified an endpoint, in this case it is going to mechanically reply with 404.
by the best way, Express additionally has res.shipStatus()
perform that ends the request and sending status
This has to do with your api design. Generally you’ll be publishing your api specs (Api specification) and there would point out how your consumer can discover out if one thing goes incorrect or going nice.
HTTP Response code are a few of best approach to inform consumer about consequence of request. So they don’t need to go contained in the payload of response to examine what was consequence. Since most of codes are effectively know and there’s consensus you’ll write extra customary code which works with community components like proxies, load-balancer and so on and comprehensible builders.
Advantages of status codes