Created
March 10, 2020 12:42
-
-
Save holocc/6b1ff6e00c388e51fc49322d8869e87c to your computer and use it in GitHub Desktop.
a http2 push demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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