From 098c210d6e4237445b80efdc1dae7bfcea976fd1 Mon Sep 17 00:00:00 2001 From: Albin Ekblom Date: Wed, 19 Jul 2017 14:54:26 +0300 Subject: [PATCH] Added HTTPS example (#264) --- examples/with-https/index.js | 21 +++++++++++++++++++++ examples/with-https/package.json | 13 +++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/with-https/index.js create mode 100644 examples/with-https/package.json diff --git a/examples/with-https/index.js b/examples/with-https/index.js new file mode 100644 index 0000000..fc0a0bc --- /dev/null +++ b/examples/with-https/index.js @@ -0,0 +1,21 @@ +const https = require('https') +const { run, send } = require('micro') + +const cert = require('openssl-self-signed-certificate') + +const PORT = process.env.PORT || 3443 + +const options = { + key: cert.key, + cert: cert.cert, + passphrase: cert.passphrase +} + +const microHttps = fn => https.createServer(options, (req, res) => run(req, res, fn)) + +const server = microHttps(async (req, res) => { + send(res, 200, { encrypted: req.client.encrypted }) +}) + +server.listen(PORT) +console.log(`Listening on https://localhost:${PORT}`) diff --git a/examples/with-https/package.json b/examples/with-https/package.json new file mode 100644 index 0000000..4cdc3bd --- /dev/null +++ b/examples/with-https/package.json @@ -0,0 +1,13 @@ +{ + "name": "with-https", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node ." + }, + "dependencies": { + "micro": "latest", + "openssl-self-signed-certificate": "^1.1.6" + } +}