Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active July 13, 2018 19:32
Show Gist options
  • Save daliborgogic/273a967bedad74885965712b0ff822e2 to your computer and use it in GitHub Desktop.
Save daliborgogic/273a967bedad74885965712b0ff822e2 to your computer and use it in GitHub Desktop.
Asynchronous HTTP2 microservice example
const h2 = require('http2')
const { run, send } = require('micro')
const cert = require('openssl-self-signed-certificate')
const { PORT = 3443 } = process.env
const options = {
key: cert.key,
cert: cert.cert,
passphrase: cert.passphrase
}
const microH2 = fn => h2.createSecureServer(options, (req, res) => run(req, res, fn))
const server = microH2(async (req, res) =>
send(res, 200, { encrypted: req.connection.encrypted })
)
server.listen(PORT)
{
"name": "with-h2",
"version": "1.0.0",
"scripts": {
"start": "node ."
},
"dependencies": {
"http2": "^3.3.7",
"micro": "^9.3.2",
"openssl-self-signed-certificate": "^1.1.6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment