1

So I created a post previous day and deleted it because it's unproper and also unclear, so right now, I'll try to write it properly.

So I created a MERN app using render.com, but the problem is every time I refresh the the webpage, I get an error / not found. Example. I went to a specific user id , I can view his the profile but once I refresh/reload, it gets an error. So I created a two separate folder and two different GitHub repo.

I uploaded the front end as a static website in render.com

Github: Front Site

Website Front Website

So for the backend, I follow this thread and Why deployed create-react-app returns 404 error upon reload?. But I don't know how can I properly use res.sendFile because I'm using a different root directory as well as different github repo.

Github : https://github.com/cruz-emman/back

Website: https://mern-tua-ebenta.onrender.com/

I tried to use this, and I admit that I'm doing it wrong because I don't know how can I do it.

app.use(express.static(path.join(__dirname,'../client/build')));
app.get('*', (req,res) => res.sendFile(path.resolve(__dirname,'../', 'client','build','index.html'))

EDIT: TYPO

Zurcemozz
  • 183
  • 1
  • 9

1 Answers1

0

After trial and error, I finally solved the answer, so what I did here is merge the two folders. enter image description here

After putting it in a single directory, I run npm run build for client folder. After that, I put this code inside index.js

const __dirname = path.resolve()
app.use(express.static(path.join(__dirname, '/client/build')));
app.get("*",(req,res) => {
  res.sendFile(path.join(__dirname, '/client/build/index.html'));
})

After that, I push it to a new git repository, and run web service for render.com

Zurcemozz
  • 183
  • 1
  • 9