diff --git a/serve.js b/serve.js index 844bfb0..251cbfe 100755 --- a/serve.js +++ b/serve.js @@ -58,6 +58,9 @@ function createServer(port, pubdir, content, opts) { var directive = { public: pubdir, content: content, livereload: opts.livereload , servername: opts.servername, expressApp: opts.expressApp }; + var redirectApp = require('redirect-https')({ + port: port + }); server.on('error', function (err) { if (opts.errorPort || opts.manualPort) { @@ -95,13 +98,7 @@ function createServer(port, pubdir, content, opts) { server.on('request', function (req, res) { if (!req.socket.encrypted) { - res.statusCode = 301; - res.setHeader( - 'Location' - , 'https://' + (req.headers.host || 'localhost') - + (httpsPort === opts.port ? '' : ':' + opts.port) - ); - res.end(); + redirectApp(req, res); return; } @@ -270,7 +267,7 @@ function run() { return promise.then(function (matchingIps) { if (matchingIps) { if (!matchingIps.length) { - console.log("Neither the attached nor external interfaces match '" + argv.servername + "'"); + console.info("Neither the attached nor external interfaces match '" + argv.servername + "'"); } } opts.matchingIps = matchingIps || []; diff --git a/stages/01-serve.js b/stages/01-serve.js new file mode 100644 index 0000000..ebb483d --- /dev/null +++ b/stages/01-serve.js @@ -0,0 +1,23 @@ +'use strict'; + +var https = require('httpolyglot'); +var httpsOptions = require('localhost.daplie.com-certificates').merge({}); +var httpsPort = 8443; +var redirectApp = require('redirect-https')({ + port: httpsPort +}); + +var server = https.createServer(httpsOptions); + +server.on('request', function (req, res) { + if (!req.socket.encrypted) { + redirectApp(req, res); + return; + } + + res.end("Hello, Encrypted World!"); +}); + +server.listen(httpsPort, function () { + console.log('https://' + 'localhost.daplie.com' + (443 === httpsPort ? ':' : ':' + httpsPort)); +});