Skip to content

Instantly share code, notes, and snippets.

@israeljrs
Created September 12, 2017 14:21
Show Gist options
  • Save israeljrs/b15aa32451905d463606371547bf969d to your computer and use it in GitHub Desktop.
Save israeljrs/b15aa32451905d463606371547bf969d to your computer and use it in GitHub Desktop.
http2_node.js
const http2 = require('http2')
const server = http2.createSecureServer(
{ cert, key },
onRequest
)
function push (stream, filePath) {
const { file, headers } = getFile(filePath)
const pushHeaders = { [HTTP2_HEADER_PATH]: filePath }
stream.pushStream(pushHeaders, (pushStream) => {
pushStream.respondWithFD(file, headers)
})
}
function onRequest (req, res) {
// Push files with index.html
if (reqPath === '/index.html') {
push(res.stream, 'bundle1.js')
push(res.stream, 'bundle2.js')
}
// Serve file
res.stream.respondWithFD(file.fileDescriptor, file.headers)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment