将Mern堆栈部署到heroku时前端不起作用

问题描述 投票:0回答:1

客户端使用create-react-app构建,后端使用express,node.js和MongoDB。它可以在本地运行,但是当我部署到Heroku时,只有后端可以运行...

index.js

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

  +app.get('/', function(req, res) {
    -app.get('/*', function (req, res) {
       res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
     });
  });
}

服务器端的package.json

  "scripts": {
    "start": "node index.js",
    "heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
  },

客户端中的package.json

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000",

此外,我在MongoLab上的猫鼬工作正常,并将其添加到Heroku顺便说一句,当我推到Heroku时,似乎一切正常。我的意思是,它导航到客户端并安装并构建软件包

node.js mongodb express heroku create-react-app
1个回答
0
投票

app.use(express.static(path.join(__dirname, 'client', 'build')));移动到app.use(error)和error handlign函数之前,然后它可以工作

© www.soinside.com 2019 - 2024. All rights reserved.