This commit is contained in:
AJ ONeal 2016-10-07 11:25:31 -06:00
commit e081f59686
2 changed files with 29 additions and 8 deletions

View File

@ -92,6 +92,9 @@ function createServer(port, pubdir, content, opts) {
var directive = { public: pubdir, content: content, livereload: opts.livereload var directive = { public: pubdir, content: content, livereload: opts.livereload
, servername: opts.servername, expressApp: opts.expressApp }; , servername: opts.servername, expressApp: opts.expressApp };
var redirectApp = require('redirect-https')({
port: port
});
// returns an instance of node-letsencrypt with additional helper methods // returns an instance of node-letsencrypt with additional helper methods
var webrootPath = require('os').tmpdir(); var webrootPath = require('os').tmpdir();
@ -159,13 +162,7 @@ function createServer(port, pubdir, content, opts) {
server.on('request', function (req, res) { server.on('request', function (req, res) {
console.log('[' + req.method + '] ' + req.url); console.log('[' + req.method + '] ' + req.url);
if (!req.socket.encrypted) { if (!req.socket.encrypted) {
res.statusCode = 301; redirectApp(req, res);
res.setHeader(
'Location'
, 'https://' + (req.headers.host || 'localhost')
+ (httpsPort === opts.port ? '' : ':' + opts.port)
);
res.end();
return; return;
} }
@ -197,7 +194,8 @@ function run() {
var tls = require('tls'); var tls = require('tls');
// letsencrypt // letsencrypt
var cert = require('localhost.daplie.com-certificates'); var cert = require('localhost.daplie.com-certificates').merge({});
var opts = { var opts = {
agreeTos: argv.agreeTos || argv['agree-tos'] agreeTos: argv.agreeTos || argv['agree-tos']
, debug: argv.debug , debug: argv.debug

23
stages/01-serve.js Normal file
View File

@ -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));
});