I got a problem with my routes in ReactJS
. I defined some routes like this :
...Import / Class...
class App extends Component {
render() {
return (
<div>
<Header />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/login" component={Login} />
<Route path="/signup" component={Signup} />
<ProtectedRoute path="/user/contact" component={Contact} />
<ProtectedRoute path="/user/person" component={UserPerson} />
<ProtectedRoute path="/user/profile" component={Profile} />
<Route component={NotFound} />
</Switch>
<Footer />
</div>
);
}
}
...Export...
Below you can see my ProtectedRoute
class
...Import / Class...
const ProtectedRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
AuthGestion.userIsAuthenticated() ? (
<Component {...props} />
) : (
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}}/>
)
)} />
);
...Export...
When I go to my page with a <NavLink to="/user/person"></NavLink>
, there is no problem, my component is loaded and I can see my page.
But when I go to the url directly /user/person
, and recharge my page (CTRL + F5), I got in the Console
the error SyntaxError: expected expression, got '<'
and a white page.
Just the user routes don't work.
I add below my server informations :
import path from 'path';
import bodyParser from 'body-parser';
import express from 'express';
import mongoose from 'mongoose';
import routes from './routes/index.route';
import config from '../../config/config';
import webpack from 'webpack';
import webpackConfig from '../../config/webpack.config.dev';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
//Connexion MongoDB
mongoose.connect(config.mongodbUri, { useNewUrlParser: true });
mongoose.connection.on('connected', () => { console.log('MongoDB connecté'); });
mongoose.connection.on('error', (error) => { console.log(error); });
//Lancement serveur express
const server = express();
//Express need
server.use(bodyParser.json());
server.use(express.static(config.publicPath));
server.use(express.static(config.distPath));
//Hot reload
if(config.isDevMode) {
const webpackCompiler = webpack(webpackConfig);
server.use(webpackDevMiddleware(webpackCompiler, {}));
server.use(webpackHotMiddleware(webpackCompiler));
}
// Route vers l'API
server.use('/api', routes);
// Landing page
server.get('/*', (req, res) => {
res.sendFile(path.join(config.publicPath, '/index.html'));
});
//Ecoute du serveur
server.listen(config.port, () => {
console.log(`App here : http://${config.host}:${config.port}`);
});
export default server;
Can you please help me to understand the problem ? And maybe correct my routing if it's not good.