【问题标题】:Starting learning Angular/Node issues when deploying to Heroku部署到 Heroku 时开始学习 Angular/Node 问题
【发布时间】:2018-05-24 09:56:19
【问题描述】:

我正在尝试配置一个简单的 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 返回一个空白页……怎么可能?

提前感谢您的帮助。

【问题讨论】:

标签: node.js angular heroku


【解决方案1】:

我在使用 heroku 时遇到了同样的问题,但我通过获取日志文件解决了这个问题。

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

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

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

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

谢谢

【讨论】:

  • 错误日志为:2018-05-24T09:36:58.926338+00:00 heroku[router]:at=error code=H10 desc="App crashed" method=GET path="/ " host=testApp.herokuapp.com request_id=a3fb7146-0807-45e0-954d-23b2f19f1f98 fwd="188.218.155.100" dyno= connect= service= status=503 bytes= protocol=https
猜你喜欢
  • 2021-12-16
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 2017-12-26
  • 2021-11-16
相关资源
最近更新 更多