[英]Deployed react and express app to heroku sends 404 not found
I build my react app using npm run build
and then i sent the built index.html
file using express locally and everything works fine.我使用
npm run build
构建我的反应应用程序,然后我使用本地快递发送构建的index.html
文件,一切正常。 But when i deployed my app to heroku and open the provided links it just says "not found".但是当我将我的应用程序部署到 heroku 并打开提供的链接时,它只会显示“未找到”。 I don't know if this is a routing issue or something else.
我不知道这是路由问题还是其他问题。 I would appreciate your feedback.
我会很感激你的反馈。
The express code:快递代码:
const express = require('express')
const app = express()
const path = require('path')
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.resolve(__dirname, '..', 'client', 'build')))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'client', 'build', 'index.html'))
})
}
const PORT = process.env.PORT || 5500
app.listen(PORT, () => console.log(`Server running on port: ${PORT}`))
The heroku deployed app output: heroku 部署的应用程序 output:
There Might be a Problem with you environment Variables.您的环境变量可能有问题。 Add.env variables to your heroku app this might be causing this error as.env files are not deployed pushed with your repo.
将.env 变量添加到您的 heroku 应用程序中,这可能会导致此错误,因为 .env 文件未与您的存储库一起推送。 Add you PORT variable in heroku project environment variables And secondly, Add a following default route in your app index or server file.
在 heroku 项目环境变量中添加您的 PORT 变量其次,在您的应用程序索引或服务器文件中添加以下默认路由。
app.use('/', (req,res) => {
res.send({msg:'Welcome to server'})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.