const http2 = require('http2');
const fs = require('fs');
// SSL options for HTTP/2
const options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem')
};
// Create an HTTP/2 server
const server = http2.createSecureServer(options);
// Handle incoming streams
server.on('stream', (stream, headers) => {
const path = headers[':path'];
const method = headers[':method'];
console.log(`${method} ${path}`);
// Respond to the request
stream.respond({
'content-type': 'text/html',
':status': 200
});
stream.end('<h1>HTTP/2 Server</h1><p>This page was served via HTTP/2</p>');
});
// Start the server
const PORT = 8443;
server.listen(PORT, () => {
console.log(`HTTP/2 server running at https://localhost:${PORT}/`);
});