React-Router v4 history on express server

我只是一个虾纸丫 提交于 2019-12-24 18:18:36

问题


I have a react app using the following for router history.

    import { BrowserRouter, Route, Link } from "react-router-dom";
    import { createBrowserHistory } from "history";

<BrowserRouter history={history}>
...

This is served using Express server like this (server.js)...

//for build
// Serve the static files from the React app
app.use(express.static(path.join(__dirname, 'build')));

app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

This is working fine running locally and I can navigate to any Route successfully. Once deployed to production, If I navigate to a Route, I get

Cannot GET /Sites/SiteDashboard/

The structure of my project is as follows which is a standard CRA install...

The problem I have is pointing to the correct location on the production build to point to the index.html (or whatever) so that I can access the history like I do running locally (npm start).

Can anyone advise where I need to point to.

Thanks.

来源:https://stackoverflow.com/questions/58489381/react-router-v4-history-on-express-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!