Mern 堆栈应用程序在 heroku local 上工作但在部署时不在线工作

Mern stack app working on heroku local but not working online when deploy

我在 Heroku 上安装了我的 mern stack 应用程序,之前它运行良好,但现在部署正常但无法运行。

所有 API 工作正常,但前端未呈现。

当我访问主页时它说

/app/client/build/index.html

所有 API 都正常工作

这是我的包 json 服务器

  "name": "***",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "client-install": "cd client && yarn install ",
    "server": "nodemon app",
    "start": "node app.js",
    "client": "cd client && yarn  start ",
    "dev": "concurrently \"yarn run server \" \"yarn run client\" ",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false &&  cd client && npm install && npm build "
  },
  "repository": {
    "type": "***",
    "url": "***"
  },
  "author": "",
  "license": "ISC",
  "homepage": "***",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "helmet": "^3.19.0",
    "jimp": "^0.6.4",
    "morgan": "^1.9.1",
    "multer": "^1.4.1"
  },
  "devDependencies": {
    "concurrently": "^4.1.1"
  }
}

这是我给客户json的包裹

{
  "name": "***",
  "version": "0.1.0",
  "private": ***,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.19",
    "@fortawesome/free-brands-svg-icons": "^5.9.0",
    "@fortawesome/free-regular-svg-icons": "^5.9.0",
    "@fortawesome/free-solid-svg-icons": "^5.9.0",
    "@fortawesome/react-fontawesome": "^0.1.4",
    "axios": "^0.19.0",
    "bootstrap": "^4.3.1",
    "file-saver": "^2.0.2",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-router-dom": "^5.0.1",
    "react-scripts": "3.0.1",
    "react-toastify": "^5.3.2",
    "reactstrap": "^8.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "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"
    ]
  },
  "devDependencies": {
    "node-sass": "^4.12.0"
  }
}

In both, I had dev dependencies and I switched from yarn to npm in my Heroku post-build.

这是我在 app.js

中的路由文件
app.use(express.static(path.join(__dirname, "client", "build")));

app.use("/*", (req, res) => {
  res.send(path.join(__dirname, "client", "build", "index.html"));
});

如有错误请指出

使用

app.use('*', express.static(path.join(__dirname, "client", "build")))

而不是

app.use("/*", (req, res) => {
  res.send(path.join(__dirname, "client", "build", "index.html"));
});

我遇到了类似的错误,不知道为什么,但解决了问题

您好,我遇到了同样的问题,这是 mongodb 网络访问问题。我是这样解决的:登录 MongoDB > select 你的项目 > 网络访问 > 添加 IP 地址 > 单击 "ALLOW ACCESS FROM ANYWHERE"