将Angular 5 + Nodejs Express应用部署到Heroku

20

我有一个Angular 5应用。

这是我在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

调试

我尝试运行这个程序

⚡️ web heroku 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日志显示构建成功。

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

但是当我打开这个应用:

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

我看到了这个:

enter image description here

我该如何进一步调试?


Angular是一个单页面应用程序(SPA),所以在运行ng build之后,你需要让express将所有的HTTP调用重定向回index.html,这样Angular才能接管。 - Mike Tung
好的,那我该怎么修复它呢?我已经附上了我的 server.js 文件。 - code-8
1
server.js 真的长这样吗?它充满了错误。 - Mika Sundland
我也关注这个链接:https://dev59.com/zFsW5IYBdhLWcg3wATDe#41171601 - code-8
你是否正在使用任何数据库连接? - Vipul Pandey
显示剩余3条评论
8个回答

15
这是我如何使我的Angular应用程序在Heroku上部署并工作的步骤:
  1. 您的server.js应该看起来像这样:https://hastebin.com/zavehahide.js
  2. 在您的package.json文件中,将@angular/cli@angular/compiler-clidevDependencies移至dependencies
  3. 同样在您的package.json文件中,在scripts中添加postinstall: ng build --prodstart: node server.js
这样做后,您就可以使用了。

3
通常当我遇到这个错误时,问题出在node的依赖上。尝试删除node_modules文件夹和dist文件夹。然后重新运行所有东西,这模拟了heroku如何构建你的项目。

2
在你的server.js文件中,你需要将http调用重定向到index页面。
app.route('/*', function(req,res) {
  res.redirect(__dirname + '/dist/index.html')
})

以上代码将重定向任何对你的index.html的调用。


我会根据你的建议更新我的server.js文件,接下来如何重新部署整个应用呢?使用git push heroku master命令吗? - code-8
另一个问题,我应该删除我的app.get(...)行吗?并用你的替换? - code-8
用我的代替你的,同时确保使用 ng build 命令生成 dist 文件夹并指向它。你不需要运行 ng serve。 - Mike Tung
我根本不使用 ng serve。我认为日志是缓存的。我将其删除,因为我不想让你们感到困惑。 - code-8
阅读文档,一旦你修复了它并按照我的指示运行,它就会工作。 - Mike Tung
显示剩余6条评论

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'); 

供日后参考 -

在访问URL之前尝试运行logs命令。

$ heroku logs

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

你是否分享了完整的构建日志?因为我在构建过程中没有看到你的安装脚本运行。尝试重新构建你的项目。 - Fuad
我应该在哪里进行重建?本地? - code-8
我认为我的 heroku logs 显示的是旧日志。我在这里提出了一个单独的问题:https://stackoverflow.com/q/48480640/4480164 - code-8
你还有其他建议吗? - code-8
@ihue 在Heroku上重新构建需要推送一个空的构建。 $ git commit --allow-empty -m "触发通知" $ git push heroku master - Fuad
显示剩余2条评论

1
尝试在package.json文件中添加@angular-devkit/build-optimizer作为一个包。
"dependencies": {
    "@angular-devkit/build-optimizer": "^0.4.0",
    ...
}

这允许运行安装后标志--aot。由于某种原因,此软件包不再内置。

1
在进一步调试时,运行您的应用程序,然后立即登录Heroku,在您的应用程序中,点击“更多”下拉菜单,然后点击“查看日志”。这应该会帮助您解决问题!
我相信还有一种方法可以通过终端查看实时日志,以便您可以看到具体失败的原因。

1

我建议更简单的做法是,如果你想在Heroku上部署后端作为API,前端在Github上并在浏览计算机上启用跨域资源共享,我正在构建这样的应用程序,这是我的计划。否则,如果你有任何好消息,请告诉我更新。


1
如果你在Heroku上构建产品,可能会遇到内存超出的问题。因此,FreeType无法使用。
所以尝试在本地构建,并使用Express来提供您的dist文件。
在PROC文件中使用相同的命令。
Heroku本地或Heroku本地Web:在本地验证更改。

谢谢,让我试着去做。 - code-8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接