首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Heroku上的Angular应用程序部署问题

Heroku上的Angular应用程序部署问题
EN

Stack Overflow用户
提问于 2018-06-09 08:10:31
回答 1查看 915关注 0票数 1

在Heroku上部署我的Angular应用程序时,我只收到“找不到”的信息,控制台中还有其他信息(“加载资源失败:服务器响应状态为404")。

下面是我收到的整个Heroku构建日志:

代码语言:javascript
复制
-----> Node.js app detected
-----> Creating runtime environment

       NPM_CONFIG_LOGLEVEL=error
       NODE_VERBOSE=false
       NODE_ENV=production
       NODE_MODULES_CACHE=true
-----> Installing binaries
       engines.node (package.json):  8.11.2
       engines.npm (package.json):   6.1.0

       Resolving node version 8.11.2...
       Downloading and installing node 8.11.2...
       Bootstrapping npm 6.1.0 (replacing 5.6.0)...
       npm 6.1.0 installed
-----> Restoring cache
       Loading 2 from cacheDirectories (default):
       - node_modules
       - bower_components (not cached - skipping)
-----> Building dependencies
       Installing node modules (package.json + package-lock)

       > aplikacja@0.0.0 postinstall /tmp/build_d1dd43479bc7dcb9ae65082b720aff2b
       > ng build --aot=prod

      31% building modules 181/189 modules 8 active …ules/rxjs/_esm5/inter       Date: 2018-06-08T23:55:35.459Z
       Hash: a1abfaf18b529ef61e77
       Time: 12592ms
       chunk {main} main.js, main.js.map (main) 27.8 kB [initial] [rendered]
       chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 227 kB [initial] [rendered]
       chunk {runtime} runtime.js, runtime.js.map (runtime) 5.22 kB [entry] [rendered]
       chunk {styles} styles.js, styles.js.map (styles) 15.6 kB [initial] [rendered]
       chunk {vendor} vendor.js, vendor.js.map (vendor) 3.5 MB [initial] [rendered]
       audited 22409 packages in 27.121s
       found 7 vulnerabilities (1 low, 5 moderate, 1 high)
       run `npm audit fix` to fix them, or `npm audit` for details
-----> Caching build
       Clearing previous node cache
       Saving 2 cacheDirectories (default):
       - node_modules
       - bower_components (nothing to cache)
-----> Pruning devDependencies
       removed 844 packages and audited 9173 packages in 12.907s
       found 0 vulnerabilities

-----> Build succeeded!
-----> Discovering process types
       Procfile declares types     -> (none)
       Default types for buildpack -> web
-----> Compressing...
       Done: 48.4M
-----> Launching...
       Released v7
       https://aplikacja-angular.herokuapp.com/ deployed to Heroku

我在我的package.json中对Heroku上的部署进行了所有必要的更改,结果如下所示:

代码语言:javascript
复制
{
  "name": "aplikacja",
  "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/cli": "~6.0.8",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/compiler-cli": "^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",
    "core-js": "^2.5.4",
    "express": "^4.16.3",
    "path": "^0.12.7",
    "rxjs": "^6.0.0",
    "typescript": "~2.7.2",
    "zone.js": "^0.8.26"
  },
  "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",
    "enhanced-resolve": "^3.3.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^2.0.2",
    "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.11.2",
    "npm": "6.1.0"
  }
}

我也尝试了设置"postinstall":"ng build --aot -prod“,但同样不起作用。

下面是我的server.js代码:

代码语言:javascript
复制
const express = require('express');
const path = require('path');
const app = express();


app.use(express.static(__dirname + '/dist'));

app.get('/*', function(req,res) {
    res.sendFile(path.join(__dirname+'/dist/index.html'));
});

app.listen(process.env.PORT || 8080);

我很困惑如何解决这个问题。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-09 09:29:49

如果您的应用程序是由6.x.x版的@angular/cli生成的,那么您的构建可能不仅仅是在dist文件夹中。如果您在angular.json中查找outputPath字段,它可能显示为dist/aplikacja。考虑到这一点,您可能希望更新您的server.js以...

代码语言:javascript
复制
const express = require('express');
const path = require('path');
const app = express();


app.use(express.static(__dirname + '/dist/aplikacja'));

app.get('/*', function(req,res) {
    res.sendFile(path.join(__dirname+'/dist/aplikacja/index.html'));
});

app.listen(process.env.PORT || 8080);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50769793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档