How to use the STATUS_CODES function from http

Find comprehensive JavaScript http.STATUS_CODES code examples handpicked from public code repositorys.

http.STATUS_CODES is a property of the Node.js http module that provides a collection of HTTP status codes and their associated descriptions.

352
353
354
355
356
357
358
359
360
361
362


/**
 * Send given HTTP status code.
 *
 * Sets the response status to `statusCode` and the body of the
 * response to the standard description from node's http.STATUS_CODES
 * or the statusCode number if no description.
 *
 * Examples:
 *
fork icon0
star icon0
watch icon1

How does http.STATUS_CODES work?

http.STATUS_CODES is a built-in Node.js property that provides a mapping of standard HTTP status code numbers to their corresponding descriptive phrases. When an HTTP response is sent, it includes a status code, which indicates the success or failure of the request. The status code is a 3-digit number, and the description or phrase is a human-readable string that explains the meaning of the code. http.STATUS_CODES is an object that provides a convenient way to access the description or phrase associated with a particular status code. For example, http.STATUS_CODES[200] returns the string "OK", which is the standard description for the 200 status code indicating a successful request.

Ai Example

1
2
3
4
5
const http = require("http");

console.log(http.STATUS_CODES[200]); // Output: OK
console.log(http.STATUS_CODES[404]); // Output: Not Found
console.log(http.STATUS_CODES[500]); // Output: Internal Server Error

In this example, we are logging the descriptive phrases for HTTP status codes 200, 404, and 500 using http.STATUS_CODES.