HTTP String Parser
http-string-parser is a lightweight, proof-of-concept library designed to parse raw HTTP request and response messages from strings within Node.js environments. The package is currently at version 0.0.6, with its last known update in 2014, indicating it is no longer actively maintained. Its core functionality offers methods like `parseRequest`, `parseResponse`, `parseRequestLine`, `parseStatusLine`, and `parseHeaders` to break down HTTP messages into structured JavaScript objects. A key differentiator is its explicit admission of being a 'naive' parser, not leveraging Node.js core's C bindings for HTTP parsing or more robust alternatives like `http-pegjs`, which were even suggested as future replacements in its own README. It serves as a basic utility for scenarios where a simple, non-production-grade parsing of well-formed HTTP strings is sufficient, but lacks the robustness and ongoing support expected for modern applications.
Common errors
-
TypeError: parser.parseRequest is not a function
cause Attempting to call `parseRequest` without correctly requiring the module as a CommonJS object, or trying to destructure incorrectly.fixEnsure you are using `const parser = require('http-string-parser');` and then calling `parser.parseRequest(...)` or `parser.parseResponse(...)`. -
SyntaxError: Cannot use import statement outside a module
cause Trying to use `import` syntax (`import parser from 'http-string-parser';`) in a Node.js project configured for CommonJS modules.fixConvert your file to CommonJS (`const parser = require('http-string-parser');`) or properly configure your project to use ES Modules (e.g., by adding `"type": "module"` to `package.json`), although this package's core functionality is not designed for ESM. -
Parsing results are incomplete or incorrect for specific HTTP messages (e.g., chunked encoding, unusual headers).
cause The parser is 'naive' and does not implement full HTTP RFC compliance, leading to failures or partial parsing for complex or non-standard HTTP messages.fixThis package is not designed for robust parsing. For complex HTTP scenarios, switch to a more mature and compliant HTTP parsing library or the `node:http` module.
Warnings
- breaking This package is effectively abandoned, with its last commit in 2014 and version 0.0.6. It is not compatible with modern Node.js versions out-of-the-box (e.g., ESM modules) and lacks ongoing maintenance for security vulnerabilities or bug fixes.
- gotcha The README explicitly states this is a 'proof of concept, naive HTTP parsing, wheel re-invention.' It does not handle complex HTTP features, malformed requests, or edge cases robustly. Do not use for production systems requiring resilience or full RFC compliance.
- deprecated The package's own README suggests it 'may be replaced with better parser from [Node.JS core's C bindings of NGINX HTTP parser] or [PEG.js HTTP parser]'. This indicates it was always intended as a temporary or educational solution.
Install
-
npm install http-string-parser -
yarn add http-string-parser -
pnpm add http-string-parser
Imports
- parser
import parser from 'http-string-parser';
const parser = require('http-string-parser'); - parseRequest
import { parseRequest } from 'http-string-parser';const { parseRequest } = require('http-string-parser'); - parseResponse
import { parseResponse } from 'http-string-parser';const parser = require('http-string-parser'); const response = parser.parseResponse(responseString);
Quickstart
const parser = require('http-string-parser');
const httpRequestString = `GET /api/data?id=123 HTTP/1.1\r\nHost: example.com\r\nUser-Agent: NodeParser/1.0\r\nContent-Type: application/json\r\nContent-Length: 20\r\n\r\n{"message": "hello"}`;
const httpResponseString = `HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 22\r\n\r\n{"status": "success"}`;
try {
console.log('Parsing HTTP Request:');
const request = parser.parseRequest(httpRequestString);
console.log(JSON.stringify(request, null, 2));
console.log('\nParsing HTTP Response:');
const response = parser.parseResponse(httpResponseString);
console.log(JSON.stringify(response, null, 2));
} catch (error) {
console.error('An error occurred during parsing:', error.message);
}