Q39: How do you run Express over HTTP/2?
Express natively handles HTTP/1.1 requests through Node's http module. To serve over HTTP/2, wrap the Express app using Node's spdy or native http2 modules.
HTTP/2 Express Server with SPDY
const spdy = require('spdy');
const fs = require('fs');
const app = require('./app');
const options = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt')
};
spdy.createServer(options, app).listen(3000, () => {
console.log('HTTP/2 Express server running on port 3000');
});💡 The SPDY wrapper translates HTTP/2 multiplexed streams into HTTP/1.1 req/res objects compatible with Express.
Key Takeaways
- HTTP/2 multiplexing reduces latency across concurrent asset requests.
- Most production setups terminate HTTP/2 at the reverse proxy level (e.g., Nginx), passing standard HTTP/1.1 upstream to Express.