Transparent HTTP/2 and HTTP/1.1 Client for Node.js

1.3.5 · maintenance · verified Wed Apr 22

http2-client is a Node.js library designed to provide a transparent, drop-in replacement for Node's native `http` and `https` modules. It allows applications to make requests that automatically negotiate between HTTP/1.1 and HTTP/2 protocols using ALPN, without requiring changes to the existing `http.request` or `http.get` interfaces. This is particularly useful for integrating HTTP/2 capabilities into older modules or applications that are tightly coupled to the HTTP/1.1 API. The library manages connection pooling for both protocols, ensuring efficient resource utilization. The current stable version is 1.3.5. Based on the provided release history (v1.3.2, v1.3.3), the package appears to have an infrequent release cadence, suggesting a maintenance rather than actively developed status. Key differentiators include its adherence to the standard Node.js `http` module API and automatic protocol detection.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `http2-client`'s `request` function to make transparent requests to both HTTP/1.1 and HTTP/2 servers using the same API as Node's native `http` module. It prints the URL, status code, and HTTP version for each response.

const { request } = require('http2-client');

const h1Target = 'http://www.example.com/';
const h2Target = 'https://www.google.com/'; // Using google as example.com might not support h2

console.log('Making HTTP/1.1 request...');
const req1 = request(h1Target, (res) => {
    console.log(`\nUrl : ${h1Target}\nStatus : ${res.statusCode}\nHttpVersion : ${res.httpVersion}`);
    res.on('data', () => {}); // Consume response data
    res.on('end', () => console.log('HTTP/1.1 request ended.'));
});
req1.on('error', (e) => console.error(`HTTP/1.1 request error: ${e.message}`));
req1.end();

console.log('\nMaking HTTP/2 request...');
const req2 = request(h2Target, (res) => {
    console.log(`\nUrl : ${h2Target}\nStatus : ${res.statusCode}\nHttpVersion : ${res.httpVersion}`);
    res.on('data', () => {}); // Consume response data
    res.on('end', () => console.log('HTTP/2 request ended.'));
});
req2.on('error', (e) => console.error(`HTTP/2 request error: ${e.message}`));
req2.end();

view raw JSON →