Skip to content

feat(http2): add HTTP/2 support #6487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/http2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# HTTP/2 Support

This directory contains an example demonstrating HTTP/2 support in Express.

## What is HTTP/2?

HTTP/2 is the second major version of the HTTP protocol. It offers several advantages over HTTP/1.1:

- Multiplexing: Multiple requests can be sent over a single connection
- Header compression: Reduces overhead and improves performance
- Server push: Allows the server to proactively send resources to the client
- Binary format: More efficient to parse than HTTP/1.1's text format

## Prerequisites

- Node.js (version 8.4.0 or higher)
- OpenSSL (for generating certificates)

## Running the example

1. Generate self-signed certificates:

```bash
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
-keyout private-key.pem -out certificate.pem
```

2. Run the example:

```bash
node http2.js
```

3. Visit https://localhost:3000 in your browser

**Note:** Your browser may show a security warning because of the self-signed certificate. This is expected and you can proceed by accepting the risk.

## How it works

The `http2.js` file demonstrates:
- Creating an HTTP/2 server with Express
- Setting up secure connections with TLS certificates
- Serving content through the HTTP/2 protocol

For more information about HTTP/2 support in Node.js, visit the [official documentation](https://nodejs.org/api/http2.html).
72 changes: 72 additions & 0 deletions examples/http2/http2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

/**
* Example application showcasing Express with HTTP/2 support
*
* Note: To run this example, you need to generate self-signed certificates first:
*
* $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
* -keyout examples/http2/private-key.pem \
* -out examples/http2/certificate.pem
*/

const express = require('../..');
const fs = require('node:fs');
const path = require('node:path');
const app = express();

// ---------------
// BASIC ROUTES
// ---------------

app.get('/', (req, res) => {
res.json({
message: 'Express with HTTP/2 support',
protocol: req.httpVersion === '2.0' ? 'HTTP/2' : 'HTTP/1.1',
method: req.method,
url: req.url
});
});

app.get('/stream', (req, res) => {
// HTTP/2 streaming example
res.write(JSON.stringify({ message: 'This is the first part' }) + '\n');

setTimeout(() => {
res.write(JSON.stringify({ message: 'This is the second part' }) + '\n');

setTimeout(() => {
res.write(JSON.stringify({ message: 'This is the final part' }) + '\n');
res.end();
}, 1000);
}, 1000);
});

// Try to load certificates for HTTP/2 HTTPS
let server;
try {
const options = {
key: fs.readFileSync(path.join(__dirname, 'private-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'certificate.pem')),
allowHTTP1: true // Allow HTTP/1.1 fallback
};

// Create HTTP/2 secure server
server = app.http2Secure(app, options);

server.listen(3000, () => {
console.log('Express HTTP/2 server running on https://localhost:3000');
console.log('Note: Since this uses a self-signed certificate, your browser may show a security warning');
});
} catch (err) {
console.error('Could not load certificates for HTTPS:', err.message);
console.log('Falling back to plain HTTP/2...');

// Create plain HTTP/2 server
server = app.http2(app, {});

server.listen(3000, () => {
console.log('Express HTTP/2 server running on http://localhost:3000');
console.log('Note: Some browsers only support HTTP/2 over HTTPS');
});
}
6 changes: 6 additions & 0 deletions lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var proto = require('./application');
var Router = require('router');
var req = require('./request');
var res = require('./response');
var http2 = require('./http2');

/**
* Expose `createApplication()`.
Expand Down Expand Up @@ -51,6 +52,10 @@ function createApplication() {
app: { configurable: true, enumerable: true, writable: true, value: app }
})

// Add HTTP/2 support
app.http2 = http2.createServer;
app.http2Secure = http2.createSecureServer;

app.init();
return app;
}
Expand Down Expand Up @@ -79,3 +84,4 @@ exports.raw = bodyParser.raw
exports.static = require('serve-static');
exports.text = bodyParser.text
exports.urlencoded = bodyParser.urlencoded
exports.http2 = http2;
Loading