#!/usr/bin/env node

"use strict";
var http = require('http'),
    https = require('https'),
   send = require('send'),
   fs = require('fs'),
   url = require('url');
   
   const options = {
  key: fs.readFileSync('/etc/letsencrypt/live/portacode.win/privkey.pem'),
  cert: fs.readFileSync('/etc/letsencrypt/live/portacode.win/fullchain.pem')
};

var app = https.createServer(options, function(req, res) {
   function error(err) {
      res.statusCode = err.status || 500;
      console.log('%s: %s', err.status, req.url);
      res.end(err.message);
   }

   function redirect() {
      res.statusCode = 301;
      res.setHeader('Location', req.url + '/');
      res.end('Redirecting to ' + req.url + '/');
   }

   send(req, url.parse(req.url).pathname, {
         root: process.cwd()
      })
      .on('error', error)
      .on('directory', redirect)
      .on('file', function() {
         console.log('%s: %s', res.statusCode, req.url);
      })
      .pipe(res);
});

var port = process.argv[2] || 3333;

console.log('HTTP Static Listening on port %s', port);

app.on('error', function(e) {
   if (e.code === 'EADDRINUSE') {
      console.log('Address in use, incrementing and retrying...');
      setTimeout(function() {
         //         app.close();
         port++;
         app.listen(port, function() {
            console.log('HTTP Static Listening on port %s', port);
         });
      }, 1000);
   }
});

app.listen(port);

@@ -14,17 +14,116 @@
 v node%0A%0A
-%22
+// EDIT THESE FOR SSL SUPPORT%0Aconst KEY_PATH = './ privkey.pem';%0Aconst CERT_PATH = './cert.pem';%0A%0A('
 use stri
@@ -128,9 +128,10 @@
 rict
-%22
+')
 ;%0Ava
@@ -156,18 +156,16 @@
 http'),%0A
-  
   https 
@@ -184,17 +184,16 @@
 ttps'),%0A
- 
   send =
@@ -212,17 +212,16 @@
 nd'),%0A  
- 
 fs = req
@@ -232,17 +232,16 @@
 ('fs'),%0A
- 
   url = 
@@ -260,15 +260,9 @@
 ');%0A
-   %0A   
+%0A
 cons
@@ -278,15 +278,45 @@
  = %7B
-%0A 
+%7D;%0Alet server = %7B%7D;%0Atry %7B%0A  let
  key
-:
+ =
  fs.
@@ -332,139 +332,158 @@
 ync(
-'/etc/letsencrypt/live/portacode.win/privkey.pem'),%0A  cert: fs.readFileSync('/etc/letsencrypt/live/portacode.win/fullchain.pem')
+KEY_PATH);%0A  let cert = fs.readFileSync(CERT_PATH);%0A  options.key = key;%0A  options.cert = cert;%0A  server = https;%0A%7D catch (error) %7B%0A  server = http;
 %0A%7D
-;
 %0A%0Ava
@@ -490,21 +490,22 @@
 r app = 
-https
+server
 .createS
@@ -540,17 +540,16 @@
  res) %7B%0A
- 
   functi
@@ -560,34 +560,32 @@
 rror(err) %7B%0A    
-  
 res.statusCode =
@@ -604,18 +604,16 @@
 %7C%7C 500;%0A
-  
     cons
@@ -648,26 +648,24 @@
 , req.url);%0A
-  
     res.end(
@@ -680,21 +680,19 @@
 age);%0A  
- 
 %7D%0A%0A
- 
   functi
@@ -707,26 +707,24 @@
 ect() %7B%0A    
- 
- 
 res.statusCo
@@ -733,18 +733,16 @@
  = 301;%0A
-  
     res.
@@ -783,18 +783,16 @@
 ');%0A    
-  
 res.end(
@@ -829,21 +829,19 @@
 '/');%0A  
- 
 %7D%0A%0A
- 
   send(r
@@ -879,21 +879,16 @@
 , %7B%0A    
-   
-  
 root: pr
@@ -898,28 +898,23 @@
 ss.cwd()
+,
 %0A
-    
   %7D)%0A
-  
     .on(
@@ -929,26 +929,24 @@
  error)%0A    
- 
- 
 .on('directo
@@ -960,18 +960,16 @@
 direct)%0A
-  
     .on(
@@ -991,27 +991,24 @@
 n() %7B%0A      
-   
 console.log(
@@ -1051,19 +1051,15 @@
     
-  
 %7D)%0A    
- 
- 
 .pip
@@ -1196,17 +1196,16 @@
 (e) %7B%0A  
- 
 if (e.co
@@ -1223,26 +1223,24 @@
 DRINUSE') %7B%0A
-  
     console.
@@ -1296,18 +1296,16 @@
 ');%0A    
-  
 setTimeo
@@ -1326,19 +1326,16 @@
 %7B%0A      
- 
-  
 //      
@@ -1356,19 +1356,16 @@
 ;%0A      
-   
 port++;%0A
@@ -1366,27 +1366,24 @@
 rt++;%0A      
- 
-  
 app.listen(p
@@ -1400,20 +1400,16 @@
 ion() %7B%0A
-    
         
@@ -1473,17 +1473,12 @@
     
-   
 %7D);%0A
-  
     
@@ -1487,17 +1487,16 @@
  1000);%0A
- 
   %7D%0A%7D);%0A

add https support with http fallback