1
const express = require('express')
const cors = require('cors')
const path = require('path');

const app = express()

app.use(express())
app.use(cors())
app.use(express.static(path.join(__dirname, 'client/build')))

app.get('/', (req, res)=>{
    console.log('server is up')
})

app.get('/data', (req, res)=>{
    res.send({name: 'name', age: 'age' })
})

app.listen(5000)

I am getting cannot Get /URL. I want to run build index.html file with express. React Router is not working. Any idea how to fix it?

1 Answer 1

1

You should be routing all the request to the index.html assuming its a SPA(Single Page Application)

like this:

app.get('*', function(req, res) {
  res.sendFile('index.html', {root: path.join(__dirname, '../../client/build/')});
});

Sign up to request clarification or add additional context in comments.

1 Comment

It is not a single page application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.