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 Handle Syntax Errors in Node.js ?
If there is a syntax error while working with Node.js it occurs when the code you have written violates the rules of the programming language you are using. In the case of Node.js, a syntax error might occur if you have mistyped a keyword, or if you have forgotten to close a parenthesis or curly bra
4 min read
10 Most Common HTTP Status Codes
You search for anything on Google, and by this, a request is being sent to the server, then the server responds. This response is done using HTTP (HyperText Transfer Protocol) by the server. There can be two cases - the server may respond with the information it has, or else it may show an error wit
5 min read
How to Change NodeJS 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
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
HTTP status codes | Client Error Responses
The browser and the site server have a conversation in the form of HTTP status codes. The server gives responses to the browserâs request in the form of a three-digit code known as HTTP status codes. The categorization of HTTP status codes is done in five sections which are listed below. Information
4 min read
How to convert function call with two callbacks promise in Node.js ?
Promise: Promise is used to handle the result, if it gets the required output then it executes the code of then block, and if it gets the error then it executes the code of the catch block.A promise looks like this -function() .then(data => { // After promise is fulfilled console.log(data); }) .c
2 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
How to Handle Errors in Node.js ?
Node.js is a JavaScript extension used for server-side scripting. Error handling is a mandatory step in application development. A Node.js developer may work with both synchronous and asynchronous functions simultaneously. Handling errors in asynchronous functions is important because their behavior
4 min read