簡體   English   中英

將Angular 5 + Nodejs Express應用程序部署到Heroku

[英]Deploy Angular 5 + Nodejs Express app to Heroku

我有一個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顯示Build Success。

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

我明白了

在此輸入圖像描述

我該如何進一步調試?

這是我如何使我的Angular應用程序部署和使用Heroku:

  1. 您的server.js應如下所示: https//hastebin.com/zavehahide.js
  2. package.json ,將@angular/cli@angular/compiler-clidevDependenciesdependencies
  3. 同樣在package.json ,將postinstall: ng build --prodstart: node server.jsscripts

你應該好好去。

通常當我發生此錯誤時,我遇到節點依賴性問題。 嘗試刪除node_modules文件夾和dist文件夾。 從那里再次旋轉一切,這模仿了heroku將如何構建你的項目。

我建議更簡單,如果你想在heroku上部署后端作為api和github上的前端並在瀏覽計算機上激活跨源資源共享,我實際上正在構建這樣的應用程序,這是我的計划,否則如果你得到任何這樣你的好消息讓我更新

在您的server.js您需要將http調用重定向到索引。

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

在上面它會將任何調用重定向到index.html。

在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

然后查看日志以獲取更多詳細信息。

如果你要在heroku上建立產品,你將面臨內存超出問題。 所以自由類型不起作用。

因此,嘗試在本地構建並使用express提供dist文件。

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

Heroku本地或Heroku本地網站:驗證您在本地的更改

在回答如何進一步調試。 運行您的應用程序,然后立即登錄Heroku,轉到您的應用程序,然后在“更多”下拉菜單中,單擊“查看日志”。 應該幫到你!

我相信有一種方法可以通過終端查看實時日志,這樣您就可以確切地看到失敗的內容。

嘗試在package.json文件中添加@angular-devkit/build-optimizer作為包。

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

這允許安裝后標志--aot運行。 由於某種原因,這個包不再構建。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM