Express status getting and setting a http status code
In express status codes can be both get and set with properties and methods in an express response object. There is the res.statusCode property than can be used to find out the current http status code, and the res.status method that can be used to set that code. In addition there is the res.sendStatus method that can be used to just set a status code and end the request without sending any data in the same way as the express end response method. So this will be a post on http status codes in express, getting it, setting it and some status code use examples.
1 - Express status and just getting the current http status code.
To get the current http status code of the request object there is the re.statusCode property. By default the status code of a response is 200 with means everything is okay.
|
|
Some times there is something that might happen that would call for setting a status code other than that of 200 though. For example if the requested resource does not exist it is typical to send a 404 status code. For this there are methods like res.status, and res.sendStatus.
2 - Express status send and end
If a status code is it be sent but with no data body then there is the res.sendStatus response method that can be used. Another option would be to use the status method combined with the end method, or the send method with no arguments given.
|
|
3 - An Express Status example that involves a 500 (Server Error) status code
Here I put together a quick example that responds with a 500 Internal Server Error if any kind of error happens when attempting to get a resource, it can also potential default to a 400 error for any request that was not handled by any other additional middleware regardless if there is an error or not.
|
|
The example generates an index of files that are in the current working directory when the root path is visited. All other get requests result in the example attempting to read a file at the path relative to the current working directory. In the event that the file is not there, or an attempt is made to read a directory, an error occurs resulting in a 500 error status being sent.
A 500 status code is very general of course, it just means an internal or server side error has occurred. Same has with a 400 status code as well, it just means Bad Request and it is not more specific like a 404 (not found) or a 403 Forbidden. It is a good idea to stick to codes like 500, or 400 when it comes to making things more general. However in a real project it would be more professional to use more specific codes.
4 - Conclusion
In express status codes can be set with the res.status response method, and the current http status can always be found via the res.statusCode property. Of course there is much more to write about when it comes to the many different status codes, and writing much more complicated express middlewares and client systems. I wanted to at least start a post on this subject though, and hopefully I will get around to expanding on this at some point as well. In the mean time you might want to check out my main post on express for more content on expressjs related topics.