create-react-app: how to use https instead of http?

后端 未结 19 1122
眼角桃花
眼角桃花 2020-12-24 10:04

I was wondering if anyone knows how to use https on dev for the \'create-react-app\' environment. I can\'t see anything about that in the README or quick googling. I just wa

相关标签:
19条回答
  • 2020-12-24 10:59

    Set HTTPS=true before you run the start command.

    Documentation

    The implementation uses the HTTPS Environment Variable to determine which protocol to use when starting the server.

    0 讨论(0)
  • 2020-12-24 11:01

    set HTTPS=true&&react-scripts start in scripts > start: of package.json as shown below.

    "scripts" in package.json:

    "scripts": {
        "start": "set HTTPS=true&&react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      },
    
    • Please don't leave any space in between the commands i.e, HTTPS=true && npm start won't work.

    Refer it in official doc. Using HTTPS in Development

    (Note: the lack of whitespace is intentional.)

    0 讨论(0)
  • 2020-12-24 11:01
    HTTPS=true npm start
    

    in the terminal worked for me on Create-React-App

    0 讨论(0)
  • 2020-12-24 11:03

    if it's still not working properly because of "your connection is not private" issues (in chrome), this worked for me just fine:

    https://github.com/facebook/create-react-app/issues/3441

    In short:

    1. First I exported certificate from chrome (view this).
    2. Imported the certificate into Windows (using certmgr.msc).
    3. Allowed chrome://flags/#allow-insecure-localhost flag. How to allow insecure localhost
    0 讨论(0)
  • 2020-12-24 11:04

    You can create a proxy.HTTPS->HTTP

    Create a key and cert.

    openssl req -nodes -new -x509 -keyout server.key -out server.cert

    Create a file named proxyServer.js

    var httpProxy = require('http-proxy');
    let fs = require('fs');
    
    httpProxy.createServer({
        target: {
          host: 'localhost',
          port: 3000
        },
        ssl: {
          key: fs.readFileSync('server.key', 'utf8'),
          cert: fs.readFileSync('server.cert', 'utf8')
        }
      }).listen(3000);
    

    From the terminal run

    node proxyServer.js

    0 讨论(0)
  • 2020-12-24 11:05
    "scripts": {
    "start": "set HTTPS=true&&set PORT=443&&react-scripts start",
    ........
    }
    

    In case you need to change the port and set it to https.

    0 讨论(0)
提交回复
热议问题