简体   繁体   English

Mern 堆栈应用程序在本地 heroku 上工作但在部署时无法在线工作

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

I had my mern stack app on Heroku and previously it is working fine but now it is deploying fine but not working.我在 Heroku 上有我的 mern 堆栈应用程序,以前它运行良好,但现在部署正常但无法正常工作。

All the API are working fine but the frontend is not rendering.所有 API 都工作正常,但前端没有呈现。

When I visit the homepage it says当我访问主页时,它说

/app/client/build/index.html /app/client/build/index.html

all the API are working fine as it is所有 API 都正常工作

here is my package json for server这是我的服务器包 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"
  }
}

And here is my package json for client这是我的客户端 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.在这两者中,我都有开发依赖项,并且在我的 Heroku 后期构建中从 yarn 切换到 npm。

here is my routing file in app.js这是我在 app.js 中的路由文件

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

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

If I had done any mistake then please point.如果我做错了什么,请指出。

use

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

instead of代替

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

I was getting similar error, don't know why, but solved the problem我遇到了类似的错误,不知道为什么,但解决了问题

Hi I was having the same problem, it's a mongodb Network access issue.嗨,我遇到了同样的问题,这是一个 mongodb 网络访问问题。 I solved it by this: login to MongoDB > select you project > Network Access > ADD IP ADDRESS > click "ALLOW ACCESS FROM ANYWHERE"我是这样解决的:登录MongoDB>选择你的项目>网络访问>添加IP地址>点击“允许从任何地方访问”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM