我正在尝试将Angular应用程序部署到Heroku。我这样做的经验有限,并且遇到错误。我对stackoverflow的搜索使我尝试了不同的配置,但是每次遇到错误时,我都会尝试。我得到的最接近的是脚本中没有包含“ postinstall”的情况,这种情况下构建成功,但是我仍然收到相同的代码H10应用程序错误。
我已经尝试过“ postinstall:ng build --aot --prod”,“ heroku-postbuild”:“ ng build --aot --prod”和ng build --aot --prod”,但是没有差异,由于此行,每次构建都会失败。
这是我的package.json
{
"name": "hockey-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "concurrently \"ng build --watch\" \"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": "^6.1.10",
"@angular/cdk": "^7.3.0",
"@angular/cli": "~6.2.5",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/compiler-cli": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/material": "^7.3.0",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/router": "^6.1.0",
"bootstrap": "^4.2.1",
"core-js": "^2.5.4",
"enhanced-resolve": "^4.1.0",
"express": "^4.16.4",
"path": "^0.12.7",
"rxjs": "~6.2.0",
"typescript": "~2.9.2",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.8.0",
"@angular/cli": "~6.2.5",
"@angular/compiler-cli": "^6.1.0",
"@angular/language-service": "^6.1.0",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.3.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~2.9.2"
},
"engines": {
"node": "8.11.4",
"npm": "6.4.1"
}
}
我的server.js
const express = require('express');
const path = require('path');
const app = express();
// Serve only the static files form the dist directory
app.use(express.static(__dirname, '/dist/hockey-app'));
app.get('*', function(req,res) {
res.sendFile(path.join(__dirname, '/dist/hockey-app/index.html'));
});
// Start the app by listening on the default Heroku port
app.listen(process.env.PORT || 8080);
感谢您的帮助。谢谢。
编辑:构建成功并部署,但继续出现Application错误。 Heroku日志:
2019-02-19T23:35:02.490703 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃”方法=获取路径=“ /” host = nhl-hockey-app.herokuapp.com request_id = cd86c7cd-aaa3-489f-b4ce-bb1c0a1f7e90 fwd =“ 151.202.21.77” dyno = connect =服务= status = 503字节= protocol = https 2019-02-19T23:35:02.861982 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃”方法=获取路径=“ / favicon.ico” host = nhl-hockey-app.herokuapp.com request_id = 1f51f401-9349-4ae6-9d1e-a354fffff8ee fwd =“ 151.202.21.77” dyno = connect = service = status = 503字节= protocol = https
答案 0 :(得分:1)
使用heroku-postbuild,它将代替普通的构建脚本。如果使用postinstall,它将两次构建应用程序(npm run postinstall && npm run build)。将代码推送到heroku时,您应该能够看到它。
当您的应用程序成功构建后,heroku将使用启动脚本来启动您的应用程序。我认为您的开始脚本无法在heroku上运行,因为它同时是一个npm模块,我在package.json中看不到,而“ ng build --watch”则用于本地开发,而不是用于在上面运行代码heroku。
我建议为您的项目文件夹创建一个Procfile。
https://devcenter.heroku.com/articles/getting-started-with-nodejs#define-a-procfile
Heroku将使用此命令来启动应用程序服务器。
Procfile:
web: node server.js
答案 1 :(得分:0)
尝试移动
"@angular/cli": "~6.2.5",
"@angular/compiler-cli": "^6.1.0"
从devDependencies
到dependencies
...