【问题标题】:Heroku ReactJS app renders blank page but works fine locallyHeroku ReactJS 应用程序呈现空白页面,但在本地工作正常
【发布时间】:2020-12-10 06:35:00
【问题描述】:

我在 heroku 上部署了一个 ReactJS + ExpressJS 应用程序,它呈现 index.html,但其中没有 React 应用程序。这是 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"
    ]
  }
}

它在本地运行良好,但在部署时不会呈现应用程序...

【问题讨论】:

    标签: reactjs express heroku


    【解决方案1】:

    在 server.js 中解决了

    你必须告诉 express 使用构建路径而不是 src 或公共目录

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

    【讨论】:

      猜你喜欢
      • 2017-04-26
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 2022-01-04
      • 2021-09-09
      相关资源
      最近更新 更多