HTTP/HTTPS Request Wrapper
The `http-https` package, at its sole stable version `1.0.0`, is a minimal wrapper designed to simplify making HTTP or HTTPS requests in Node.js. It automatically selects the appropriate native module (`http` or `https`) based on the protocol found in the target URL, exposing a unified `request` method similar to `http.request`. This package was published over a decade ago (around 2012) by Isaac Schlueter and has seen no updates, new features, or bug fixes since its initial release. Consequently, it is an abandoned project and is not actively maintained. While it streamlined basic outbound web requests in older Node.js environments, modern applications should consider actively maintained alternatives that offer current best practices, better error handling, and broader feature sets.
Common errors
-
TypeError: hh.request is not a function
cause Attempting to use `hh.request` when `http-https` was imported incorrectly or when `require` returns `undefined`.fixEnsure the package is correctly `require`d: `const hh = require('http-https');`. If in an ESM context, consider this package incompatible and use a modern alternative. -
Error: write after end
cause Attempting to write to or end a request multiple times, a common mistake when dealing with Node.js's native `http`/`https` modules that this wrapper exposes directly.fixEnsure `req.end()` is called exactly once after all data has been written to the request body, and avoid attempting to write to the request after `req.end()` has been invoked.
Warnings
- breaking This package is abandoned and has not been updated in over a decade. It does not receive security patches, bug fixes, or feature updates. Using it in production environments is strongly discouraged due to potential vulnerabilities and compatibility issues with modern Node.js.
- gotcha The package uses Node.js's legacy `url.parse` API in its examples and internal logic, which is deprecated in favor of the global `URL` constructor. While `url.parse` still works, it's considered outdated and has known quirks.
- gotcha This package is CommonJS-only and does not provide an ECMAScript Module (ESM) export. Attempting to `import` it will result in an error in pure ESM environments or require explicit CJS compatibility wrappers.
Install
-
npm install http-https -
yarn add http-https -
pnpm add http-https
Imports
- hh
import hh from 'http-https'
const hh = require('http-https') - request
import { request } from 'http-https'const { request } = require('http-https') - url.parse
new URL('...').hrefconst url = require('url'); url.parse('...')
Quickstart
const hh = require('http-https');
const url = require('url'); // Node.js built-in url module
// Example 1: Direct URL string
const simpleReq = hh.request('http://example.com/bar', (res) => {
console.log(`HTTP Status: ${res.statusCode}`);
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
console.log('Simple HTTP Response received.');
});
});
simpleReq.on('error', (e) => console.error(`Simple request error: ${e.message}`));
simpleReq.end();
// Example 2: With parsed URL object and custom headers
const someUrlMaybeHttpMaybeHttps = 'https://secure.example.com/foo';
const parsedOpt = url.parse(someUrlMaybeHttpMaybeHttps);
parsedOpt.headers = {
'User-Agent': 'flergy mc flerg/1.0'
};
parsedOpt.method = 'GET'; // Changed to GET for typical fetch
const secureReq = hh.request(parsedOpt, (res) => {
console.log(`HTTPS Status: ${res.statusCode}, Headers: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
console.log('Secure HTTPS Response received.');
});
});
secureReq.on('error', (e) => console.error(`Secure request error: ${e.message}`));
secureReq.end();