Intermediate LevelExpress JS
How do you configure an HTTP/2 server with Express using node:http2 or spdy?
❓ Interview Prompt: Explain HTTP/2 multiplexing, spdy / node:http2 integration with Express, SSL TLS certificates, and performance gains.
HTTP/2 enables multiplexing multiple requests over a single TCP connection, eliminating head-of-line blocking.
Express HTTP/2 Setup:
Express can run over HTTP/2 using the native 'node:http2' module or 'spdy' package, requiring SSL TLS certificates ('key.pem' and 'cert.pem') because browsers support HTTP/2 exclusively over HTTPS!
Code Implementation Example
javascript
import express from 'express';
import http2 from 'node:http2';
import fs from 'node:fs';
const app = express();
app.get('/api/http2-demo', (req, res) => res.json({ protocol: 'HTTP/2' }));
const options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt'),
allowHTTP1: true
};
// Create secure HTTP/2 server wrapping Express instance!
http2.createSecureServer(options, app).listen(443, () => {
console.log('HTTP/2 Express server running on port 443');
});💡Configuring a secure HTTP/2 server wrapping Express using node:http2.
🎯 Interview Key Takeaway
Wrap Express in node:http2.createSecureServer with SSL certificates to serve HTTP/2 multiplexed endpoints.
#HTTP/2#SSL#Performance#Intermediate
Back to all Express JS Questions→