Node.js 将Angular 6部署到heroku后的空白页
我已经尝试将我的mean应用程序部署到heroku,Angular 6。部署成功,没有任何错误,但页面为空 我的package.json文件是:Node.js 将Angular 6部署到heroku后的空白页,node.js,angular,heroku,mean,Node.js,Angular,Heroku,Mean,我已经尝试将我的mean应用程序部署到heroku,Angular 6。部署成功,没有任何错误,但页面为空 我的package.json文件是: { "name": "koko-and-friend", "version": "0.0.0", "scripts": { "ng": "ng", "start": "node server.js", "build": "ng build", "test": "ng test", "lint": "ng lint
{
"name": "koko-and-friend",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "node server.js",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"postinstall": "ng build --aot --prod",
"preinstall": "npm install -g @angular/cli @angular/compiler-cli typescript @angular-devkit/build-angular"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.0.2",
"@angular/common": "^6.0.2",
"@angular/compiler": "^6.0.2",
"@angular/core": "^6.0.2",
"@angular/forms": "^6.0.2",
"@angular/http": "^6.0.2",
"@angular/platform-browser": "^6.0.2",
"@angular/platform-browser-dynamic": "^6.0.2",
"@angular/router": "^6.0.2",
"bootstrap": "^4.1.1",
"core-js": "^2.5.4",
"express": "^4.16.3",
"font-awesome": "^4.7.0",
"jquery": "^3.3.1",
"popper.js": "^1.14.3",
"primeng": "^6.0.0-alpha.1",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.2",
"@angular-devkit/build-angular": "~0.6.3",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.3",
"@angular/language-service": "^6.0.2",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
},
"engines": {
"node": "8.11.2",
"npm": "6.0.1"
}
}
my server.js:
const path = require('path');
const express = require('express');
const app = express();
app.use(express.static(path.join(__dirname, '/dist')));
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/koko-and-friend/index.html'));
});
app.listen(process.env.PORT || 4201, () => {
console.log('Connected to Port'); //Listening on port 4201
});
我的angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"koko-and-friend": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"styleext": "sass"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/koko-and-friend",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"./src/assets/styles/bootstrap.min.css",
"./node_modules/font-awesome/css/font-awesome.min.css",
"./node_modules/primeng/resources/themes/omega/theme.css",
"src/styles.scss"
],
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/popper.js/dist/umd/popper.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js"
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "koko-and-friend:build"
},
"configurations": {
"production": {
"browserTarget": "koko-and-friend:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "koko-and-friend:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.sass"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"koko-and-friend-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "koko-and-friend:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "koko-and-friend"
}
当我使用bash'dir'检查heroku控制台时,angular应用程序构建后的dist/koko和friend文件夹就在那里。这很奇怪,因为我在angular 4中使用了相同的选项,排除了预安装中的@angular devkit/build angular。我还检查了heroku日志,没有发现任何错误。您的NPM脚本没有任何将内容移动到特定文件夹的逻辑 这意味着您的页面应该位于
dist/index.html
,而不是dist/koko和friend/index.html
请同时发布
angular.json
(v5)或angular.json
(v6)文件的内容 尝试替换server.js中的以下行
app.use(express.static(path.join(uu dirname,/dist'))代码>
与
app.use(express.static(path.join(uu dirname,'/dist/koko and friend'))代码>您可以在angular.json中看到路径是dist/koko和friend/index.html。它是由Angular CLI自动创建的。我看到了是的。但是是自动创建的吗?我怀疑是这样。。。您的dist文件夹是否包含此文件夹?此外,由于使用沙盒(HeloCu),请考虑在脚本中显式输出。这意味着在您的package.json中,可以尝试以下操作:“build”:“ng build--prod--output path koko and friend”
,“postinstall”:“npm run build”
是的,路径是在执行此命令“ng new koko and friend”后自动创建的,当我检查angular.json构建路径时,就是这样。是的,dist文件夹包含koko和friend文件夹,这是应用程序的名称。您是否尝试使用显式输出?怎么回事?显式输出对heroku也不起作用,但我已经解决了。我在Angular.json中修改了自动创建的路径,并没有使用dist/koko和friend,而是使用dist only并将整个文件移动到dist文件夹,删除了koko和friend文件夹。我还使用angular v6创建了一个新的angular项目,构建路径非常类似于“dist/project name”。它在heroku中部署时也是空白的,而且日志或部署中也没有错误,因此服务器可以找到index.html。这太奇怪了。