【问题标题】:Heroku Express Creat-React-App Mlab deployment - cannot GETHeroku Express Create-React-App Mlab 部署 - 无法获取
【发布时间】:2018-12-26 20:34:03
【问题描述】:

寻求帮助。我刚刚将我的第一个 react/express/mongo 应用程序部署到 heroku,但它加载时无法获取,我似乎看不到任何表明原因的错误。

这是我的文件夹结构:

我的顶级 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"
  }

过程文件:

web: yarn start:prod

服务器包.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,但之后应用程序会继续连接。

谢谢

【问题讨论】:

  • 这个 react 应用是用 create-react-app 创建的吗?
  • 是的,它是使用 create react app 创建的
  • 您是在尝试使用已构建的客户端文件夹进行推送,还是在部署时构建 heroku?
  • 我认为 heroku 在部署上构建..."heroku-postbuild": "cd client &amp;&amp; yarn &amp;&amp; yarn run build" 在我的顶级 package.json 中

标签: reactjs express heroku deployment mlab


【解决方案1】:

似乎 heroku 永远不会获取您的构建文件夹。您的 procfile 为 herkou 指定此命令以调用 deploy web: yarn start:prod。查看您的顶级 package.json 似乎 start:prod 仅执行此操作 "start:prod": "cd server &amp;&amp; yarn start:prod" 不包括有关构建反应应用程序的任何说明。

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

您也可以在本地构建文件夹,然后将其从客户端 .gitignore 中删除,这样 heroku 就可以启动服务器并且已经有了可用的构建文件夹。

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

【讨论】:

    猜你喜欢
    • 2017-09-21
    • 2018-11-10
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2023-01-30
    • 2019-08-21
    相关资源
    最近更新 更多