node.js - Heroku 应用显示空白页。反应/表达/节点
问题描述
https://peaceful-basin-17224.herokuapp.com/
当然,“它适用于我的机器”。
当我查看网络时,我看到资产是从 index.html 加载的,但没有别的。此外,由于某种原因,npm run build
似乎没有运行。我会heroku bash
进入应用程序,但我不会找到/frontend/build
从昨晚开始我就一直在搞这个,感觉我没有取得任何进展。任何帮助,将不胜感激!
我的 server.js 的一部分
app.use(express.static(path.join(__dirname, '/frontend')));
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, '/frontend/public', 'index.html'));
});
app.use(express.static(__dirname +'frontend'));
//Serve static assests if in production
if(process.env.NODE_ENV === 'production'){
//Set static folder
app.use(express.static('frontend/build'));
app.get('*', (req, res)=>{
res.sendFile(path.resolve(__dirname, 'frontend', 'public', 'index.html')) });
}
这是我的 package.json:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"proxy": "http://localhost:3001",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"server": "nodemon server.js",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix frontend && npm run build"
},
"author": "",
"license": "ISC",
"dependencies": {
"@mailchimp/mailchimp_marketing": "^3.0.69",
"express": "^4.17.1",
"simple-react-modal": "^0.5.1"
}
}
解决方案
我认为您可以删除所有这些代码:
app.use(express.static(path.join(__dirname, '/frontend')));
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, '/frontend/public', 'index.html'));
});
app.use(express.static(__dirname +'frontend'));
//Serve static assests if in production
if(process.env.NODE_ENV === 'production'){
//Set static folder
app.use(express.static('frontend/build'));
app.get('*', (req, res)=>{
res.sendFile(path.resolve(__dirname, 'frontend', 'public', 'index.html')) });
}
只放这个:
app.use(express.static(path.join(__dirname, './frontend/build')));
我的 react.js 文件夹的名称为“frontend”,它与我的 server.js 文件处于同一级别,这就是为什么它是一个点./frontend/build
。并且不要删除build
名称,也不要将其更改为任何其他名称。
如果这个答案有帮助,请告诉我。
推荐阅读
- java - 网络服务 java
- oracle - 如何将多个结果连接到oracle中的一列?
- c# - 如何使用按钮更改表单的背景颜色
- plot - 为什么 Mathematica 显示一个空白框?
- php - PHP Singleton 作为 HTTP 服务器中 HTTP 请求的回调
- python - Python:将数据从 excel 转换为饼图
- android - Firestore Stripe 错误“无法序列化对象。不支持序列化集合,请改用列表。”
- android - HttpURLConnection 间歇性返回错误代码 500
- scala - 使用不同的设置两次运行 sbt 任务
- node.js - 带有令牌的 PASSPORT GOOGLE OAUTH2