0

This is my previous question. How can I properly use the res.sendFile for my MERN APP, I get an error every time I refresh it, it works there.

What happened here is the same thing, I still get an error when I refresh to a certain site even though I just followed the previous work. I decided to continue my previous work, and apply it to my previous works, but it stills the same even though followed the express.static

root
|--- client
|--- model
|--- route
|--- server
|--- index.js
|--- package.json

index.js

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

And at the same time, I used this inside my package.json

{
  "name": "tua_thesis_rework",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "build": "npm install && cd client && npm install && npm run build",
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@mui/x-data-grid": "^5.17.17",
    "cors": "^2.8.5",
    "crypto-js": "^4.1.1",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.0",
    "mongodb": "^4.13.0",
    "mongoose": "^6.8.2",
    "nodemon": "^2.0.20"
  }
}

Webiste: https://tua-ebenta.onrender.com/

Github Repo: https://github.com/cruz-emman/revison1

Zurcemozz
  • 183
  • 1
  • 9
  • I took a quick look into your repo, why are you jamming everything in the same workspace? and the reason why your client shows an empty page after build because cra static uses absolute paths, not relative ones, so your index.html will not be able to load the required js files – tbc Jan 05 '23 at 11:11
  • @tbc the reason I jam it everything in the same route because I was getting confused on calling `scripts` inside `package.json`, I was following a tutorial from youtube and apply it to mine. – Zurcemozz Jan 05 '23 at 11:15
  • And addition to that is, I followed the same step I used when the other `mernapp` works. – Zurcemozz Jan 05 '23 at 11:17
  • thanks @tbc , I followed your statement and fix problem! – Zurcemozz Jan 05 '23 at 11:35

1 Answers1

0

I fixed it, I went to my client/package.json and then remove the "homepage:" "."! Big thanks to @tbc, I hope I can tag you here!

Zurcemozz
  • 183
  • 1
  • 9