24
loading...
This website collects cookies to deliver better user experience
create-react-app
and if you're running it locally its served using HTTP. A production application should run in HTTPS (SSL) for security reasons, in some cases a local version has to run on https for authenticating users on the backend, when using AzureB2C, or using the proxy feature or similar. Let's see how this can be achieved.[email protected]
. To enable HTTPS you have to set the environment variable to true, then start the dev server as usual with npm start
.set HTTPS=true&&npm start
($env:HTTPS = "true") -and (npm start)
HTTPS=true npm start
create-react-app ssl-test
, and after running the command (linux) the output looks like this:$ HTTPS=true npm start
> [email protected] start C:\dev\ssl-test
> react-scripts start
i 「wds」: Project is running at https://123.123.123.123
i 「wds」: webpack output is served from
i 「wds」: Content not from webpack is served from C:\dev\ssl-test\public
i 「wds」: 404s will fallback to /
Starting the development server...
Compiled successfully!
You can now view ssl-test in the browser.
Local: https://localhost:3000
On Your Network: https://123.123.123.123
Note that the development build is not optimized.
To create a production build, use yarn build.
SSL_CERT_FILE
and SSL_KEY_FILE
to the generated files.mkcert -install
.mkcert localhost
.set HTTPS=true&&set SSL_CRT_FILE={CERT-PATH}&&set SSL_KEY_FILE={KEY-PATH}&&react-scripts start
HTTPS=true SSL_CRT_FILE={CERT-PATH} SSL_KEY_FILE={KEY-PATH} react-scripts start
CERT-PATH
and KEY-PATH
are the paths to the generated files. Eventually, we set the command as the npm start
script, and the application runs on HTTPS."scripts": {
"start": "HTTPS=true SSL_CRT_FILE={CERT-PATH} SSL_KEY_FILE={KEY-PATH} react-scripts start"
}