Heroku ReactJS应用程序呈现空白页,但在本地运行良好

时间:2020-08-20 21:20:17

标签: reactjs express heroku

我在heroku上部署了ReactJS + ExpressJS应用程序,它呈现了index.html,但其中没有React App。这是package.json:


{
  "name": "spotifyproj",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://whispering-caverns-57172.herokuapp.com",
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "bootstrap": "^4.5.2",
    "compression": "^1.7.4",
    "cookie-parser": "^1.4.5",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "morgan": "^1.10.0",
    "path": "^0.12.7",
    "querystring": "^0.2.0",
    "react": "^16.13.1",
    "react-bootstrap": "^1.3.0",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.3",
    "react-spotify-player": "^1.0.4",
    "spotify-web-api-js": "^1.5.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "heroku-postbuild": "npm run build"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

它在本地运行良好,但是在部署后不会呈现该应用...

1 个答案:

答案 0 :(得分:0)

已在server.js中解决

您必须告诉express使用构建路径,而不要使用src或公共目录

app.use(express.static(path.join(__dirname, 'build')))
    .use(cors())
    .use(cookieParser());