I'm running BOTH an express and create-react-app app. They are seperate entities during development and express is just an endpoint which we communicate through 'proxy' field in package.json proxy: 'http://localhost:5000
. During production, I run yarn build
and serve the static index.html inside build folder through catch-all-route in Express.
// RENDER REACT STATIC INDEX.HTML FROM BUILD
if(process.env.NODE_ENV === 'production') {
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '../client/build', 'index.html'))
})
}
Okay so, there is huge difference during production and development. For instance, if I navigate to /pages/plainhtmlfile during production
, I can serve the .EJS file if route matches. Because during production there is only Express running, which serves static files including React build folder index.html.
//THIS WORKS DURING PRODUCTION, NOT WORKING DURING DEVELOPMENT.
app.get('/plainhtmlfile', (req, res) => {
res.render('nonreactpage')
})
How to do development where it's possible to serve '/plainthtmlfile'? Right now create-react-app is a seperate process running on port 3000 and express port 5000 during dev.