HTTP Reason Phrase Lookup
raw JSON →http-reasons is a minimalist JavaScript utility that provides a database to look up human-readable reason phrases for HTTP response status codes. The package is currently at version 0.1.0 and appears to be in a maintenance state, as there have been no significant updates or new releases from Postman Labs, its maintainer, for several years. Its primary function is a static data lookup, offering a simple mapping of numeric HTTP status codes to their corresponding standard reason phrases. Developers seeking more actively maintained alternatives with broader features like explicit ES module support, TypeScript definitions, and more comprehensive status code constants (e.g., `StatusCodes.OK`, `ReasonPhrases.OK`) should consider packages like `http-status-codes`, which offers a more robust and modern API.
Common errors
error TypeError: httpReasons is not a function ↓
const reason = httpReasons[200]; error TS2307: Cannot find module 'http-reasons' or its corresponding type declarations. ↓
@types/http-reasons if it exists (unlikely for an unmaintained package) or create a manual declaration file, for example, declare module 'http-reasons'; as a quick fix, or provide more specific types if needed. error ERR_REQUIRE_ESM: require() of ES Module ... not supported ↓
import httpReasons from 'http-reasons';. If using CJS, stick to const httpReasons = require('http-reasons');. Consider setting "type": "module" in your package.json for new ESM projects and adjusting imports/exports accordingly. Warnings
gotcha The `http-reasons` package is at an early version (0.1.0) and has not been updated in several years by Postman Labs. This indicates a lack of active maintenance, meaning bug fixes, new features, or compatibility updates for newer Node.js versions or standards are unlikely. ↓
breaking This package was developed in a CommonJS-first era. While Node.js offers some interoperability, direct `import` statements in an ES Module context might require specific build configurations or lead to `ERR_REQUIRE_ESM` errors if not handled correctly. It does not explicitly define `exports` in its `package.json`. ↓
gotcha The package does not ship with official TypeScript type definitions (`.d.ts` files). This means TypeScript users will not get type safety or autocompletion for the `httpReasons` object by default, potentially leading to runtime errors if incorrect keys are accessed. ↓
gotcha The package exports a static object, not a function. Attempting to call `httpReasons(200)` will result in a `TypeError`, as it's designed for direct property access (e.g., `httpReasons[200]`). ↓
Install
npm install http-reasons yarn add http-reasons pnpm add http-reasons Imports
- httpReasons wrong
import { httpReasons } from 'http-reasons';correctimport httpReasons from 'http-reasons'; - getReason wrong
const { getReason } = require('http-reasons');correctconst httpReasons = require('http-reasons'); const reason = httpReasons[200]; - reasonsMap wrong
import { ReasonsMap } from 'http-reasons';correctimport type { ReasonsMap } from 'http-reasons/types'; // (Assuming custom type declaration)
Quickstart
import httpReasons from 'http-reasons';
// Look up the reason phrase for a 200 OK status code
const okReason = httpReasons[200];
console.log(`200: ${okReason}`); // Expected: "OK"
// Look up the reason phrase for a 404 Not Found status code
const notFoundReason = httpReasons[404];
console.log(`404: ${notFoundReason}`); // Expected: "Not Found"
// Attempt to look up a non-existent status code
const nonExistentReason = httpReasons[999];
console.log(`999: ${nonExistentReason}`); // Expected: undefined
// Example of using the lookup in a simple server response (conceptual)
/*
import http from 'http';
http.createServer((req, res) => {
const statusCode = 200; // or 404, 500, etc.
const reasonPhrase = httpReasons[statusCode] || 'Unknown Status';
res.writeHead(statusCode, reasonPhrase, { 'Content-Type': 'text/plain' });
res.end(`Status ${statusCode}: ${reasonPhrase}`);
}).listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
*/