4

I have found the solution to error from here

app.get('/*', function(req, res) {
  res.sendFile(path.join(__dirname, 'path/to/your/index.html'), function(err) {
    if (err) {
      res.status(500).send(err)
    }
  })
})

but I am not able to understand what I have actually write in that index.html file

enter image description here

9
  • 1
    index.html should contains the all the html code which necessary to render your app. For example: github.com/moshfeu/synced-shopping-list/blob/master/public/… (This app created by create-react-app, but it applies to regular react app too) Commented Oct 19, 2020 at 8:11
  • where I have to write this code in backend or frontend Commented Oct 19, 2020 at 8:17
  • 1
    In the frontend. i am using that build static in my backend how are you using it? If you put the html file also in build, so your code should be res.sendFile(path.join(__dirname, 'build/index.html') Commented Oct 19, 2020 at 8:38
  • 1
    You mention you're using React. How you bundle (npm run build) the react scripts? Commented Oct 19, 2020 at 8:52
  • 1
    Further our chat, just to recap (for future readers), OP is using react-scripts so the index.html is under build folder therefor, the server serves this html file using res.sendFile(path.join(__dirname, 'build/index.html'). Commented Oct 19, 2020 at 13:44

1 Answer 1

5

This solved my problem all thanks to @Mosh Feu Who helped me a lot in this problem solving you can see discussion in this chat

NOTE : use index.html file after declaring routes to api

app.use("/api", indexRouter);

app.get('/*', function (req, res) {
  res.sendFile(path.join(__dirname, '/build/index.html'), function (err) {
    if (err) {
      res.status(500).send(err)
    }
  })
})
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct solution.

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.