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
How to change Node console font color ?
In NodeJS, you can change the console font color to improve readability and enhance user experience, especially for debugging or logging purposes. Although NodeJS does not provide built-in methods for modifying font colors, developers can achieve this using ANSI escape codes or external libraries li
4 min read
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
State the core components of an HTTP response ?
Have you ever thought about how the front-end of an application communicates with the backend to get data or perform certain operations? It is done through API Requests. API stands for Application Programming Interface. The communication between our client and the API is achieved using HTTP Request
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
HTTP status codes | Redirection Responses
HTTP status codes are a conversation between your browser and the site server. The server gives responses to the browser's request in the form of a three-digit code known as HTTP status codes. The HTTP status codes are categorized into five sections which are listed below. Informational responses (1
2 min read
How to Dynamically Call Router Function in Node.js ?
In Node.js Dynamically calling a router function means that the function is selected at runtime based on the request parameters, instead of being explicitly defined in the code. This can be useful when you want to handle a large number of similar requests without having to define a separate function
4 min read
HTTP status codes | Informational Responses
The HTTP status codes are used to indicate that any specific HTTP request has successfully completed or not. The HTTP status codes are categorized into five sections those are listed below: Informational responses (100â199) Successful responses (200â299) Redirects (300â399) Client errors (400â499) S
2 min read
Node.js console.warn() Function
The console.warn() function from console class of Node.js is used to display the warning messages on the console. It prints to stderr with newline. Note: This function is an alias of console.error() function. Syntax: console.warn( [data][, ...args] ) Parameter: This function can contains multiple pa
1 min read
What do you understand by the HTTP Status Codes ?
The HTTP or the HyperText Transfer Protocol is a protocol of the application layer. It helps in establishing communication between a web browser and a web server. When a client requests any information, the browser sends a response using numeric status codes. These status codes are in the form of 3-
3 min read
HTTP Request vs HapiJS Request in Node.js
Node.js: Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and itâs not a programming language. Most of the people are confused and understand itâs a framework or a programming languag
3 min read