使用Angular CLI创建的应用程序如何部署到Heroku上

事前准备

    • Herokuのアカウトを作成する

 

    • Heroku上にアプリを作成する

 

    Heroku CLIをインストールする

请根据以下内容进行事前准备:
在Heroku注册账号并创建应用程序。

将它变成用 “ng new” 创建的项目。

安装 Express

cd your-app-name
npm install express --save

修改package.json文件的描述

    • scripts内のstartの内容をnode server.jsに変更

 

    scripts内にpostinstallを追加
  "scripts": {
    "start": "node server.js",
    "postinstall": "ng build --aot --target=production"
  },
    • devDependenciesに記述されているangular系とtypescriptをdependenciesへ移動する

 

    enginesを追加する
  "engines": {
    "node": "6.10.3",
    "npm": "3.10.10"
  }

请参考下面的package.json文件。

{
  "name": "your-app-name",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "node server.js",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build --aot --target=production"
  },
  "private": true,
  "dependencies": {
    "@angular/cli": "1.2.7",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/animations": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "core-js": "^2.4.1",
    "express": "^4.15.3",
    "ng2-bootstrap": "^1.6.3",
    "rxjs": "^5.4.1",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "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.0.4"
  },
  "engines": {
    "node": "6.10.3",
    "npm": "3.10.10"
  }
}

创建一个server.js文件

    your-app-name 直下に作成する
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/dist'));
app.listen(process.env.PORT || 8080);
const path = require('path');
app.get('/*', function(req, res){
  res.sendFile(path.join(__dirname + '/dist/index.html'));
});
console.log('Server listening on port 8080');

将代码部署到Heroku平台上。

heroku login
# gitが初めての場合、git initする

heroku git:clone -a [heroku-app-name]
cd your-app-name
git add .
git commit -am "make it better"
git push heroku master

参考代码放在 GitHub 上。

广告
将在 10 秒后关闭
bannerAds