简体   繁体   English

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

[英]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:

the response from heroku app: heroku 应用程序的响应:

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.

相关问题 我在 Heroku 上部署了一个快速应用程序,但出现 404(未找到)错误 - I have an express app deployed on Heroku but I have 404 (not found) error 部署在 heroku 上的 React + Node + Express 应用程序显示不匹配 404 路由的空白页 - React + Node + Express app deployed on heroku shows a blank page for no match 404 routes 在部署在 Heroku 上的 node.js + Express 应用程序中找不到 jQuery 文件 - jQuery file is not found in node.js + Express app deployed on Heroku Heroku-Express和React应用程序在部署时显示空白屏幕 - Heroku - Express and React app showing blank screen when deployed React + Express 应用程序上的 API 路由在 Heroku 上给出 404 - API routes on react + express app giving 404 on Heroku 在部署在 heroku 上的 MERN 应用程序上获取 404 - Getting 404 on a MERN App deployed on heroku 无法在Heroku中查看Express / React应用(当前未找到) - Can't View Express/React App in Heroku (concurrently not found) 部署到 heroku 时,在 typescript 中找不到 tsc 表达和反应应用程序 - tsc not found in typescript express and react app when deploying to heroku 在 heroku 上部署 express 应用程序时无法获取/ - Cannot Get/ when express app is deployed on heroku React 应用程序在 Heroku 上顺利部署,但应用程序未运行 - React app deployed smoothly on Heroku but the app is not running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM