简体   繁体   English

create-react-app:如何使用 https 而不是 http?

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

I was wondering if anyone knows how to use https on dev for the 'create-react-app' environment.我想知道是否有人知道如何在 dev 上为“create-react-app”环境使用 https。 I can't see anything about that in the README or quick googling.我在自述文件或快速谷歌搜索中看不到任何相关信息。 I just want either the https://localhost:3000 to work, or else https://localhost:3001 .我只想让https://localhost:3000工作,或者让 https://localhost:3001工作。

Set HTTPS=true before you run the start command.在运行 start 命令之前设置HTTPS=true

Documentation文档

The implementation uses the HTTPS Environment Variable to determine which protocol to use when starting the server.该实现使用HTTPS 环境变量来确定启动服务器时使用的协议

You can edit your package.json scripts section to read:您可以编辑 package.json 脚本部分以阅读:

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

or just run set HTTPS=true&&npm start或者直接运行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 -- 注意:操作系统 === Windows 10 64 位

You can also create a file called .env in the root of your project, then write你也可以在你的项目根目录下创建一个名为 .env 的文件,然后写

HTTPS=true

After that, just run "npm start" as you usually do to start your app.之后,只需像平常一样运行“npm start”即可启动您的应用程序。

Docs: https://facebook.github.io/create-react-app/docs/advanced-configuration文档: https : //facebook.github.io/create-react-app/docs/advanced-configuration

Works both on Linux and Windows , unlike some other answers posted here.与此处发布的其他一些答案不同,适用于 Linux 和 Windows

In Case of MAC/UNIX do在 MAC/UNIX 的情况下做

export HTTPS=true
npm start

Or simple one liner或简单的一个班轮

export HTTPS=true&&npm start

Or update start script in package.json to或者将 package.json 中的启动脚本更新为

"start": "export HTTPS=true&&PORT=3000 react-scripts start",

you should be able to hit https.你应该能够点击https。

set HTTPS=true&&react-scripts start in scripts > start: of package.json as shown below. set HTTPS=true&&react-scripts start in scripts > start: of package.json 如下所示。

"scripts" in package.json : 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 ie, HTTPS=true && npm start won't work.请不要在命令之间留任何空格,即HTTPS=true && npm start不起作用。

Refer it in official doc.参考官方文档。 Using HTTPS in Development 在开发中使用 HTTPS

(Note: the lack of whitespace is intentional.) (注意:缺少空格是有意的。)

Windows (cmd.exe) Windows (cmd.exe)

set HTTPS=true&&npm start

(Note: the lack of whitespace is intentional.) (注意:缺少空格是有意的。)

Windows (Powershell) Windows (Powershell)

($env:HTTPS = "true") -and (npm start)

Linux, macOS (Bash) 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.请注意,服务器将使用自签名证书,因此您的 Web 浏览器几乎肯定会在访问该页面时显示警告。

Custom SSL certificate自定义 SSL 证书

HTTPS=true SSL_CRT_FILE=<SSLCert.crt> SSL_KEY_FILE=<SSLCert.key> npm start

Linux, macOS (Bash) Linux、macOS (Bash)

HTTPS=true SSL_CRT_FILE=<SSLCert.crt> SSL_KEY_FILE=<SSLCert.key> npm start

To avoid doing it each time: You can include in the npm start script like so:为了避免每次都这样做:您可以像这样在 npm start 脚本中包含:

{
  "start": "HTTPS=true react-scripts start"
}

Or you can create a .env file with HTTPS=true或者您可以使用HTTPS=true创建一个.env文件

请在命令提示符中使用它

 set HTTPS=true&&npm start

if it's still not working properly because of "your connection is not private" issues (in chrome), this worked for me just fine:如果它仍然无法正常工作,因为“您的连接不是私人的”问题(在 chrome 中),这对我来说很好用:

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

In short:简而言之:

  1. First I exported certificate from chrome (view this).首先,我从 chrome 导出了证书(查看这个)。
  2. Imported the certificate into Windows (using certmgr.msc).将证书导入 Windows(使用 certmgr.msc)。
  3. Allowed chrome://flags/#allow-insecure-localhost flag.允许 chrome://flags/#allow-insecure-localhost 标志。 How to allow insecure localhost 如何允许不安全的本地主机
"scripts": {
"start": "set HTTPS=true&&set PORT=443&&react-scripts start",
........
}

In case you need to change the port and set it to https.如果您需要更改端口并将其设置为 https。

might need to Install self-signed CA chain on both server and browser.可能需要在服务器和浏览器上安装自签名 CA 链。 Difference between self-signed CA and self-signed certificate 自签名CA和自签名证书的区别

I think it is worth to mention to set PORT=443 , default HTTPS standard port.我认为值得一提的是设置PORT=443 ,默认HTTPS标准端口。 You can avoid to attach :PORT at the end of the address when browsing every time.您可以避免每次浏览时在地址末尾附加:PORT

su
export HTTPS=true 
export PORT=443
export SSL_CRT_FILE=/PATH/TO/cert.pem     # recommended
export SSL_KEY_FILE=/PATH/TO/privkey.pem  # recommended
npm start

Or要么

you can put them all in to package.json :您可以将它们全部放入package.json

  "scripts": {
    "start": "HTTPS=true PORT=443 react-scripts start",

Then, without export ing:然后,没有export ing:

su
npm start

You can create a proxy.HTTPS->HTTP你可以创建一个代理。HTTPS->HTTP

Create a key and cert.创建密钥和证书。

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

Create a file named proxyServer.js创建一个名为 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

I`m using Windows 10 and I had the same issue.我正在使用Windows 10 ,我遇到了同样的问题。 I realized that you need to:我意识到你需要:

  1. Run Command Prompt with Administrator Privileges使用管理员权限运行命令提示符
  2. Run on the terminal bash this command: set HTTPS=true&&npm start在终端 bash 上运行这个命令: set HTTPS=true&&npm start
  3. You can also put this code into your package.json file under the scripts section like this:您还可以将此代码放入脚本部分下的package.json文件中,如下所示:

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

  4. Bonus: If you want to change the PORT use this command insted:奖励:如果您想更改端口,请使用以下命令:

    set HTTPS=true&&set PORT=443&&react-scripts start

    Obs.: Pay attention to the blank spaces NOT left in between some characters.观察:注意某些字符之间没有留下空格。

You can browse this link for more detais.您可以浏览此链接以获取更多详细信息。

Edit your package.json file and change the starting scripts for starting your secures domain .编辑 package.json 文件并更改启动安全的启动脚本。 for example https例如 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谢谢

HTTPS=true npm start

在终端为我工作的 Create-React-App

I could not get that to work (setting HTTPS=true), instead, i used我无法让它工作(设置 HTTPS=true),相反,我使用了

react-https-redirect反应-https-重定向

A simple wrapper around your App component.围绕您的App组件的简单包装器。

In Windows environment add following lines to package.json:在 Windows 环境中,将以下几行添加到 package.json:

  "scripts": {
    "start-dev": "set HTTPS=true&&set PORT=443&&react-scripts start"
  },

It will start development server with https and port 443. At the present moment NodeJs have known bug to run this correctly but it worked with nodeJs v8.11.3 - https://nodejs.org/dist/v8.11.3/node-v8.11.3-x64.msi for me.它将使用 https 和端口 443 启动开发服务器。目前 NodeJs 知道错误可以正确运行它,但它可以与 nodeJs v8.11.3 - https://nodejs.org/dist/v8.11.3/node-v8一起使用。 11.3-x64.msi对我来说。

I am using Windows 10 latest build with Windows Insider Program till this date.到目前为止,我正在使用带有 Windows Insider Program 的Windows 10最新版本。

It seems like there are three cases while using Windows 10:使用 Windows 10 时似乎有三种情况:

  1. Windows 10 with CMD command line for your NPM Windows 10 带有用于 NPM 的 CMD 命令行
set HTTPS=true&&npm start
  1. Windows 10 with PowerShell command line for your NPM Windows 10 带有用于 NPM 的 PowerShell 命令行
set HTTPS=true&&npm start
  1. Windows 10 with Linux Bash command line for your NPM ( My Case was this )带有 Linux Bash 命令行的 Windows 10 用于您的 NPM(我的案例是这样)
HTTPS=true npm start

Documentation: Create react app dev文档: 创建 react app dev

To avoid untrusted certificate errors in Chrome and Safari you should manually specify a self-signed key pair.为避免 Chrome 和 Safari 中出现不受信任的证书错误,您应该手动指定自签名密钥对。 CRA allows you to specify them . CRA 允许您指定它们

Also, use .env file to store these vars.此外,使用.env文件来存储这些变量。

On macOS, just add your certificate to Keychain Access and then set Trust Always in its details.在 macOS 上,只需将您的证书添加到 Keychain Access,然后在其详细信息中设置Trust Always

Open the package.json file and change the start script file like given below.打开 package.json 文件并更改启动脚本文件,如下所示。

"start": "react-scripts start",

to

"start": "HTTPS=true react-scripts start",

Restart your localhost and check the terminal you are probably able to see the local and on your.network runs by HTTPS.重新启动您的本地主机并检查您可能能够看到本地和在您的网络上运行的终端 HTTPS。

For Windows, try this one对于 Windows,试试这个

($env:HTTPS = "true") -and (npm start)

I am using VS Code Terminal (powershell).我正在使用 VS Code 终端(powershell)。

Important point about this issue: if you are looking to use https on LAN (rather than localhost) then SSL certification is an issue because the IP is not static!关于这个问题的重要一点:如果您希望在 LAN(而不是 localhost)上使用 https,那么 SSL 认证是一个问题,因为 IP 不是静态的!

This is a nice read on the subject where they explore the option of doing it anyway: SSL For Devices in Local Networks这是一个很好的阅读主题,他们探索了无论如何都要这样做的选项: SSL For Devices in Local Networks

添加到文件 .env(或 .env.local)行:HTTPS=true

// add this line to android manifest to use http when releasing apk in react native // 将此行添加到 android 清单中,以便在 react native 中发布 apk 时使用 http

<application android:usesCleartextTraffic="true">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM