Last active
August 5, 2017 22:08
-
-
Save daliborgogic/ba3b7c5f26e08ad969d03217d7d2a801 to your computer and use it in GitHub Desktop.
Node.js Push Stream
This file contains hidden or 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
// 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