How to start React project with HTTPS

ⅰ亾dé卋堺 提交于 2020-08-10 19:11:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!