小编gam*_*est的帖子

如何通过 https 在生产中部署 React 应用程序?

我有一个 React 应用程序,它使用 .env.production 文件在 HTTPS 上完美运行,其中包含:

HTTPS=true
SSL_CRT_FILE=/etc/ssl/certs/mycert.crt
SSL_KEY_FILE=/etc/ssl/private/mykey.key
Run Code Online (Sandbox Code Playgroud)

package.json 文件包含:

"scripts": {
   "start": "env-cmd -f .env.production react-scripts start",
   "build:production": "env-cmd -f .env.production react-scripts build"
}
Run Code Online (Sandbox Code Playgroud)

当我做:

npm start
Run Code Online (Sandbox Code Playgroud)

一切正常,我可以从任何地方通过 HTTPS 访问我的服务器。现在,这很有趣,现在我做:

npm run build:production
Run Code Online (Sandbox Code Playgroud)

这也很好用,我得到:

The build folder is ready to be deployed.
You may serve it with a static server:

serve -s build
Run Code Online (Sandbox Code Playgroud)

现在,这是失败的:

1)当我使用上面的serve命令时,.env.production文件中的环境变量没有被拾取!为什么?

2)如果我手动传递环境变量,如下所示:

HTTPS=true SSL_CRT_FILE=/etc/ssl/certs/mycert.crt SSL_KEY_FILE=/etc/ssl/private/mykey.key PORT=8580 serve -s build 
Run Code Online (Sandbox Code Playgroud)

似乎只选择了 PORT 变量,但服务器现在在 HTTP 上运行。任何想法为什么?!

https production environment-variables node.js reactjs

2
推荐指数
1
解决办法
3127
查看次数