更新:见下面安迪的回答。在最近的版本中,您应该设置环境变量来配置证书
SSL_CRT_FILE=.cert/server.crt
SSL_KEY_FILE=.cert/server.key
create-react-app
不建议弹出,因为您将无法无缝升级它。此外,您可以轻松拥有有效的 SSL 证书而无需弹出。
您需要将证书复制到node_modules/webpack-dev-server/ssl/server.pem
. 缺点是您需要手动复制文件。但是,使这种无缝连接的一种方法是添加一个postinstall
创建符号链接的脚本。这是我创建的脚本:
#!/bin/bash
# With create-react-app, a self signed (therefore invalid) certificate is generated.
# 1. Create some folder in the root of your project
# 2. Copy your valid development certificate to this folder
# 3. Copy this file to the same folder
# 4. In you package.json, under `scripts`, add `postinstall` script that runs this file.
# Every time a user runs npm install this script will make sure to copy the certificate to the
# correct location
TARGET_LOCATION="./node_modules/webpack-dev-server/ssl/server.pem"
SOURCE_LOCATION=$(pwd)/$(dirname "./local-certificate/server.pem")/server.pem
echo Linking ${TARGET_LOCATION} TO ${SOURCE_LOCATION}
rm -f ${TARGET_LOCATION} || true
ln -s ${SOURCE_LOCATION} ${TARGET_LOCATION}
chmod 400 ${TARGET_LOCATION} # after 30 days create-react-app tries to generate a new certificate and overwrites the existing one.
echo "Created server.pem symlink"
你package.json
应该看起来像:
"scripts": {
...
"postinstall": "sh ./scripts/link-certificate.sh"
}