将 Angular 5 + Nodejs Express 应用程序部署到 Heroku [英] Deploy Angular 5 + Nodejs Express app to Heroku

查看:28
本文介绍了将 Angular 5 + Nodejs Express 应用程序部署到 Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Angular 5 应用.

这是我的 package.json

<代码>{名称":网络",版本":0.0.0",许可证":MIT",脚本":{ng":ng",开始":节点 server.js",build":ng build",测试":ng测试",lint":ng lint",e2e":ng e2e",安装后":ng build --aot --prod"},私人":真的,依赖关系":{@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-b​​rowser":5.0.3",@angular/platform-b​​rowser-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",引导程序":4.0.0-beta.2",core-js":2.4.1",快递":^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",粘性套件":1.1.3",打字稿":~2.4.2",zone.js":0.8.4";},开发依赖":{@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",茉莉花芯":~2.6.2",jasmine-spec-reporter":~4.1.0",业力":~1.7.0",karma-chrome-launcher":~2.1.1",karma-cli":~1.0.1",karma-coverage-istanbul-reporter":1.2.1",业力茉莉":~1.1.0",karma-jasmine-html-reporter":0.2.2",量角器":~5.1.2",ts-node":~3.2.0",tslint":~5.7.0"},引擎":{节点":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);//路径位置策略app.get('/'function(req,res) {res.sendFile(path.join(__dirname+'/dist/index.html'));});console.log('控制台监听');


然后,我运行这些命令

heroku auth:login电子邮件:johndoe@outlook.com密码 : #########heroku 创建 iproject 演示heroku git:远程 iproject 演示状态git add -Agit push heroku master


调试

我试过运行这个

⚡️ 网络 Heroku ps

本月剩余的免费测功小时配额:998h 46m (99%)有关 dyno 睡眠以及如何升级的更多信息,请参阅:https://devcenter.heroku.com/articles/dyno-sleeping=== 网络(免费):npm start (1)web.1: 崩溃 2018/01/27 14:18:58 -0500 (~ 1m 前)


结果

一切看起来都不错.

Heroku Log 显示构建成功.

----->检测到 Node.js 应用----->创建运行时环境NPM_CONFIG_LOGLEVEL=错误NPM_CONFIG_PRODUCTION=真NODE_VERBOSE=假NODE_ENV=生产NODE_MODULES_CACHE=真----->安装二进制文件引擎节点(package.json):未指定engine.npm (package.json):未指定(使用默认)正在解决节点版本 8.x...下载并安装节点 8.9.4...使用默认 npm 版本:5.6.0----->恢复缓存跳过缓存恢复(未找到)----->构建依赖安装节点模块 (package.json)在 5.46s 内添加了 26 个包----->缓存构建清除以前的节点缓存保存 2 个缓存目录(默认):- 节点模块- bower_components(无缓存)----->构建成功!----->发现进程类型Procfile 声明类型 ->(没有任何)buildpack 的默认类型 ->网络----->压缩...完成:34.4M----->发射...发布 v3https://iproject-demo.herokuapp.com/部署到 Heroku

但是当我去应用程序时:

我该如何进一步调试?

解决方案

以下是我如何让我的 Angular 应用程序在 Heroku 上部署和工作:

  1. 您的 server.js 应如下所示:https://hastebin.com/zavehahide.js
  2. 在你的 package.json 中,从 devDependencies 移动 @angular/cli@angular/compiler-cli> 到 dependencies
  3. 也在你的 package.json 中,将 postinstall: ng build --prodstart: node server.js 添加到 脚本

你应该没事了.

I have an Angular 5 App.

This is what I have in my 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"
  }
}


I created a server.js with these contents in it.

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


Then, I run these commands

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


Debug

I tried running this

⚡️ 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)


Result

everything looks good.

Heroku Log show 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 

But when I go to the app:

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

I see this

How can I debug this further?

解决方案

Here's how I make my Angular app to deploy and work on Heroku:

  1. Your server.js should look something like this: https://hastebin.com/zavehahide.js
  2. In your package.json, move @angular/cli and @angular/compiler-cli from devDependencies to dependencies
  3. Also in your package.json, add postinstall: ng build --prod and start: node server.js to scripts

You should be good to go.

这篇关于将 Angular 5 + Nodejs Express 应用程序部署到 Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆