繁体   English   中英

部署反应和快递应用程序到 heroku 发送 404 未找到

[英]Deployed react and express app to heroku sends 404 not found

我使用npm run build构建我的反应应用程序,然后我使用本地快递发送构建的index.html文件,一切正常。 但是当我将我的应用程序部署到 heroku 并打开提供的链接时,它只会显示“未找到”。 我不知道这是路由问题还是其他问题。 我会很感激你的反馈。

快递代码:

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}`))

heroku 部署的应用程序 output:

heroku 应用程序的响应:

您的环境变量可能有问题。 将.env 变量添加到您的 heroku 应用程序中,这可能会导致此错误,因为 .env 文件未与您的存储库一起推送。 在 heroku 项目环境变量中添加您的 PORT 变量其次,在您的应用程序索引或服务器文件中添加以下默认路由。

app.use('/', (req,res) => {
    res.send({msg:'Welcome to server'})
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM