Heroku Express Creat-React-App Mlab部署-无法获取

时间:2018-07-18 20:07:42

标签: reactjs express heroku deployment mlab

寻求帮助。我刚刚将我的第一个react / express / mongo应用程序部署到了heroku,但是它无法加载,而且我似乎看不到任何指示原因的错误。

这是我的文件夹结构:

folder structure

我的顶级package.json文件看起来像

{
  "name": "russia_2018",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "9.5.0"
  },
  "scripts": {
    "build": "concurrently \"cd client && yarn build\" \"cd server && 
yarn build\"",
    "clean": "concurrently \"rimraf node_modules\" \"cd client && rimraf 
node_modules build\" \"cd server && rimraf node_modules build\"",
    "heroku-postbuild": "yarn build",
    "install": "(cd client && yarn) && (cd server && yarn)",
    "start": "concurrently \"cd client && PORT=3000 yarn start\" \"cd 
server && PORT=3001 yarn start\"",
    "start:prod": "cd server && yarn start:prod",
    "heroku-postbuild": "cd client && yarn && yarn run build"
  }

Procfile:

web: yarn start:prod

服务器package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "babel . --ignore node_modules,build --out-dir build",
    "start": "nodemon -r babel-register server.js",
    "start:prod": "node server.js",
    "seeds": "mongo < db/seeds.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^1.17.5"
  },
  "dependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "mongod": "^2.0.0",
    "mongodb": "^3.1.1"
  }
}

和server.js:

const express = require('express');
const app = express();
const path = require('path');
const parser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const createRouter = require('./helpers/create_router.js');

const staticFiles = express.static(path.join(__dirname, 
'../../client/build'))

app.use(staticFiles)

app.use(parser.json())

  MongoClient.connect('mongodb://full-url... 
 ||'mongodb://localhost:27017'', (err, client) => {
  if(err){
    console.log(err);
  }

  const db = client.db('russia');

  const games = db.collection('games');
  const gamesRouter = createRouter(games);
  app.use('/api/games', gamesRouter);

  app.use('/*', staticFiles)


  app.listen(process.env.PORT || 3001, function(){
    console.log('listening on port 3001');
  })

});

我无法看到错误的出处,因为日志显示该应用已连接到本地路由。唯一看起来可能是错误的行是Stopping all processes with SIGTERM,但此后该应用程序仍可继续连接。

谢谢

1 个答案:

答案 0 :(得分:1)

似乎heroku从未获得过您的构建文件夹。您的procfile为herkou分配了此命令,以在部署web: yarn start:prod上调用。查看您的顶级package.json,似乎start:prod仅执行此"start:prod": "cd server && yarn start:prod",其中不包含有关构建react应用程序的任何说明。

您可能需要更改procfile才能调用命令,该命令将为您构建客户端文件夹,然后在完成后启动服务器。

您还可以在本地构建文件夹,并将其从客户端.gitignore删除,以便heroku可以仅启动服务器并且已经具有构建文件夹。

您可能已经看过this文章,但如果没有,这是该主题的很好阅读。