HTTP/2 Support for Express

1.1.0 · active · verified Wed Apr 22

http2-express integrates HTTP/2 protocol support into Express.js applications. The current stable version, 1.1.0, adds explicit support for Express 5 while maintaining backward compatibility with Express 4. Its release cadence is responsive to major Express and Node.js version updates. A key differentiator is its direct compatibility with the now-deprecated `http2-express-bridge` package, allowing for a straightforward migration by just changing the import name. Unlike its predecessor, http2-express intentionally removes support for HTTP/2 Server Push, aligning with current browser and client behavior where Server Push is largely deprecated due to observed limited or negative performance impacts. The library supports both secure (HTTPS/2) and insecure (h2c) HTTP/2 connections and provides options for backward compatibility with HTTP/1.1 clients.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates setting up a basic HTTP/2 Express server with TLS (HTTPS/2) support, serving a 'Hello World' message. It highlights how to wrap an Express app, configure certificates, and listen for connections.

import express from 'express';
import * as http2 from 'node:http2';
import http2Express from 'http2-express';
import fs from 'node:fs';

const app = http2Express(express);

app.get('/', (req, res) => {
  res.send('Hello World from HTTP/2!');
});

const options = {
  key: fs.readFileSync(process.env.HTTP2_KEY_PATH ?? 'path/to/your/certificate.key'),
  cert: fs.readFileSync(process.env.HTTP2_CERT_PATH ?? 'path/to/your/certificate.crt'),
  passphrase: process.env.HTTP2_CERT_PASSPHRASE ?? 'your_certificate_passphrase',
  allowHTTP1: false
};

const server = http2.createSecureServer(options, app);

server.listen(44320, () => {
  console.info('HTTP/2 server listening on https://localhost:44320...');
});

view raw JSON →