Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active August 5, 2017 22:08
Show Gist options
  • Save daliborgogic/ba3b7c5f26e08ad969d03217d7d2a801 to your computer and use it in GitHub Desktop.
Save daliborgogic/ba3b7c5f26e08ad969d03217d7d2a801 to your computer and use it in GitHub Desktop.
Node.js Push Stream
// https://github.com/nodejs/node/blob/master/doc/api/http2.md
const fs = require('fs')
const http2 = require('http2')
const options = {
key: fs.readFileSync('privkey.pem'),
cert: fs.readFileSync('cert.pem')
}
const server = http2.createSecureServer(options)
server.on('stream', (stream, requestHeaders) => {
stream.respond({
'content-type': 'text/html',
':status': 200
})
stream.pushStream({ ':path': '/main.js' }, (pushStream => {
pushStream.respond({
'content-type': 'application/javascript',
':status': 200
})
pushStream.end('console.log(\'Pushed!\')')
}))
stream.end('<h1>Hello World</h1><script src="main.js"></script>')
})
server.listen(443)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment