部署到Heroku时开始学习Angular / Node问题

时间:2018-05-24 09:56:19

标签: node.js angular heroku

我正在尝试配置一个简单的Angular / NodeJS应用程序并使用heroku进行部署。我是Angular / Node世界的新手,我正在关注一个在线课程,本地一切都很好但是在部署时我无法执行它。

当我尝试在浏览器上执行时出现以下错误: 应用程序错误 应用程序中发生错误,无法提供您的页面。如果您是应用程序所有者,请检查日志以获取详细信息

我的package.json是:

{
  "name": "ggrande",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build --aot -prod",
    "start": "node server.js"
    },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.0",
    "@angular/cli": "^6.0.3",
    "@angular/compiler-cli": "^6.0.2",
    "@angular/common": "^6.0.0",
    "@angular/compiler": "^6.0.0",
    "@angular/core": "^6.0.0",
    "@angular/forms": "^6.0.0",
    "@angular/http": "^6.0.0",
    "@angular/platform-browser": "^6.0.0",
    "@angular/platform-browser-dynamic": "^6.0.0",
    "@angular/router": "^6.0.0",
    "body-parser": "^1.18.3",
    "bootstrap": "^3.3.7",
    "core-js": "^2.5.4",
    "express": "^4.16.3",
    "mongodb": "^3.1.0-beta4",
    "path": "^0.12.7",
    "rxjs": "^6.0.0",
    "typescript": "~2.7.2",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.0",
    "@angular/language-service": "^6.0.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "enhanced-resolve": "^3.3.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  },
  "engines": {
    "node": "10.0.0",
    "npm": "5.6.0"
  }
}

我的个人资料很简单:

web: node server.js

server.json是:

    var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;
const path = require('path');

var USERS_COLLECTION = "users";

var app = express();
app.use(bodyParser.json());

// Create link to Angular build directory
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));



// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;

// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/test", function (err, client) {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  // Save database object from the callback for reuse.
  db = client.db();
  console.log("Database connection ready");

  // Initialize the app.
  var server = app.listen(process.env.PORT || 8080, function () {
    var port = server.address().port;
    console.log("App now running on port", port);
  });

  app.get('/*', function(req, res) {
    res.sendFile(path.join(distDir + '/myapp/index.html'));
  })
});
编辑:我解决了崩溃,问题发生在server.js文件中。但是现在/dist/index.html会返回一个空白页......它怎么可能?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我和heroku有同样的问题,但我通过获取日志文件解决了这个问题。

只做一件事,看到你的日志,你就会得到确切的错误。

1)转到项目目录, 2)打开终端/ cmd 3)输入" heroku logs -n 500"命令。

它会显示你的heroku应用程序的所有活动,然后找到红色标记的文本,你就会知道确切的错误。

如果您无法解决问题,只需发布​​日志的清晰屏幕截图即可。

谢谢