我有一个用create-react-app启动的项目,我想在实例上部署它。目前我尝试了两种设置部署的方法,但只能设置端口而无法设置主机。
我的项目架构如下:
Project
- Frontend
- Backend
我的主要问题是,尽管我能够在创建React应用程序构建时设置端口,但我无法将主机名设置为0.0.0.0,而不是localhost。
在创建React应用程序中,我在“start”脚本中硬编码了
HOST=0.0.0.0
和PORT=443
。我尝试的另一种方法是编写一个脚本,将页面服务于端口443。构建可以使用端口443,但不能使用0.0.0.0。
create-react-app脚本
"scripts": {
"start": "HOST=0.0.0.0 PORT=443 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
项目根目录下的基本脚本
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"client": "cd frontend && npm start",
"client-prod": "cd frontend/build && npm install -g serve && serve -s build -l 443",
"server": "cd backend && npm start",
"sass": "node-sass -w frontend/source/styles/ -o frontend/source/styles/ --recursive",
"dev": "concurrently \"npm run server\" \"npm run client\" ",
"prod": "concurrently \"npm run server\" \"npm run client-prod\" "
},
本地主机上的开发工作良好,但我需要在0.0.0.0:443上提供构建页面,而不是在本地主机上。