簡體   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