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

后端 未结 19 1088
眼角桃花
眼角桃花 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:45

    You can edit your package.json scripts section to read:

    "scripts": { "start": "set HTTPS=true&&react-scripts start", ... }

    or just run set HTTPS=true&&npm start

    Just a sidenote, for me, making this change breaks hot reloading for some reason....

    -- Note: OS === Windows 10 64-Bit

    0 讨论(0)
  • 2020-12-24 10:45

    I could not get that to work (setting HTTPS=true), instead, i used

    react-https-redirect

    A simple wrapper around your App component.

    0 讨论(0)
  • 2020-12-24 10:46

    I am using Windows 10 latest build with Windows Insider Program till this date.

    It seems like there are three cases while using Windows 10:

    1. Windows 10 with CMD command line for your NPM
    set HTTPS=true&&npm start
    
    1. Windows 10 with PowerShell command line for your NPM
    set HTTPS=true&&npm start
    
    1. Windows 10 with Linux Bash command line for your NPM ( My Case was this )
    HTTPS=true npm start
    

    Documentation: Create react app dev

    0 讨论(0)
  • 2020-12-24 10:51

    Edit your package.json file and change the starting scripts for starting your secures domain. for example https

     {
      "name": "social-login",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "react": "^16.4.1",
        "react-dom": "^16.4.1",
        "react-facebook-login": "^4.0.1",
        "react-google-login": "^3.2.1",
        "react-scripts": "1.1.4"
      },
      "scripts": {
        // update this line "start": "HTTPS=true react-scripts start",
        "start": "set HTTPS=true&&react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject"
      }
    }
    

    thanks

    0 讨论(0)
  • 2020-12-24 10:54

    might need to Install self-signed CA chain on both server and browser. Difference between self-signed CA and self-signed certificate

    0 讨论(0)
  • 2020-12-24 10:55

    I think it is worth to mention to set PORT=443, default HTTPS standard port. You can avoid to attach :PORT at the end of the address every time.

    My package.json is like (tested from Ubuntu Server 18.04):

    {
      ...
      "scripts": {
        "start": "HTTPS=true PORT=443 react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      },
      ...
    }
    
    0 讨论(0)
提交回复
热议问题