DEV Community

Discussion on: Using Create-React-App with Express

Collapse
 
lisa profile image
Lisa • Edited

Hey,

Great article! I would like to know how to set up HTTPS here.

I tried creating a self-signed certificate following the steps below,

openssl genrsa -out key.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
rm csr.pem

I'm adding the following code in my server.js

var express = require("express");
var https = require("https");
var fs = require("fs");
const path = require('path');
var app = express();​
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var server = https.createServer(options, app);​
app.use(express.static(__dirname));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build'));
});
server.listen(8000);

But this doesn't seem to work as when I try to open https://localhost:3000, it throws a "The site can't provide a secure connection". Could you please point out where exactly I am going wrong?