Configure HTTPS for React applications running locally

Configure HTTPS for React applications running locally

If you build an application with create-react-app and run it locally, the application will be served using the HTTP protocol by default. Applications running in a production environment are served using HTTPS.

Although configuring HTTPS is pretty straightforward, configuring it locally is a bit more complicated.

[[346336]]

As we all know, the create-react-app program is run with npm run start (or simply npm start). In the scripts section of the package.json file, there is a line of configuration:

  1. "start": "react-scripts start"

Change it to:

  1. "start": " HTTPS = true react-scripts start"

Set the value of the environment variable HTTPS to true.

This is not enough, you also need to generate a local certificate.

Note: The following commands are executed on macOS, and can be operated in the same way on Linux. However, there is no guarantee that they will work successfully on Windows.

In the project root directory, run:

  1. openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365

Then run:

  1. openssl rsa -in keytmp.pem -out key.pem

Two files, cert.pem and key.pem, will be generated in the current directory.

Now change the start script in the package.json file to:

  1. "start": "export HTTPS = true && SSL_CRT_FILE = cert .pem&& SSL_KEY_FILE = key .pem react-scripts start",

Now run npm run start and access the React application. You should see the following warning message:

This is because the default browser does not trust the local certificate.

Click on the little "Not Secure" icon near the address bar and a small panel will appear:

Click on Certificate and you will see another panel with the certificate details:

Now drag the certificate icon to the desktop or a folder with the mouse, and you will get the certificate file. Then double-click the certificate to install it and set it as "Trusted Certificate or Issuing Authority".

After the setup is complete, you can access your React application locally via HTTPS:

<<:  Huawei's Ryan Ding: Working together to create the most successful 5G

>>:  WeChat PC version 3.0 official version detailed experience: friends' permissions are more convenient to view

Recommend

Why does 5G need edge computing (MEC)?

[[354637]] This article is reprinted from the WeC...

How Wi-Fi 6, WWAN and 5G make fully wireless office possible

For use cases, fully wireless connectivity for of...

Let's talk about Ocelot gateway using IdentityServer4 authentication

[[387801]] This article is reprinted from the WeC...

How to solve VirtualBox bridged networking problems?

【51CTO.com Quick Translation】Let's assume tha...

Why does TCP use three-way handshake? Can't two or four-way handshakes work?

The TCP protocol needs three handshakes to establ...

How Do PoE Switches Work?

All modern industrial equipment uses long cables ...

What problems does each generation of HTTP protocol solve?

Recently, I briefly studied the development histo...

Node.js knowledge - How to set cookie information in HTTP request and response

[[398674]] HTTP Cookie[1] is a small piece of dat...