Can you console all HTTP status code in Node.js ?
Last Updated :
27 Sep, 2022
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 -
- Informational Responses: Codes 100 to 199
- Successful Responses: Codes 200 to 299
- Redirection Messages: Codes 300 to 399
- Client Error Responses: Codes 400 to 499
- 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.
Similar Reads
JSP - HTTP Status Codes When the Client makes any requests to the server, the Status Codes are issued by the server as a response to the client's request. So, in an application, we have the client and the server architecture. The server is the part that holds the particular web service or an API. The client is the actor wh
4 min read
How To Create a Simple HTTP Server in Node? NodeJS is a powerful runtime environment that allows developers to build scalable and high-performance applications, especially for I/O-bound operations. One of the most common uses of NodeJS is to create HTTP servers. What is HTTP?HTTP (Hypertext Transfer Protocol) is a protocol used for transferri
3 min read
Servlet - HTTP Status Codes For each HTTP request and HTTP response, we have messages. The format of the HTTP request and HTTP response messages are similar and will have the following structure â An initial status line + CRLFCRLF = Â ( Carriage Return + Line Feed i.e. New Line )Zero or more header lines + CRLFA blank line, i.e
4 min read
What are HTTP Status Codes ? HTTP Protocol is used everywhere from the server page to each service communication, deploying service to monitoring service. HTTP codes give an extension of HTTP protocol to debug network problems using curl, telnet able to check server availability, service responses HTTP status code is used for s
8 min read
Which HTTP response status codes result in then() and which in catch() ? Axios is a popular JavaScript library for making HTTP requests. It uses promises to handle the response from the server, and provides a convenient then() and catch() syntax for working with the data or handling errors. Have you ever wondered which HTTP response status codes will cause axios to call
4 min read