我正在尝试在heroku上部署Angular应用,但出现以下错误:
项目结构:
我的index.html
在src
文件夹中
Heroku日志:
2018-12-06T10:30:46.837610+00:00 heroku[web.1]: Starting process with command `npm start`
2018-12-06T10:30:50.434505+00:00 app[web.1]:
2018-12-06T10:30:50.434529+00:00 app[web.1]: > music-app@0.0.0 start /app
2018-12-06T10:30:50.434531+00:00 app[web.1]: > node server.js
2018-12-06T10:30:50.434532+00:00 app[web.1]:
2018-12-06T10:30:51.276734+00:00 heroku[web.1]: State changed from starting to up
2018-12-06T10:30:51.217887+00:00 app[web.1]: Server started running..
2018-12-06T10:30:51.000000+00:00 app[api]: Build succeeded
2018-12-06T10:31:05.060270+00:00 heroku[router]: at=info method=GET path="/" host=boulder-app.herokuapp.com request_id=69ed1cd2-fb68-499a-9568-bfb3af482188 fwd="115.112.65.146" dyno=web.1 connect=1ms service=58ms status=404 bytes=380 protocol=https
2018-12-06T10:31:05.049971+00:00 app[web.1]: Fetching from../app/dist/index.html
2018-12-06T10:31:05.060851+00:00 app[web.1]: Error: ENOENT: no such file or directory, stat '/app/dist/index.html'
我的Package.json:
{
"name": "music-app",
"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"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.0.3",
"@angular/common": "^6.0.3",
"@angular/compiler": "^6.0.3",
"@angular/core": "^6.0.3",
"@angular/forms": "^6.0.3",
"@angular/http": "^6.0.3",
"@angular/platform-browser": "^6.0.3",
"@angular/platform-browser-dynamic": "^6.0.3",
"@angular/router": "^6.0.3",
"bootstrap": "^4.1.2",
"core-js": "^2.5.4",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26",
"@angular/cli": "~6.0.8",
"@angular/compiler-cli": "^6.0.3",
"typescript": "~2.7.2",
"express":"^4.15.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.6.8",
"@angular/language-service": "^6.0.3",
"@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": "~2.0.0",
"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.14.0",
"npm":"~5.0.0"
}
}
我的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 STARTEGY
app.get('/*', function(req,res){
const fullPath = path.join(__dirname + '/dist/index.html');
console.log(" Fetching from.." + fullPath);
res.sendFile(fullPath);
})
console.log('Server started running..');
//Changed to run on Heroku
答案 0 :(得分:1)
得到答案:
在server.js中,我必须将路径从/dist
更改为/dist/music-app
,然后
从/dist/index.html
到/dist/music-app/index.html
。
其中music-app
是我的package.json文件中的名称属性["name": "music-app"
]。