I've done some routing for my ReactJS application, it work perfectly in localhost. But when i want to deploy it on my Node.js server, i can only access to my root path '/'. For the other i get this:
Cannot GET /dir/secret/dashboard
I'm pretty sure that i've done something wrong but i can't figure out what.
Here's the code where i use react-route-dom:
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router , Route } from 'react-router-dom';
import './index.css';
import App from './App';
import Register from './pages/Register';
import Login from './pages/Login';
import Dashboard from './pages/Dashboard';
import Settings from './pages/Settings';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<Router>
<div>
<Route exact path='/' component={App} />
<Route path='/dir/secret/login' component={Login} />
<Route path="/dir/secret/register" component={Register} />
<Route exact path="/dir/secret/dashboard" component={Dashboard} />
<Route exact path="/dir/secret/dashboard/settings" component={Settings} />
</div>
</Router>,
document.getElementById('root')
);
serviceWorker.unregister();
server.js
const express = require('express');
const server = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const cron = require('node-cron');
const axios = require('axios');
const port = 4200;
const cors = require('cors');
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb+srv://***:***@***-hsm29.mongodb.net/test?retryWrites=true&w=majority', {useNewUrlParser: true, useUnifiedTopology: true})
.then(() => {
console.log('> Connected to your MongoDB Cluster');
})
.catch(err => {
console.error('App starting error:', err.stack);
process.exit(1);
});
const router = require('./routes/router');
const corsOption = {
origin: '*',
optionSuccessStatus: 200,
}
server.use(express.static('public'));
server.use(cors(corsOption));
server.use(bodyParser.urlencoded({extended: true}));
server.use(bodyParser.json());
const upload = require('./upload')
server.post('/upload', upload)
const banner = require('./banner')
server.post('/banner', banner)
server.use(express.static(__dirname+'/public'));
server.use('/api', router);
server.use(express.static(path.join(__dirname, 'build')));
-server.get('/', function (req, res) {
+server.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
});
server.listen(port, function(){
console.log(`> Ready on http://localhost:${port}`);
});
Thank's by advance for your time and your help
Ok i've fixed my problem. Here's the modifications i've made
Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter} from 'react-router-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<BrowserRouter>
<App/>
</BrowserRouter>,
document.getElementById('root')
);
serviceWorker.unregister();
App.js
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Content from './pages/Content';
import Register from './pages/Register';
import Login from './pages/Login';
import Dashboard from './pages/Dashboard';
import Settings from './pages/Settings';
import './App.css';
function App() {
return (
<>
<Router basename={process.env.PUBLIC_URL+'/'}>
<div className="App">
<Switch>
<Route exact path='/' component={Content}/>
<Route path='/dir/secret/login' component={Login} />
<Route path="/dir/secret/register" component={Register} />
<Route exact path="/dir/secret/dashboard" component={Dashboard} />
<Route exact path="/dir/secret/dashboard/settings" component={Settings} />
</Switch>
</div>
</Router>
</>
);
}
export default App;
Server.js
const express = require('express');
const server = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const cron = require('node-cron');
const axios = require('axios');
const port = 4200;
const cors = require('cors');
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb+srv://***:***@***-hsm29.mongodb.net/test?retryWrites=true&w=majority', {useNewUrlParser: true, useUnifiedTopology: true})
.then(() => {
console.log('> Connected to your MongoDB Cluster');
})
.catch(err => {
console.error('App starting error:', err.stack);
process.exit(1);
});
const router = require('./routes/router');
const corsOption = {
origin: '*',
optionSuccessStatus: 200,
}
server.use(express.static('public'));
server.use(cors(corsOption));
server.use(bodyParser.urlencoded({extended: true}));
server.use(bodyParser.json());
const upload = require('./upload')
server.post('/upload', upload)
const banner = require('./banner')
server.post('/banner', banner)
server.use(express.static(__dirname+'/public'));
server.use('/api', router);
server.use(express.static(path.join(__dirname, 'build')));
server.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
server.listen(port, function(){
console.log(`> Ready on http://localhost:${port}`);
});
So i've modify all my routing to match the react-router-dom documentation and i just change this:
server.use(express.static(path.join(__dirname, 'build')));
-server.get('/', function (req, res) {
+server.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
to this:
server.use(express.static(path.join(__dirname, 'build')));
server.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
i'm pretty sure that there is a better way to do what i want but the fact is that this way work for me so if it could help anyone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.