簡體   English   中英

一些反應路線不適用於刷新或手動網址

[英]Some react routes don't work on refresh or manual url

我已經閱讀了 StackOverflow 或任何其他平台上的所有可用問題。 我知道每次輸入網址時,它首先會到達后端。 但是當后端和前端在不同的端口上工作時,這怎么可能呢?

這是我的服務器:

app.use(cors())

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

connectDB()


const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, () => console.log(`Server running on port ${PORT}`))



const socketio = require('socket.io')
const io = socketio(server)
const questions = require('./api/questions')(io)

app.use('/api/users', users)
app.use('/api/posts', posts)
app.use('/api/questions', questions)




express.static(path.resolve(__dirname, '/client/public'));

app.get('/*', (req, res) => {
    res.sendFile(__dirname + '/client/public/index.html'), err => console.log(err)
})

如果沒有匹配的路由,我確實有一個重定向到我的 index.html 的路由,但是當我輸入一個 url 時它永遠不會到達后端路由。

使用郵遞員可以正常工作。 前端反應應用程序在端口 3000 上運行,后端在端口 5000 上運行。我在頁面刷新時沒有收到任何錯誤或任何錯誤。 它不會加載它應該在前端的路由。

編輯:它到達路線但不呈現 index.html 頁面,我檢查了目錄,它應該可以工作,我不知道是錯誤的

這個問題是因為 react 有自己的服務器,並且默認情況下將 localhost@5000 作為任何請求(代理)的前綴,這樣你就可以通過 put 來解決它。

"proxy": "http://localhost@serverport"  // in the package.json file on the react

然后多次重新啟動您的前端服務器,然后嘗試您的請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM