I have one react application created using create-react-app
command.
For example I have the following routing in my react application.
/Home
/About
/Profile/:id
Each routing is working perfectly in create-react-app
.
In my node application I have written all api for this application and are available using at myServer/api/v1/
. On other route I'm simply returning build/index.html
which is my build for react application.
Here is server.js
code snippet
app.use(express.static(__dirname + '/build'));
/* route path : 'api/v1/' */
app.use(require('./server/v1/routes'));
app.get('/*', function (req, res) {
return res.sendFile(path.join(__dirname, '/build/index.html'));
});
This is my server/v1/routes/index.js
file
var express = require('express');
var router = express.Router();
router.use('/api/v1/', require('./../api'));
module.exports = router;
This is my api/index.js
file
var express = require('express');
var router = express.Router();
var path = require('path');
import generalRouter from './controllers/general/router';
import profileRouter from './controllers/prifile/router';
//Login, forget password, reset password
router.use('/', generalRouter);
router.use('/profile', profileRouter);
module.exports = router;
each router file contains relative get/post/put/delete
method for my application.
React routing :
<BrowserRouter>
<Switch>
<Route exact={true} path='/' component={Home}/>
<Route path='/About' component={About}/>
<Route path='/ContactUs' component={ContactUs}/>
....
<Route path='/Profile/:id?' component={Profile}/>
<Route path='/VerifyAccount/:verifyLink?' component={VerifyAccount}/>
</Switch>
</BrowserRouter>
All my routing are working perfectly except routes with parameters in above example Profile/:id
and is not working. Same for all other routes (/VerifyAccount/:verifyLink?
) that are using parameters in react routing in my application. Its working in create-react-app
but not with my node server. Even if I comment out my api
routes.