问题
I'm trying to start my React project with HTTPS instead of HTTP. I have my project in an Ubuntu Server and I want to try it with HTTPS. I have created a SSL certificate with Certbot for my domain and I want to use it in React.
I have tried to modify the file Server.js
in node_module/webpack-dev-server/lib/Server.js
as follows:
// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/adan.appha.es/privkey1.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/adan.appha.es/cert1.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/adan.appha.es/fullchain1.pem', 'utf8');
and change:
this.listeningApp = require('spdy').createServer(options.https, app);
to
this.listeningApp = https.createServer(credentials, app);
Then do npm start
and write in my browser https://mydomain:3000 but it still doesn't work.
回答1:
You can set HTTPS in development as below.
Windows (cmd.exe)
set HTTPS=true&&npm start
Windows (Powershell)
($env:HTTPS = "true") -and (npm start)
Linux, macOS (Bash)
HTTPS=true npm start
Note that the server will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page.
来源:https://stackoverflow.com/questions/60150518/how-to-start-react-project-with-https