Skip to content

Instantly share code, notes, and snippets.

@holocc
Created March 10, 2020 12:42
Show Gist options
  • Save holocc/6b1ff6e00c388e51fc49322d8869e87c to your computer and use it in GitHub Desktop.
Save holocc/6b1ff6e00c388e51fc49322d8869e87c to your computer and use it in GitHub Desktop.
a http2 push demo
const http2 = require('http2')
const fs = require('fs')
const privatekey = fs.readFileSync('./certificate/private.pem', 'utf8')
const certificate = fs.readFileSync('./certificate/csr.crt', 'utf8')
const server = http2.createSecureServer({
key: privatekey,
cert: certificate
}, function (req, res) {
res.stream.pushStream({ ':path': '/b.js' }, (err, pushStream, headers) => {
if (err) throw err;
pushStream.respond({ ':status': 200 });
pushStream.end(fs.readFileSync('./b.js'));
});
res.stream.end(fs.readFileSync('./index.html'))
})
const port = 8443
server.listen(port, (err) => {
if (err) {
console.log(err)
return
}
console.log('server is listening on port ', port)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment