F
F
Fatoblack2016-12-05 17:18:37
Express.js
Fatoblack, 2016-12-05 17:18:37

How to set up react-route in conjunction with express?

I use React-Route and expess.
It is necessary to give the file public/index.html in all ways.
On the server, the code is as follows:

//устанавливаем папку public за статику.
app.use(express.static('public'));
//путь "/"
app.get('/', (req, res) => {
    res.sendFile( __dirname + '/public/index.html' );
});
//путь "/signin"
app.get('/signin', (req, res) => {
    res.sendFile( __dirname + '/public/index.html' );
});

React-Router on the client:
<Router history={browserHistory}>
        <Route path="/" component={Home} />
        <Route path="/signin" component={LoginForm} />
</Router>

When the server starts, the {Home} component is displayed perfectly,
but getting into the "/signin" path, the {loginForm} component can only be accessed through a link
<Link to="/signin"> signin </Link>
that is in the Home component.
The problem is that if we hit the /signin path and reload the page with F5, we get the following error:
Error: ENOENT: no such file or directory, stat 'C:\Web development\server\public\index.html'
at Error (native )
Express requires statics, but I already added a public folder on the server. How to solve the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question