← Runnable Projects

阅读中文版本

HTTP Status Code Lookup CLI

A Node CLI that maps an HTTP status code to its reason phrase and category, and lists every code in a category such as 4xx.

beginnerjavascriptunverified

Project

node-http-status-lookup

Entry point: status.mjs

Install

node --version

Run

node status.mjs 404

Test

node --test status.test.mjs

Expected output

Prints the code, its reason phrase, and its class, for example 404 Not Found (client error).

Dependencies

  • node >=20

What it does

Holds a table of standard status codes and their phrases, classifies each by its leading digit, and either looks up one code or lists a whole class. Unknown codes are reported as reserved.

const CODES = {
  200: 'OK', 201: 'Created', 204: 'No Content', 301: 'Moved Permanently', 302: 'Found',
  304: 'Not Modified', 400: 'Bad Request', 401: 'Unauthorized', 403: 'Forbidden',
  404: 'Not Found', 409: 'Conflict', 422: 'Unprocessable Entity', 429: 'Too Many Requests',
  500: 'Internal Server Error', 502: 'Bad Gateway', 503: 'Service Unavailable', 504: 'Gateway Timeout',
};
const CLASS = { 1: 'informational', 2: 'success', 3: 'redirection', 4: 'client error', 5: 'server error' };

const arg = process.argv[2];
if (!arg) { console.error('usage: status <code|1xx..5xx>'); process.exit(1); }
if (/^[1-5]xx$/i.test(arg)) {
  const cls = Number(arg[0]);
  for (const [code, phrase] of Object.entries(CODES)) if (Number(code[0]) === cls) console.log(code + ' ' + phrase);
} else {
  const code = Number(arg);
  console.log(code + ' ' + (CODES[code] ?? '(reserved)') + ' (' + (CLASS[Number(arg[0])] ?? 'unknown') + ')');
}

Run and verify

node status.mjs 404

Run the test command to check the implementation: node --test status.test.mjs.

Expected Output

Querying a single code prints the phrase and class on one line:

404 Not Found (client error)

Limitations

Covers the commonly used codes; less common ones are reported as reserved rather than enumerated, and phrases follow RFC 9110 wording.