HTTP Headers Parser
The `http-headers` package provides a utility to parse HTTP request and response start-lines and headers into a structured JavaScript object. It can process raw string or Buffer data containing complete HTTP messages, or directly extract and parse headers from Node.js `http.ServerResponse` objects. Key features include automatic body detection and ignored parsing, RFC 2068 compliance, and support for multi-line and repeating headers. The current stable version is 3.0.2. This package appears to be unmaintained; its last update was several years ago, meaning it does not receive active development, bug fixes, or compatibility updates for newer Node.js versions or HTTP standards (e.g., HTTP/2, HTTP/3). Its primary differentiator was simplifying the extraction of response headers from Node's internal `_header` property, which was not easily accessible otherwise.
Common errors
-
SyntaxError: Named export 'default' not found (module 'http-headers')
cause Attempting to use ES module `import` syntax for a CommonJS-only module.fixUse CommonJS `require` syntax instead: `const httpHeaders = require('http-headers');` -
TypeError: httpHeaders is not a function
cause Incorrectly destructuring or calling `require('http-headers')` as a property rather than the function it directly exports.fixEnsure you are assigning the result of `require('http-headers')` directly to a variable and then calling it: `const parser = require('http-headers'); parser(data);` -
TypeError: data must be a string, Buffer, or http.ServerResponse instance
cause Passing an unsupported data type (e.g., a plain object, array, or null) to the `httpHeaders` function.fixVerify that the `data` argument is either a string, a Buffer containing HTTP message data, or an instance of Node.js `http.ServerResponse`.
Warnings
- breaking The package is unmaintained since 2017. It has not received updates for over 7 years and may contain unpatched security vulnerabilities, especially when parsing arbitrary network input. Relying on it in production for security-sensitive applications is risky.
- gotcha This module is exclusively CommonJS (`require`). It does not provide an ES module build and cannot be directly imported using `import` statements in modern Node.js or browser environments without a CommonJS wrapper or bundler configuration.
- gotcha The package relies on accessing the private `_header` property of `http.ServerResponse` to extract headers. While functional at the time of its development, relying on private API properties can lead to breakage in future Node.js versions if the internal implementation changes.
Install
-
npm install http-headers -
yarn add http-headers -
pnpm add http-headers
Imports
- httpHeaders
import httpHeaders from 'http-headers'
const httpHeaders = require('http-headers') - httpHeaders (with onlyHeaders option)
const parse = require('http-headers'); const headersOnly = parse(data, true);
Quickstart
const http = require('http');
const httpHeaders = require('http-headers');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('X-Request-ID', '12345');
res.end('Hello World');
// Log the parsed response headers as they were sent to the client
// This uses http-headers to access the internal _header property of res
console.log('Parsed ServerResponse headers:', httpHeaders(res));
}).listen(8080, () => {
console.log('Server listening on http://localhost:8080');
});
// To test, run the server and access http://localhost:8080 in your browser or with curl.
// The server's console will output the parsed response headers.