So I have a basic NodeJS server which I am trying to convert from HTTP to HTTPS.
Here is the old code (HTTP) which works:
const express = require('express');
const port = 80;
const app = express();
app.listen(port, () => console.log('listening on ' + port));
app.use(express.static('public'));
app.use(express.json({'limit':'2mb'}));
I've been looking up how to convert this to HTTPS. I have tried many attempts but I just cant seem to get this working. Below is my current state of code.
New code (HTTPS) does not work:
const express = require('express');
const https = require('https');
const fs = require('fs');
const port = 443;
var key = fs.readFileSync('./private/privkey.pem');
var cert = fs.readFileSync('./private/fullchain.pem');
var options = {
key: key,
cert: cert
};
var app = express();
https.createServer(options, app).listen(port, () => console.log('listening on ' + port));
app.use(express.static('public'));
app.use(express.json({'limit' : '2mb'}));
The port does seem to open and listen but whenever I try to connect via browser, I get the error:
"<ip> didn’t send any data."
ERR_EMPTY_RESPONSE
I have tried omitting the express app from the https server and replacing with
function (req, res) { res.send('hello!') }
But this did not make any difference in the error.
I am truly stuck now as I have checked so many forums and I don't know what I'm doing wrong.
EDIT:
I am not getting any syntax errors from running index.js