将Angular 5 + Nodejs Express App部署到Heroku

时间:2018-01-27 19:16:12

标签: node.js angular heroku angular5 package.json

我有一个Angular 5 App。

这就是我在 package.json

中的内容
{
  "name": "web",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "node server.js",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build --aot --prod"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "5.1.0",
    "@angular/cli": "^1.6.4",
    "@angular/common": "5.0.3",
    "@angular/compiler": "5.0.3",
    "@angular/compiler-cli": "5.0.3",
    "@angular/core": "5.0.3",
    "@angular/forms": "5.0.3",
    "@angular/http": "5.0.3",
    "@angular/platform-browser": "5.0.3",
    "@angular/platform-browser-dynamic": "5.0.3",
    "@angular/router": "5.0.3",
    "@ng-bootstrap/ng-bootstrap": "1.0.0-beta.5",
    "@ngx-translate/core": "8.0.0",
    "@types/jquery": "3.2.16",
    "angular2-image-upload": "^1.0.0-rc.0",
    "bootstrap": "4.0.0-beta.2",
    "core-js": "2.4.1",
    "express": "^4.16.2",
    "jquery": "3.2.1",
    "jquery-slimscroll": "1.3.8",
    "ngx-toastr": "8.0.0",
    "ngx-uploader": "4.2.1",
    "pace-js": "1.0.2",
    "popper.js": "1.13.0",
    "rxjs": "5.5.0",
    "sticky-kit": "1.1.3",
    "typescript": "~2.4.2",
    "zone.js": "0.8.4"
  },
  "devDependencies": {
    "@angular/language-service": "5.0.3",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.2.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0"
  },
  "engines": {
    "node": "8.9.4",
    "npm": "5.6.0"
  }
}

我在其中创建了一个 server.js

constexpress=require('express');
constapp=expres();
constpath=require('path');

app.us(express.static(__dirname+'/dist'));

app.listen(process.env.PORT||8080);


//PathLocationStradegy
app.get('/'function(req,res) {
  res.sendFile(path.join(__dirname+'/dist/index.html'));
});

console.log('Console Listening'); 

然后,我运行这些命令

heroku auth:login
Email : johndoe@outlook.com
Password : #########
heroku create iproject-demo
heroku git:remote iproject-demo
git status
git add -A
git push heroku master

调试

我尝试过运行

⚡️webheroku ps

Free dyno hours quota remaining this month: 998h 46m (99%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping

=== web (Free): npm start (1)
web.1: crashed 2018/01/27 14:18:58 -0500 (~ 1m ago)

结果

一切看起来都不错。

Heroku Log 显示构建成功。

-----> Node.js app detected
-----> Creating runtime environment

       NPM_CONFIG_LOGLEVEL=error
       NPM_CONFIG_PRODUCTION=true
       NODE_VERBOSE=false
       NODE_ENV=production
       NODE_MODULES_CACHE=true
-----> Installing binaries
       engines.node (package.json):  unspecified
       engines.npm (package.json):   unspecified (use default)

       Resolving node version 8.x...
       Downloading and installing node 8.9.4...
       Using default npm version: 5.6.0
-----> Restoring cache
       Skipping cache restore (not-found)
-----> Building dependencies
       Installing node modules (package.json)
       added 26 packages in 5.46s
-----> Caching build
       Clearing previous node cache
       Saving 2 cacheDirectories (default):
       - node_modules
       - bower_components (nothing to cache)
-----> Build succeeded!
-----> Discovering process types
       Procfile declares types     -> (none)
       Default types for buildpack -> web
-----> Compressing...
       Done: 34.4M
-----> Launching...
       Released v3
       https://iproject-demo.herokuapp.com/ deployed to Heroku 

但是当我去APP时:

https://iproject-demo.herokuapp.com/

我看到了这个

enter image description here

问题

如何继续进行调试?

我现在对任何建议持开放态度。

任何提示/建议/帮助都将非常感谢!

8 个答案:

答案 0 :(得分:11)

以下是我如何使用Angular应用程序部署和使用Heroku:

  1. 您的server.js应该如下所示:https://hastebin.com/zavehahide.js
  2. package.json中,将@angular/cli@angular/compiler-clidevDependencies移至dependencies
  3. 同样在您的package.json中,将postinstall: ng build --prodstart: node server.js添加到scripts
  4. 你应该好好去。

答案 1 :(得分:2)

通常当我发生此错误时,我遇到节点依赖性问题。尝试删除node_modules文件夹和dist文件夹。从那里再次旋转一切,这模仿了heroku将如何建立你的项目。

答案 2 :(得分:2)

我建议更简单,如果你想在heroku上部署后端作为api和github上的前端并激活浏览计算机上的跨源资源共享,我实际上正在构建这样的应用程序,这是我的计划,否则如果你得到这方面的任何好消息你都在更新我

答案 3 :(得分:1)

server.js中,您需要将您的http调用重定向到索引。

app.route('/*', function(req,res) {
  res.redirect(__dirname + '/dist/index.html')
})

在上面,它会将任何调用重定向到index.html。

答案 4 :(得分:1)

更正Server.js中的代码

const express = require('express');
const app = express();
const path = require('path');

app.use(express.static(__dirname+'/dist'));

app.listen(process.env.PORT||8080);


//Path Location Strategy
app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname+'/dist/index.html'));
});

console.log('Console Listening'); 

供将来参考 -

在点击网址之前尝试运行logs命令。

$ heroku logs

然后查看日志以获取更多详细信息。

答案 5 :(得分:1)

如果你要在heroku上建立产品,你将面临内存超出问题。所以自由类型不起作用。

因此,尝试在本地构建并使用express提供dist文件。

在PROC文件中使用相同的命令

Heroku local或Heroku本地网站:验证您在本地的更改

答案 6 :(得分:1)

在回答如何进一步调试。运行您的应用程序,然后立即登录Heroku,转到您的应用程序,然后在“更多”下拉菜单中,单击“查看日志”。应该帮助你!

我相信有一种方法可以通过终端查看实时日志,这样您就可以确切地看到失败的原因。

答案 7 :(得分:1)

尝试在package.json文件中添加@angular-devkit/build-optimizer作为包。

"dependencies": {
    "@angular-devkit/build-optimizer": "^0.4.0",
    ...
}

这允许安装后标志--aot运行。出于某种原因,这个包不再构建了。