javascript - heroku 本地网络工作正常,但部署无法正常工作
问题描述
应用由 Node.js 和 React 组成。没有数据库。Heroku 本地网络工作正常,但部署无法正常工作。部署只有 index.html。简而言之,node 和 react 在部署时不能正常工作。
后端包.json
"engines": {
"node": "15.4.0",
"npm": "7.20.5"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js",
"heroku-postbuild": "cd frontend && npm install && npm run build"
}
前端包.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:5000"
应用程序.js
app.use(express.static(path.join(__dirname, './frontend/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'/frontend/build/index.html'));
});
const PORT = process.env.PORT || 5000;
档案
web: node app.js
heroku local web
上面的代码一切都很好。但是在部署时我遇到了这样的错误:
我得到的错误
Stopping all processes with SIGTERM
heroku[web.1]: Process exited with status 143
我在这里尝试了一些解决方案。但我无法解决。 我可以根据需要写更多信息。谢谢!
解决方案
将这些添加到您的前端 package.json 脚本中:
"devStart": "react-scripts start",
"start": "serve -s build",
....
"heroku-postbuild": "npm run build"
推荐阅读
- laravel - 在 laravel 字符串验证中避免使用 html 标签
- node.js - MongoDB Docker URI 没有主机名、域名和 tld
- php - 错误 1044 (42000):拒绝用户 'root'@'localhost' 访问数据库 (MySQL - MariaDB)
- python - Python Web Scraper - 从 href 抓取链接的问题
- r - 在 dplyr 中应用带有 cross() 的 summarise() 函数时出错
- sql - 在基于行的字段/值表上处理复杂的查询
- java - 如何保护您的 API 免受了解您 API 的开发人员的攻击?
- angular - 从 Angular 的 RXJS/NGRX 存储中获取不可变项以在组件中编辑的最佳实践
- android - 在 x 轴上滑动时锁定水平 RecyclerView 中的 Y 轴滚动
- javascript - 如何在赛普拉斯(vscode)中进行自定义命令实现