const https = require('https'); const http = require('http'); const fs = require('fs'); const hostname = '0.0.0.0'; //not 127.0.0.1 that is localhost const port = 443; const domain ='omarkhan.ca' const options = { key: fs.readFileSync('/home/omar/_.omarkhan.ca_private_key.key'), cert: fs.readFileSync('/home/omar/omarkhan.ca_ssl_certificate.cer') }; https.createServer(options, function (req, res) { res.writeHead(200); res.end("hello world secured\n"); }).listen(port, hostname); // Redirect from http port 80 to https http.createServer(function (req, res) { res.writeHead(301, { "Location": "https://" + domain }); res.end(); }).listen(80); //run using sudo node index.js