diff --git a/example.js b/example.js index 72d7d10..d40640b 100644 --- a/example.js +++ b/example.js @@ -4,7 +4,6 @@ // // Configure LetsEncrypt // -var LEX = require('letsencrypt-express').testing(); var leConfDir = require('os').homedir() + '/letsencrypt/etc'; throw new Error( @@ -12,18 +11,20 @@ throw new Error( + " Also, you'll need to remove .testing() and rm -rf '" + leConfDir + "'" + " to get actual, trusted production certificates."); -var lex = LEX.create({ - configDir: leConfDir - // leave `null` to disable automatic registration -, approveRegistration: function (hostname, cb) { - // Note: this is the place to check your database - // to get the user associated with this domain - cb(null, { - domains: [hostname] - , email: 'CHANGE_ME' // user@example.com - , agreeTos: true - }); +var le = require('letsencrypt-express').create({ + server: 'staging' // in production use https://acme-v01.api.letsencrypt.org/directory + +, configDir: require('os').homedir() + '/letsencrypt/etc' + +, approveDomains: function (opts, certs, cb) { + opts.domains = certs && certs.altnames || opts.domains; + opts.email = 'john.doe@example.com' // CHANGE ME + opts.agreeTos = true; + + cb(null, { options: opts, certs: certs }); } + + , debug: true }); @@ -33,10 +34,11 @@ var lex = LEX.create({ var hapi = require('hapi'); var https = require('spdy'); var server = new hapi.Server(); -var acmeResponder = LEX.createAcmeResponder(lex); -var httpsServer = https.createServer(lex.httpsOptions).listen(443); +var acmeResponder = le.middleware(); +var httpsServer = https.createServer(le.httpsOptions).listen(443); server.connection({ listener: httpsServer, autoListen: false, tls: true }); + server.route({ method: 'GET' , path: '/.well-known/acme-challenge' @@ -62,8 +64,8 @@ server.route({ // Redirect HTTP to HTTPS // var http = require('http'); -http.createServer(LEX.createAcmeResponder(lex, function redirectHttps(req, res) { - res.setHeader('Location', 'https://' + req.headers.host + req.url); - res.statusCode = 302; - res.end(''); -})).listen(80); +var redirectHttps = require('redirect-https')(); + +http.createServer(le.middleware(redirectHttps)).listen(80, function () { + console.log('handle ACME http-01 challenge and redirect to https'); +});