How to use the Node.js http.ServerResponse.statusCode property

HTTP response status codes are used in web development to determine whether a particular HTTP request has been completed successfully.

This article will talk about httpServerResponse.statusCode and how to use it in modern Node.js.

What is httpServerResponse.statusCode?

This property controls the status code that will be delivered to the client when the headers are flushed.

The httpServerResponse.statusCode is an internal application programming interface of class ServerResponse within the HTTP module.

Types of status codes

Status codes are a set of universal web development codes utilized to create apps all over the world.

  • Informational responses (100–199)
  • Successful responses (200–299)
  • Redirects (300–399)
  • Client errors (400–499)
  • Server errors (500–599)

How to use httpServerResponse.statusCode

In our discussion, we’ll see some instances of how to use some of these status codes.

1. 200 OK

The HTTP 200 OK response code implies a successful request.

Here is an example of this kind of HTTP response.

  • GET: The response contains an entity that corresponds to the resource requested.
  • POST: An entity that describes or contains the action’s result.
  • HEAD: In the response, the entity-header fields related to the requested resource are sent without any message-body.

Example:

app.get('/', function (req, res) {
    res.status(200).send('Status: OK')
});

2. 201 Created

This indicates that the request was completed, and a new resource was created as a result. The URI(s) sent in the response entity can refer to the newly generated help, with a Location header field providing the most particular URI for the resource.

app.get('/', function (req, res) {
    res.status(201).send('Status: Created')
});

3. 204 No Content

This response is sent when the server has completed the request and does not need to return an entity-body, but wants updated metainformation. The answer may contain additional or updated metadata in entity-headers, which should be associated with the requested variant if present.

app.get('/', function (req, res) {
    res.status(204).send('Status: No Content')
});

4. 400 Bad Request

When the server is unable to understand the request due to incorrect syntax, this response is returned.

app.get('/', function (req, res) {
    res.status(400).send('Status: Bad Request')
});

5. 401 Unauthorized

Even though the HTTP standard specifies “unauthorized,” this response indicates “unauthenticated” logically. To get the requested response, the client must first authenticate itself.

This response code can also be used to ensure the user has the proper access to the resource.

app.get('/', function (req, res) {
    res.status(401).send('Status: Unauthorized')
});

6. 403 Forbidden

This response is gotten when the server refuses to provide the requested resource because the client does not have access permissions to the material; it is unauthorized. Unlike 401, the server is aware of the client’s identity.

app.get('/', function (req, res) {
    res.status(403).send('Status: Forbidden')
});

7. 404 Not Found

This means the requested resource is unavailable on the server, or maybe the URL has been mistyped in the browser. This can also indicate that the endpoint is correct, but the resource does not exist in an API. This response may be used instead of 403 to conceal the existence of a resource from an unauthorized client.

app.get('/', function (req, res) {
    res.status(404).send('Status: Not Found')
});

8. 500 Internal Server Error

The server ran into an unanticipated problem that stopped it from completing the request.

app.get('/', function (req, res) {
    res.status(500).send('Status: Internal Server Error')
});

9. 503 Service Not Available

This happens due to a sudden overload or server maintenance and it means the server cannot handle the request.

app.get('/', function (req, res) {
    res.status(503).send('Status: Service Not Available')
});

10. 504 Gateway Timeout

When the server is operating as a gateway and cannot promptly respond, this error response is returned.

app.get('/', function (req, res) {
    res.status(504).send('Status: GateWay Timeout')
});

There are many different status codes for HTTP responses, and we’ve gone through a few of them here. For more, see the resources section of this page.

Conclusion

In this article, we learned what status codes are and how to apply them in our applications.

Resources