尝试将Angular 8应用程序部署到Heroku-构建成功,但heroku提示“未找到”

我正在尝试将我的角度应用程序部署到heroku,构建成功,但是当我转到链接时,它告诉我“未找到”。我对所发生的事情感到非常困惑,因为我已经访问了经过这些步骤的各种网站,并且我确信自己会正确地遵循这些步骤。 我有一个procfile 网络:节点server.js

我的package.json看起来像这样-

{
  "name": "ng-barry-project","version": "0.0.0","scripts": {
    "ng": "ng","start": "node server.js","build": "ng build","test": "ng test","lint": "ng lint","e2e": "ng e2e","heroku-postbuild": "ng build --aot --prod"
  },"private": true,"dependencies": {
    "@angular/animations": "~8.2.0","@angular/cli": "^8.3.17","@angular/common": "~8.2.0","@angular/compiler": "~8.2.0","@angular/compiler-cli": "^8.2.13","@angular/core": "~8.2.0","@angular/forms": "~8.2.0","@angular/platform-browser": "~8.2.0","@angular/platform-browser-dynamic": "~8.2.0","@angular/router": "~8.2.0","@fortawesome/angular-fontawesome": "^0.5.0","@fortawesome/fontawesome-svg-core": "^1.2.25","@fortawesome/free-solid-svg-icons": "^5.11.2","express": "^4.17.1","path": "^0.12.7","rxjs": "~6.4.0","tslib": "^1.10.0","typescript": "~3.5.3","zone.js": "~0.9.1"
  },"devDependencies": {
    "@angular-devkit/build-angular": "~0.802.2","@angular/language-service": "~8.2.0","@types/jasmine": "~3.3.8","@types/jasminewd2": "~2.0.3","@types/node": "~8.9.4","codelyzer": "^5.0.0","enhanced-resolve": "^3.3.0","jasmine-core": "~3.4.0","jasmine-spec-reporter": "~4.2.1","karma": "~4.1.0","karma-chrome-launcher": "~2.2.0","karma-coverage-istanbul-reporter": "~2.0.1","karma-jasmine": "~2.0.1","karma-jasmine-html-reporter": "^1.4.0","protractor": "~5.4.0","ts-node": "~7.0.0","tslint": "~5.15.0","typescript": "~3.5.3"
  },"engines": {
    "node": "12.9.1","npm": "6.10.2"
  }
}

这是我的server.js文件

//Install express server
const express = require('express');
const path = require('path');

const app = express();

// Serve only the static files form the dist directory
app.use(express.static('./dist/pi-project'));

app.get('/*',function(req,res) {

res.sendFile(path.join(__dirname,'/dist/pi-project/index.html'));
});

// Start the app by listening on the default Heroku port
app.listen(process.env.PORT || 8080);
yashuhan 回答:尝试将Angular 8应用程序部署到Heroku-构建成功,但heroku提示“未找到”

请尝试这样。click here

,

这为我在heroku上部署6号和7号角工作。 试试这个-https://www.youtube.com/watch?v=hE1i_LhL_Nc

,

更新后的答案

您需要对server.js文件进行几次修改

更改此:

app.use(express.static('./dist/pi-project'));

为此:

app.use(express.static(path.join(__dirname,'./dist/pi-project')));

并更改此:

app.get('/*',function(req,res) {

对此

app.get('/',res) {

因此该功能不能满足所有请求

原始答案

在server.js中,您具有以下内容:

app.get('/*',res) {

res.sendFile(path.join(__dirname,'/dist/pi-project/index.html'));
});

由于/后面的星号,这意味着对于每个传入的请求都返回index.html文件。因此,将发生的情况是您首先向localhost:8080发出请求,然后将返回索引.html(到目前为止非常好)。然后,浏览器将从index.html读取<script>标签,并针对每个标签发出另一个请求。因此,假设下一个请求将发送到localhost:8080 / main.js。当express看到它将再次返回index.html时,这显然不是您想要的。

要解决该问题,您可以将server.js更改为此:

//Install express server
const express = require('express');
const path = require('path');

const app = express();

// Serve only the static files form the dist directory
app.use(express.static('./dist/pi-project'));

app.get('/*',res) {

  if(req.url === '/') {
    res.sendFile(path.join(__dirname,'/dist/pi-project/index.html'));
  }
  else {
    res.sendFile(path.join(__dirname,'/dist/pi-project' + req.url))
  }

});

// Start the app by listening on the default Heroku port
app.listen(process.env.PORT || 8080);
本文链接:https://www.f2er.com/3145248.html

大家都在问