让我们尝试将您的应用程序部署到Heroku:
在您的代码库中,创建一个名为server.js
的文件,并插入以下代码:
const express = require('express');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(port);
++在您的package.json
文件中,将 start 脚本更改为以下内容:
start: "node server.js"
Heroku 在您部署时自动运行构建命令,但最好在部署之前本地测试生产构建(尤其是第一次)。
您可以通过在终端中运行以下命令来在本地创建生产构建:
npm run build
使用 Git
完成此操作,因此您需要执行以下操作:
git init // to initialize a Git repo
git add . // to add the new files
git commit -m "Heroku deploy" // In order to commit your changes
// Now goes the comment to connect your local app to the one created on the Heroku
heroku git:remote -a nameOfYourApp // <- the one created on their website
git push heroku master // to push it to heroku master repo
Now you can run `heroku open` to see your app in the browser
参考链接
b) 使用零配置方法。使用此方法,您可以跳过express服务器(所有操作都已为您完成)->只需输入以下命令:
npm install -g create-react-app
create-react-app my-app
cd my-app
git init
heroku create -b https://github.com/mars/create-react-app-buildpack.git
git add .
git commit -m "react-create-app on Heroku"
git push heroku master
heroku open
参考链接
更新:
如果你使用 firebase,这里是你需要执行的步骤:
- 确保你已经安装了 firebase 工具
npm i -g firebase-tools
通过运行 npm run build
创建应用程序的生产 build
访问https://console.firebase.google.com 并创建你的"empathy" 应用程序
在你的项目中运行 firebase init
并提供以下答案:
你准备好继续了吗?-> YES
- 从列表中选择
hosting
- 选择之前在 firebase 控制台 上创建的应用程序
- 你想要将哪个文件夹作为你的
public
文件夹 -> build
- 你想要配置单页面应用程序吗?-> YES
- 你想要覆盖现有的
index.html
文件吗?-> NO
之后只需要运行 firebase deploy
create-react-app
使用 Babel 和 Webpack -> 它们负责你的开发工作流程。在创建生产构建之后,它会优化你的代码并将其放入 build 文件夹中。注意:看一下它,它会大大改变文件夹结构,这就是为什么你会得到不同的路径。但是使用create-react-app
一切都应该可以直接运行。希望这有意义。干杯。 - Dzenis H.