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

时间:2019-11-24 08:28:54

标签: node.js mongodb express heroku create-react-app

客户端使用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时,似乎一切正常。我的意思是,它导航到客户端并安装并构建软件包

1 个答案:

答案 0 :(得分:0)

app.use(express.static(path.join(__dirname, 'client', 'build')));移至app.use(error)和error handlign函数之前,然后可以正常使用

相关问题