Skip to content

Instantly share code, notes, and snippets.

@jiangkunhe

jiangkunhe/push.js

Created Mar 10, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.