I have build a project started with create-react-app. I'm trying it to serve it with express. It works, but when I try to go to other routes, these routes are not working and my React app page is the only one thing that showing up. Could you explain me why please ? Here is my code.
const express = require('express');
const path = require('path');
const app = express();
const router = express.Router();
app.use(express.static(path.join(__dirname, 'build')));
app.use('/', router);
router.get('/api', (req, res) => {
res.send("API endpoints! ;)")
});
router.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(8080, () => {
console.log('Listening on 8080');
})
I have tried the solution there `express.static()` keeps routing my files from the route but it doesn't work.
I think i know whats wrong... Make sure you restart your backend when you make a code change or use nodemon when developing.
Your code looks fine.
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.