代码之家  ›  专栏  ›  技术社区  ›  L-R

Heroku Express Creat React应用程序MLAB部署-无法获取

  •  0
  • L-R  · 技术社区  · 7 年前

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

    这是我的文件夹结构:

    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"
      }
    

    程序文件:

    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 但之后应用程序继续连接。

    谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   Chaim Friedman    7 年前

    似乎Heroku永远不会得到你的构建文件夹。procfile为herkou指定此命令,以便在部署时调用 web: yarn start:prod 是的。查看您的顶级package.json似乎 start:prod 只有这样 "start:prod": "cd server && yarn start:prod" 其中不包括任何关于构建react应用程序的说明。

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

    您还可以在本地生成文件夹,并将其从客户端删除。gitignore这样heroku就可以启动服务器并且已经有了可用的生成文件夹。

    你可能已经看到了 this 文章,但如果不是,这是一个非常好的主题阅读。

    推荐文章