部署Angle 9应用程序

时间:2020-05-24 20:24:09

标签: node.js angular github-pages web-hosting

我是新手。我构建并部署到github页面,但是我的REST api标注均无法正常工作。 我在heroku上部署了一个节点后端。这些API调用是在ngAfterViewInit()方法内部执行的。 我已将代理添加到后端节点表达服务器。 这是我的angular.json

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "musicsite-test2": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/musicsite-test2",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "musicsite-test2:build",
            "proxyConfig": "src/proxy.conf.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "musicsite-test2:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "musicsite-test2:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "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"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "musicsite-test2:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "musicsite-test2:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "musicsite-test2",
  "cli": {
    "analytics": false
  }
}

这是我的代理配置文件

{
    "/api/*": {
      "target": "https://village-voice-server.herokuapp.com",
      "secure": false,
      "logLevel": "debug",
      "changeOrigin": true
    }
  }

proxy conf文件位于/ src文件夹中。 我究竟做错了什么? 还有一个问题,我可以将我的Angular项目上传到由hostinger(网络主机)提供的普通网络服务器上吗?

1 个答案:

答案 0 :(得分:0)

第一件事!您选择了哪个选项来生成角度构建。如果通常由ng buil --prod完成,那么不用担心,但是使用```@ angular / universal之类的库将不会轻松地运行您的应用程序。对于此选项,您需要将其部署到提供node.js以便进行预渲染或服务器端渲染Web应用程序的平台。

现在,我假设您具有简单的角度构建(通过``ng build --prod”)。您需要检查开发过程中是否在运行应用程序以及将heroku作为节点后端。

下一个问题是向您的节点应用程序添加代理的需求是什么?如果您的节点api工作正常,甚至可以从其他服务器作为服务器,那么我认为没有任何必要。

最后,您询问是否要像托管服务商一样将其托管。显然,它甚至可以部署在静态托管平台中。但是缺点是基于javascript渲染。因此,这里不可能进行搜索引擎优化。

这就是为什么角度生成@angular/universal的原因。在这里,使用node,您可以得到想要的任何东西。当然,搜索引擎优化也是如此。