const fs = require('fs');
const https = require('https');
const url = require('url');
const mime = require('mime-types');

const httpsOptions = {
  "enable": true,
  'key': fs.readFileSync('https/localhost.key'),
  'cert':  fs.readFileSync('https/localhost.pem'),
  "passphrase":   "abcd" 
}

server = https.createServer(httpsOptions, (req, res) => {
  console.log("Request");
  let parsedURL = url.parse(req.url, true);
  let path = parsedURL.path.replace(/^\/+|\/+$/g,"");
  if(path == ""){ path = "index.html"; }


  let file = __dirname + "/public/" + path;
  if(path.indexOf("node_modules/")!= -1){
    console.warn("Pfad muss auf Module umgehängt werden: " + path);
    file = __dirname + "/" + path;
  }
  
 
  console.log("file : " + file);
  fs.readFile(file, function(err, content){
  if(err){
      console.log("File Not found "+ file);
      res.writeHead(404);
      res.end();
  } else {
    console.log("test");
      res.setHeader('X-Content-Type-Option', 'nosniff');
      let mimeT = mime.lookup(path);
      res.writeHead(200,{'Content-type': mimeT});      
      res.end(content);
  }
  })
});


server.listen(1003);
console.log("WebServer auf https://localhost:1003")
