简体   繁体   English

一些反应路线不适用于刷新或手动网址

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

I've read all the available questions here at StackOverflow or any other platform.我已经阅读了 StackOverflow 或任何其他平台上的所有可用问题。 I understand that every time a url is entered it first hits the backend.我知道每次输入网址时,它首先会到达后端。 But how is that possible when backend and frontend work on different ports?但是当后端和前端在不同的端口上工作时,这怎么可能呢?

Here is my server:这是我的服务器:

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

I do have a route that redirects to my index.html if no route is matched, however when I enter a url it never reaches the backend route.如果没有匹配的路由,我确实有一个重定向到我的 index.html 的路由,但是当我输入一个 url 时它永远不会到达后端路由。

It works fine using postman.使用邮递员可以正常工作。 Frontend react app runs on port 3000, backend runs on port 5000. I don't get any errors or what so ever on page refresh.前端反应应用程序在端口 3000 上运行,后端在端口 5000 上运行。我在页面刷新时没有收到任何错误或任何错误。 It does'nt load the routes it should in the front end.它不会加载它应该在前端的路由。

EDIT: it reaches the route but doesnt render the index.html page, I checked the directory,it should be working, i dont know wth is wrong编辑:它到达路线但不呈现 index.html 页面,我检查了目录,它应该可以工作,我不知道是错误的

This problem because react has its own server and by default put the localhost@5000 as a prefix on any request (proxy) so you can solve it by put.这个问题是因为 react 有自己的服务器,并且默认情况下将 localhost@5000 作为任何请求(代理)的前缀,这样你就可以通过 put 来解决它。

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

then restart your front server more than one time then try your requests.然后多次重新启动您的前端服务器,然后尝试您的请求。

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

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