Open In App

Can you console all HTTP status code in Node.js ?

Last Updated : 27 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

NodeJS is a runtime environment built to run Javascript outside the browser. It is widely used to build servers at the backend, which interact with the frontend such as websites, mobile applications, and many more. In this article, We will check whether we can console all HTTP status codes in NodeJS.

To understand this problem, we first need to understand, what is meant by HTTP Status codes.

HTTP Status Codes: Here, we are talking about the response status codes. These codes tell us whether an HTTP request has been successfully completed. Let us take a look at the five classes of the codes –

  1. Informational Responses: Codes 100 to 199
  2. Successful Responses: Codes 200 to 299
  3. Redirection Messages: Codes 300 to 399
  4. Client Error Responses: Codes 400 to 499
  5. Server Error Responses: Codes 500 to 599

The simplest example for the above would be the status code 404, which indicates – ‘Page Not Found. This is a very common error response, and most of us must have encountered it.

Now, coming back to our question, can we console all the HTTP status codes above, in NodeJS? The answer is NO. NodeJS does not provide the utility to console all the above status codes. But still, we have plenty of status codes, which we can console. Let us take a look at them.

Open your command prompt or shell, and type in the following command:

node

You should see the following output, indicating that you are inside the node environment now.

Welcome to Node.js v16.14.0.
Type ".help" for more information.
>

Now, we type the command:

http.STATUS_CODES

Console output:

{
  '100': 'Continue',
  '101': 'Switching Protocols',
  '102': 'Processing',
  '103': 'Early Hints',
  '200': 'OK',
  '201': 'Created',
  '202': 'Accepted',
  '203': 'Non-Authoritative Information',
  '204': 'No Content',
  ...
  '505': 'HTTP Version Not Supported',
  '506': 'Variant Also Negotiates',
  '507': 'Insufficient Storage',
  '508': 'Loop Detected',
  '509': 'Bandwidth Limit Exceeded',
  '510': 'Not Extended',
  '511': 'Network Authentication Required'
}

As you can see, we don’t have the list of all, but still a lot of HTTP status codes. Let’s understand the use-case of the 200 status code using an example.

Project Structure:

 

We will be needing express for this tutorial. So, in the same directory, open a command prompt and say –

npm install express

In the below example, we are running our server on port 5000, and handling a get request using express.js. If the request is successfully completed the status code will be sent to the client end as well as print the response status at the end server end.

index.js




const express = require('express');
const app = express();
const port = 5000;
  
app.get('/', (req, res) => {
    res.status(200).send('Status code of 200!');
    console.log(res.status(200));
})
  
app.listen(port, () => {
    console.log(`Server is running at the port: ${port}`);
})


Now, open your command prompt and say –

node index.js

The server is Running:

 

Now, open your browser, and type in the URL: http://localhost:5000/. Your browser will display a webpage saying, ‘Status code of 200!’. 

 

But that’s not all. Take a look at your command prompt now. You will see a lot of stuff. Don’t worry, just scroll down to the end of the output, and you will see something like this –

 

As you can see, we have successfully printed the status code along with its message in our console. Similarly, you can try with different status codes in the list, we consoled earlier.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads