Heroku应用程序显示空白页面.react/express/node



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'))   });
}

这是我的包.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文件夹的名称是"前端";它与我的server.js文件处于同一级别,这就是为什么它是单点./frontend/build。并且不要删除build名称,也不要将其更改为任何其他名称。

如果这个答案有帮助,请告诉我。

最新更新