【发布时间】:2018-10-04 17:29:07
【问题描述】:
我有一个部署到 Heroku 并在 Express/Node.js 服务器上运行的 React Web 应用程序。当有人尝试访问任何路由时,我的服务器 index.js 文件运行以下代码:
const path = require('path')
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
当我从根路由 ('/') 加载应用程序时,一切都很好,当我浏览应用程序(使用 React 路由器)时,一切都很好。当我尝试从子路由(例如 /contact)刷新时会出现问题,然后我收到“未找到”错误。当我检查 Heroku 日志时,我看到了这条消息:
Error: ENOENT: no such file or directory, stat '/client/build/index.html'
我应该注意我正在服务器上运行构建脚本,当我登录 Heroku 时,我可以看到“构建”文件夹和所有内容,包括“index.html”,都存在。
我还应该注意我尝试过其他 sendFile 配置,但没有成功,例如:
res.sendFile(path.resolve(__dirname + '/client/build/index.html'))
有谁知道问题出在哪里?
【问题讨论】:
标签: node.js reactjs express heroku