HTTPolyglot (Abandoned - Use @httptoolkit/httpolyglot)
raw JSON →The original `httpolyglot` package, last published in 2016, is a Node.js module designed to serve both HTTP and HTTPS connections over a single port by sniffing the initial bytes of an incoming connection to determine the protocol. This package, version `0.1.2`, is **abandoned** and no longer maintained. Its functionality is limited to HTTP/1.x and HTTPS with outdated Node.js compatibility (down to `v0.10.0`). For modern applications, including HTTP/2 support, improved TLS handling, TypeScript compatibility, and active maintenance, developers are strongly advised to use the actively maintained and significantly enhanced fork, `@httptoolkit/httpolyglot`, which serves as the de facto successor for this functionality. The original package has no defined release cadence and lacks contemporary features and security updates.
Common errors
error httpolyglot.createServer is not a function ↓
@httptoolkit/httpolyglot package. If already installed, verify it's imported correctly via ESM: import * as httpolyglot from '@httptoolkit/httpolyglot';. error TypeError: Cannot read properties of undefined (reading 'key') ↓
createServer call to httpolyglot.createServer({ tls: { key: fs.readFileSync('server.key'), cert: fs.readFileSync('server.crt') } }, ...);. error ERR_REQUIRE_ESM: require() of ES Module .../node_modules/@httptoolkit/httpolyglot/dist/index.js from ... not supported. ↓
"type": "module" to your package.json or by using .mjs file extensions. Then use import * as httpolyglot from '@httptoolkit/httpolyglot';. Alternatively, use dynamic import() for specific needs. Warnings
breaking The original `httpolyglot` package (v0.1.2) is abandoned and has not been updated in over 9 years. It lacks support for modern Node.js versions, HTTP/2, and contains unaddressed issues. It is strongly recommended NOT to use this package in any new or existing project. ↓
breaking The API for `createServer` in the actively maintained fork (`@httptoolkit/httpolyglot`) has changed significantly. TLS configuration is now nested under a `tls` property within the main configuration object. The original package accepted `key` and `cert` directly at the top level. ↓
gotcha The `@httptoolkit/httpolyglot` package is written in TypeScript and primarily designed for ESM usage. Attempting to `require()` it in a CommonJS context or using incorrect import syntax can lead to runtime errors or undefined modules. ↓
breaking The `@httptoolkit/httpolyglot` fork drops support for very old Node.js versions, requiring Node.js v20 or newer. The original `httpolyglot` package specified `>=0.10.0`. ↓
Install
npm install httpolyglot yarn add httpolyglot pnpm add httpolyglot Imports
- createServer wrong
const httpolyglot = require('httpolyglot'); httpolyglot.createServer(...);correctimport { createServer } from '@httptoolkit/httpolyglot'; - Server
import { Server } from '@httptoolkit/httpolyglot'; - default wrong
import httpolyglot from '@httptoolkit/httpolyglot';correctimport * as httpolyglot from '@httptoolkit/httpolyglot';
Quickstart
import * as httpolyglot from '@httptoolkit/httpolyglot';
import * as fs from 'node:fs';
import * as https from 'node:https'; // For generating certificates
// --- Utility to generate self-signed certs for testing ---
// In a real app, use proper certificate management (e.g., LetsEncrypt)
const generateSelfSignedCert = () => {
try {
fs.readFileSync('server.key');
fs.readFileSync('server.crt');
} catch (e) {
console.log('Generating self-signed certificates...');
const { generateKeyPairSync } = require('node:crypto');
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
});
fs.writeFileSync('server.key', privateKey);
// A simple self-signed cert (for testing only!)
const cert = `-----BEGIN CERTIFICATE-----\nMIIDDDCCAfSgAwIBAgIUQd+Wj+T/h7g8v4f5g4f/g4f/g4f/g4f/g4f/g4f/g4f\nMBMGCSqGSIb3DQEBCwUAMBQxDDAKBgNVBAMMA2xvYzAeFw0yNjA0MjMyMDIyNDZa\nFw0yNzA0MjMyMDIyNDZaMBQxDDAKBgNVBAMMA2xvYWMwggEiMA0GCSqGSIb3DQEB\nAQUAA4IBDwAwggEKAoIBAQCmF5fN+h/d+g/g4f/g4f/g4f/g4f/g4f/g4f/g4f\nAgMBAAGjMDAuMBsGA1UdEQQUMBKCDmRpcmVjdC5leGFtcGxlMA4GA1UdDwEB/wQE\nAwIBAzANBgkqhkiG9w0BAQsFAAOCAQEAy/g4f/g4f/g4f/g4f/g4f/g4f/g4f\n-----END CERTIFICATE-----\n`; // Placeholder
fs.writeFileSync('server.crt', cert);
console.log('Certificates generated. PLEASE DO NOT USE IN PRODUCTION.');
}
};
generateSelfSignedCert();
// --- End cert generation utility ---
const key = fs.readFileSync('server.key');
const cert = fs.readFileSync('server.crt');
httpolyglot.createServer({
// For the @httptoolkit fork, TLS config is nested under `tls`
tls: {
key: key,
cert: cert,
},
}, function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end((req.socket.encrypted ? 'HTTPS' : 'HTTP') + ' Connection!\nHello from @httptoolkit/httpolyglot!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
console.log('Visit http://localhost:9000 and https://localhost:9000 in your browser.');
console.log('Ensure you accept any self-signed certificate warnings for HTTPS.');
});